繁体   English   中英

记住Facebook登录Ionic

[英]Remember Facebook login Ionic

我的应用程序中有一个Facebook登录名,但是每次关闭并重新打开该应用程序时,我都需要使用Facebook重新登录。

如何保存用户的Facebook登录名,所以不必再次单击Facebook登录名,而只需重定向到主页?

我的TS登录名是:

loginWithFB(userData) {


this.fb.login(['email', 'public_profile']).then((response: FacebookLoginResponse) => {
  this.fb.api('me?fields=id,name,email,first_name,picture.width(720).height(720).as(picture_small)', []).then(profile => {
    this.userData = { email: profile['email'], first_name: profile['first_name'], picture: profile['picture_small']['data']['url'], username: profile['name'] }

    if (this.userData != "") {

      this.navCtrl.setRoot(HomePage, { 'logfb': this.userData })

      this.events.publish('loginfacebook', this.userData, Date.now());
      //  this.navCtrl.push(HomePage, {
      //   'logfb': this.userData
      // });
    } else {
      let toast = this.toastCtrl.create({ duration: 3000, position: 'bottom' 
  });
      toast.setMessage('Não foi possível logar com Facebook');
      toast.present();
    }
  })

});

}

为此,请使用localstorage,以便每当您重新加载页面时,localstorage都会看到您已登录并保持登录状态,并且每当您要注销时,请使用按钮并清空localstorage

使用Localstorage插件。

从用户登录页面调用loginWithFB() 在该函数内部,将登录用户设置为localstorage

loginWithFB(userData) {
        this.fb.login(['email', 'public_profile']).then((response: FacebookLoginResponse) => {
                    this.fb.api('me?fields=id,name,email,first_name,picture.width(720).height(720).as(picture_small)', [])
                    .then(profile => {
                        this.userData = {
                            email: profile['email'],
                            first_name: profile['first_name'],
                            picture: profile['picture_small']['data']['url'],
                            username: profile['name']
                        }
                        // Set user to storage
                        this.nativeStorage.setItem('user', this.userData)
                    })
                    .then(() => { // Redirect to user page })
        // ....

最后一步是在platform is readylocalstorage platform is ready时获取用户数据:

例:

platform.ready().then(() => {
      // Here we will check if the user is already logged in
      // because we don't want to ask users to log in each time they open the app
      let env = this;
      this.nativeStorage.getItem('user')
      .then( function (data) {
        // user is previously logged and we have his data
        // we will let him access the app
        env.nav.push(UserPage);
        env.splashScreen.hide();
      }, function (error) {
        //we don't have the user data so we will ask him to log in
        env.nav.push(LoginPage);
        env.splashScreen.hide();
      });
      this.statusBar.styleDefault();
    });

这是非常有用的链接: Facebook登录

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM