[英]Ionic 1 - Show login page if user not logged and skip if user is already logged
Working on an Ionic version 1.3.3 application where need following functionalities for user login. 在Ionic 1.3.3版应用程序上工作,需要遵循用户登录功能。 I had go through all stackoverflow answer but nothing found a workable solution for me.
我已经完成了所有stackoverflow的答案,但没有找到适合我的解决方案。
On login page after login success redirect to home page and clear login page history. 登录成功后登录页面重定向到主页并清除登录页面历史记录。
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'); })
This is how I accomplished this in Ionic v1: For the redirect if user is logged in: 这就是我在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();
}
]
}
}
}
})
For the redirect when user logs in or is already logged in. 用于登录或已登录时的重定向。
.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();
}
]
}
}
}
})
Auth service isAuthenticated() 验证服务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.