How to increase Firebase Authentication Session Time using Flutter?

别说谁变了你拦得住时间么 提交于 2021-02-11 12:30:14

问题


I developed an Agro App and I have enabled offline persistence because, after logging in, operators can spend 3-4 hours recording information offline until they return to the central point where there is internet.

The problem I have is that sometimes the application asks for credentials (login and password) again when they do not have internet, which means that they cannot record information offline or they have to go back to the central point to authenticate which is not efficient.

I understand that the Firebase auth token lasts up to an hour, how can I extend this time?

The way I am authenticating users using a provider is like following:

class UsuarioProvider {
  final FirebaseAuth _firebaseAuth;
  DatabaseReference db = FirebaseDatabase.instance.reference();

  UsuarioProvider({FirebaseAuth firebaseAuth})
    : _firebaseAuth = firebaseAuth ?? FirebaseAuth.instance;

 Future <Map<String, dynamic>> signIn(String email, String password) async {
   
   try {
      UserCredential result = await FirebaseAuth.instance.signInWithEmailAndPassword(email: email, password: password);
      User user = result.user;
      final token = await user.getIdToken();
      return {'ok' : true, 'token' : token, 'localId': user.uid, 'email' : user.email};
   } catch (e) {
        print(e);
      return {'ok': false, 'code': '${e.code}', 'mensaje': '${e.message}' }; 
   }
 }

How can I increase the Firebase Token Time? or How can I prevent users from being prompted for credentials when they are offline?

UPDATE: The way I'm using to call firebase is:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  final prefs = new PreferenciasUsuario();
  await prefs.initPrefs();
  final pushNotificationsProvider = new PushNotificationsProvider(); 
  pushNotificationsProvider.initNotifications();
  FirebaseDatabase database;
  database = FirebaseDatabase.instance;
  database.setPersistenceEnabled(true);
  database.setPersistenceCacheSizeBytes(10000000); // 10MB de cache
  runApp(MyApp());
} 

回答1:


Firebase Authentication uses two types of tokens: a refresh token that "never" expires, and a short-lived ID token that is valid for an hour but is auto-refreshed by the SDK. There is no way to expand the life-time of the ID tokens.

If you want to pass a value along to a client that they can use for a longer period of time, you can mint an authentication cookie, which can be valid for up to two weeks. Note that this is a sensitive operation, so it can only be performed in trusted environments (typically by using the Firebase Admin SDK). For a full walkthrough of the process, see the Firebase documentation on Managing Session Cookies.



来源:https://stackoverflow.com/questions/65373623/how-to-increase-firebase-authentication-session-time-using-flutter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!