Android 记住爱奥尼亚Firebase应用程序中的用户
我正在编写一个使用Firebase作为后端的Ionic应用程序,我正在努力让用户登录。 目前我只在安卓系统上进行了测试 当应用程序启动时,我使用Android 记住爱奥尼亚Firebase应用程序中的用户,android,angularjs,authentication,firebase,ionic-framework,Android,Angularjs,Authentication,Firebase,Ionic Framework,我正在编写一个使用Firebase作为后端的Ionic应用程序,我正在努力让用户登录。 目前我只在安卓系统上进行了测试 当应用程序启动时,我使用firebase.auth().signiWithEmailandPassword登录用户 登录后,如果用户多次按物理后退按钮关闭应用程序,然后他们再次启动应用程序,他们必须再次登录,因为应用程序已丢失其凭据(firebase.auth()。currentUser为空) 我希望应用程序在用户登录时记住设备上的用户,并且只有在用户特别点击应用程序注销按钮时
firebase.auth().signiWithEmailandPassword
登录用户
登录后,如果用户多次按物理后退按钮关闭应用程序,然后他们再次启动应用程序,他们必须再次登录,因为应用程序已丢失其凭据(firebase.auth()。currentUser
为空)
我希望应用程序在用户登录时记住设备上的用户,并且只有在用户特别点击应用程序注销按钮时才将其注销
下面是我的app.js中的代码
angular.module('starter', [
'ionic',
'firebase',
'starter.loginController',
'starter.resetemailController',
'starter.signupController',
'starter.dashboardController',
'starter.sidebarController'
])
.run(function ($ionicPlatform, $rootScope, $state, CONFIG) {
$ionicPlatform.ready(function () {
if (window.cordova && window.cordova.plugins.Keyboard) {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
// Don't remove this line unless you know what you are doing. It stops the viewport
// from snapping when text inputs are focused. Ionic handles this internally for
// a much nicer keyboard experience.
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
cordova.plugins.Keyboard.disableScroll(true);
}
if (window.StatusBar) {
StatusBar.styleDefault();
}
});
// on stateChange event if state requires authentication and user not logged in, go to login state
$rootScope.$on("$stateChangeStart", function (event, toState, toParams, fromState, fromParams) {
var user = firebase.auth().currentUser;
if (toState.authRequired && !user) {
$state.go("login");
event.preventDefault();
}
});
// initialise firebase
firebase.initializeApp({
apiKey: 'xxxxxxxxx',
authDomain: 'xxxxxxx.firebaseapp.com',
databaseURL: 'https://xxxxxx.firebaseio.com',
storageBucket: 'xxxxxxxxxx.appspot.com',
messagingSenderId: 'xxxxxxxxxxxxxx'
});
})
.config(['$stateProvider', '$urlRouterProvider', '$ionicConfigProvider', function ($stateProvider, $urlRouterProvider, $ionicConfigProvider) {
$ionicConfigProvider.navBar.alignTitle('center');
$stateProvider
.state('app', {
url: '/app',
abstract: true,
templateUrl: 'shared/sidebar/sidebarView.html',
controller: 'sidebarController'
})
.state('app.dashboard', {
url: '/dashboard',
views: {
'menuContent': {
templateUrl: "components/dashboard/dashboardView.html",
controller: "dashboardController"
}
},
authRequired: true
})
.state('login', {
cache: false,
url: '/login',
templateUrl: "components/login/loginView.html",
controller: "loginController"
})
.state('signup', {
url: '/signup',
templateUrl: "components/signup/signupView.html",
controller: "signupController"
})
.state('reset', {
url: '/reset',
templateUrl: "components/resetemail/resetemailView.html",
controller: "resetemailController"
})
// this is a workaround to prevent the $stateChangeStart firing multiple times
$urlRouterProvider.otherwise(function ($injector) {
var $state = $injector.get('$state');
$state.go('app.dashboard');
});
}])
当用户登录时,$urlRouterProvider。否则
将应用程序作为初始状态转到仪表板
如果用户为null,则以下代码将重新路由到登录状态
$rootScope.$on("$stateChangeStart", function (event, toState, toParams, fromState, fromParams) {
var user = firebase.auth().currentUser;
if (toState.authRequired && !user) {
$state.go("login");
event.preventDefault();
}
});
这一切都很好,但一旦用户关闭应用程序,我希望应用程序记住用户,以便下次启动时,他们将直接进入仪表板状态,而不必每次都登录
当用户登录时,我不存储任何内容-我需要在本地存储中存储一些凭据吗
我读过关于JWT的书,但我不完全理解如何创建或使用它们
我看到一些帖子提到了firebase.auth().currentUser.getToken(true)
,但同样,我不知道如何处理它-登录后我需要将此令牌存储在某个地方吗
抱歉,如果这一切有点模糊;我对这件事真的很陌生,现在很多事情都让我不知所措