Search code examples
firebasefirebase-authenticationionic3ionic-storage

How to store firebase user email and password in Ionic Storage


I'm new to ionic 3 and firebase. I have built an application that uses email and password to authenticate users. Right now whenever you quit the app, It logs the user out and they have to re-login which can be a hassle. I have seen hints of some users saving the user credentials (email and password) in Storage (@ionic/storage) so the next time they use the app it will automatically sign them in. Is this the correct way to go about this? And if so, could I please see and example? Any help would be great.


Solution

  • Yes, of course, but it better store with checked checkbox 'Remember me', that allows user to make choise use it or not.

    And i prefer use NativeStorage

    import { NativeStorage } from '@ionic-native/native-storage';
    
    let login: string;
    
    constructor(private nativeStorage: NativeStorage, ...) {
        this.login = '';
    }
    

    Store example:

    this.nativeStorage.setItem('LOGIN', 'Your Value').then(
        () => {
            // Do something, optional
        },
        error => {
    
        }
    );
    

    Getting example:

    this.nativeStorage.getItem('LOGIN').then(
        (data) => {
            // Get value and put it into our variable
            this.login = data;
        },
        error => {
    
        }
    );
    

    And use it in your view:

    <ion-input [(ngModel)]="login" type="text"></ion-input>
    

    More info about NativeStorage:

    https://ionicframework.com/docs/native/native-storage/