如何在ionic应用程序中正确实现单点登录

how to properly implement single sign in inside ionic app

本文关键字:实现 单点 登录 ionic 应用程序      更新时间:2023-10-07

我正在尝试在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")
        }
    })
}

如有任何帮助,将不胜感激

这可以通过代码处理splashscreen来实现。

添加防溅屏插件

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);
        //=======================
    })
}

希望能有所帮助。