我正在尝试在 ionic 应用程序内部实现单点登录;这样,当用户第一次登录(除非退出)时,他应该可以直接访问该应用程序,而无需多次登录。
该实现仅在用户被定向到应用程序页面之前起作用,他会短暂地看到启动屏幕,然后短暂地看到登录页面,最后是我希望他在应用程序午餐时看到的正确页面。
我有一个名为 UserService
的服务,它会在首次登录时将用户数据保存到本地存储。
然后我检查了UserService中是否存在数据,如果存在,则将用户带到应用程序页面,否则按照正常的登录流程进行操作。这是在 app.js
内的运行中完成的,如下所示
.run(function($ionicPlatform, $rootScope, $ionicHistory, $state, UserService,localStorage)
{
$ionicPlatform.ready(function() {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if(window.cordova && window.cordova.plugins.Keyboard)
{
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if(window.StatusBar)
{
StatusBar.styleDefault();
}
// implement single sign on here
user = UserService.getUser().loggedInUserId;
if(user !==undefined )
{
console.log("user exists")
$state.go('app.apppage');
}
else
{
console.log("user does not exist")
}
})
}
任何帮助将不胜感激
最佳答案
这可以通过代码处理闪屏来实现。
添加闪屏插件
cordova plugin add https://github.com/apache/cordova-plugin-splashscreen.git
在 config.xml 中禁用 AutoHideSplashScreen 属性
<preference name="AutoHideSplashScreen" value="false" />
然后修改游览代码,如下所示,
.run(function($ionicPlatform, $rootScope, $ionicHistory, $state, UserService,localStorage, $timeout)
{
$ionicPlatform.ready(function() {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if(window.cordova && window.cordova.plugins.Keyboard)
{
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if(window.StatusBar)
{
StatusBar.styleDefault();
}
// implement single sign on here
user = UserService.getUser().loggedInUserId;
if(user !==undefined )
{
console.log("user exists")
$state.go('app.apppage');
}
else
{
console.log("user does not exist")
}
//====================
// hide splash screen
//====================
$timeout(function() {
// clear history to prevent the user from navigating back to login page
$ionicHistory.clearHistory();
navigator.splashscreen.hide();
}, 1000);
//=======================
})
}
希望有帮助。
关于javascript - 如何在 ionic 应用程序内正确实现单点登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36304486/