[英]Ionic 1 - Show login page if user not logged and skip if user is already logged
在Ionic 1.3.3版應用程序上工作,需要遵循用戶登錄功能。 我已經完成了所有stackoverflow的答案,但沒有找到適合我的解決方案。
登錄成功后登錄頁面重定向到主頁並清除登錄頁面歷史記錄。
angular.module('starter', ['ionic', 'starter.controllers', 'starter.directives', 'starter.services', 'ngStorage','ab-base64',]) .config(function ($stateProvider, $urlRouterProvider) { $stateProvider .state('app', { url: '/app', abstract: true, cache: false, templateUrl: 'templates/menu.html', controller: 'AppCtrl', onEnter: function ($state) { console.log($state); } }) .state('app.home', { cache: false, url: '/home', views: { 'menuContent': { templateUrl: 'templates/home.html' } } }) .state('app.login', { cache: false, url: '/login/:username/:password', views: { 'menuContent': { templateUrl: 'templates/login.html', controller: 'LoginController' } } }) .state('app.profile', { cache: false, url: '/profile', views: { 'menuContent': { templateUrl: 'templates/profile.html', controller: 'ProfileController' } } }) $urlRouterProvider.otherwise('/app/home'); })
這就是我在Ionic v1中完成此操作的方法:對於用戶登錄時的重定向:
.state("app.dash", {
url: "/dashboard",
abstract: true,
views: {
mainContent: {
templateUrl: "templates/dashboard.html",
controller: "DashboardCtrl",
controllerAs: "vm",
resolve: {
auth: [
"authService",
function(authService) {
return authService.isAuthenticated();
}
],
permissions: [
"authService",
function(authService) {
return authService.getPermissions();
}
]
}
}
}
})
用於登錄或已登錄時的重定向。
.state("app.login", {
url: "/login?accountCreated",
views: {
mainContent: {
templateUrl: "templates/login.html",
controller: "LoginCtrl",
controllerAs: "vm",
resolve: {
isLoggedIn: [
"$q",
"$state",
"authService",
function($q, $state, authService) {
authService.isAuthenticated().then(function(res) {
$state.go("app.dash.home");
});
return $q.defer().resolve();
}
]
}
}
}
})
驗證服務isAuthenticated()
function isAuthenticated() {
var deferred = $q.defer();
getToken().then(function(token) {
isExpired().then(function(isExpired) {
if (!token || isExpired) {
deferred.reject("Not Authenticated");
} else {
decodeToken().then(function(decodedToken) {
deferred.resolve(decodedToken);
});
}
});
});
return deferred.promise;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.