tanutapi / dart_meteor

A Meteor DDP library for Dart/Flutter developers.
Other
37 stars 15 forks source link

Auto login #13

Closed YouSour closed 4 years ago

YouSour commented 4 years ago

hi , bro , how to auto login existing user ? do i need any package to do it (example : a package that store session to local storage for checking if user exist) ? 🤔

tanutapi commented 4 years ago

I use https://pub.dev/packages/flutter_secure_storage to store login token. Here is my snippet code from my app.

Function that store login token and its expired date for using later.

_storeMongoClientLoginResult(MeteorClientLoginResult result) async {
  if (result != null) {
    await _secureStorage.write(key: 'loginToken', value: result.token);
    await _secureStorage.write(
        key: 'loginTokenExpires',
        value: result.tokenExpires.millisecondsSinceEpoch.toString(),
    );
  }
}

Somewhere in your code that handle a password login.

meteor.loginWithPassword(username, password, delayOnLoginErrorSecond: 10).then((result) async {
  _storeMongoClientLoginResult(result);
  ...
  ...
}

Next time, try to login with the token first. You may put it into your initState().

// Try to connect with previously saved token.
_secureStorage.read(key: 'loginTokenExpires').then((tokenExpiresStr) {
  if (tokenExpiresStr != null) {
    int millisecond = int.parse(tokenExpiresStr);
    DateTime expires = DateTime.fromMillisecondsSinceEpoch(millisecond);
    _secureStorage.read(key: 'loginToken').then((token) {
      if (token != null) {
        meteor.loginWithToken(token: token, tokenExpires: expires).then((result) {
          _storeMongoClientLoginResult(result);
        }).catchError((_) {});
      }
    });
  }
});
YouSour commented 4 years ago

ok , so we need a package that store session to local storage , i got it , thank you 😄

YouSour commented 4 years ago

hi , bro , @tanutapi i try to do what you told me , but it isn't work , the part that read token and check user token exist or not , can you check my source code https://drive.google.com/file/d/11U1AidxqEkCFhRJzqawob3h2Q6q4cyRc/view?usp=sharing , you can check main.dart & lib/screen/login.dart (📌 Note : it works only when i run my flutter project)

YouSour commented 4 years ago

@tanutapi everything it works when i run project by cmd "flutter run" but if i don't run that command , and i open my app that already install on emulator or real device it doesn't work , what should i do ? 🤔

tanutapi commented 4 years ago

On Flutter development, the hot code reload change the app logic but it is not persist. If you unplug your phone, the app that already installed might not be the latest version you want. Try to uninstall the app or kill the debugging and run it again to update the binary.

You can make a simple project just for test your login logic.

  1. On app opening, check the existing login token available?
  2. If yes try to login with that token.
  3. If not show login page to the user.
  4. User provides username and password, then try to login.
  5. If login success, store login token for later use.
YouSour commented 4 years ago

@tanutapi i don't know how can i debug code when i kill or close app , because when i close or kill app "flutter run" stop working and debug console stop working too 🤔

YouSour commented 4 years ago

@tanutapi problem solved , the problem my code that checking user token exist or not isn't finish their processing in initState() so i need async and await now it works 😄 sorry, my bad