ionic2 初步实现 Authentication
来源:互联网 发布:淘宝卖家不说话怎么办 编辑:程序博客网 时间:2024/04/27 17:29
用处:当用户打开 APP 后会判断用户 token 是否已失效,若未失效则进入首页,否则进入登录页面。当用户登出或正在操作的时候 token 失效都会回到登录界面。
首先我们新建一个 provider:ionic g provider AuthenticationService
打开这个文件,引用BehaviorSubject:import { BehaviorSubject } from 'rxjs/BehaviorSubject';
BehaviorSubject 是 Subject 的一个衍生类,具有“最新的值”的概念。它总是保存最近向数据消费者发送的值,当一个 Observer 订阅后,它会即刻从 BehaviorSubject 收到“最新的值”。
代码如下:
export class AuthenticationService { activeUser = new BehaviorSubject(null); constructor() { } doLogin(_username) { this.activeUser.next(_username); } doLogout() { this.activeUser.next(null); }}
接下来我们来注册用户认证事件。
打开 app.component.ts 文件,引入 AuthenticationService,注册用户认证事件以及 rootPage,代码如下:
export class MyApp { rootPage: any; public static backButtonPressed: boolean = false; // 用于判断返回键是否触发 @ViewChild('myNav') nav: Nav; constructor(public ionicApp: IonicApp, public platform: Platform, private toastService: ToastService, private paramService: ParamService, private auth: AuthenticationService) { platform.ready().then(() => { // Okay, so the platform is ready and our plugins are available. // Here you can do any higher level native things you might need. StatusBar.styleDefault(); Splashscreen.hide(); if (!this.paramService.isNull(localStorage.getItem("token_name"))) { auth.doLogin(localStorage.getItem("token_name")); } this.registerBackButtonAction(); // 注册返回按键事件 this.registerAuthentication(); // 注册用户认证事件 }); } registerAuthentication() { this.auth.activeUser.subscribe((_user) => { if (_user) { this.rootPage = TabsPage; } else { this.rootPage = LoginPage; } }); } registerBackButtonAction() { this.platform.registerBackButtonAction(() => { this.backButtonAction(); }, 999); } backButtonAction() { // 如果想点击返回按钮隐藏 toast 或 loading 或 Overlay 就把下面加上 // this.ionicApp._toastPortal.getActive() || this.ionicApp._loadingPortal.getActive() || this.ionicApp._overlayPortal.getActive() let activePortal = this.ionicApp._modalPortal.getActive(); if (activePortal) { activePortal.dismiss().catch(() => {}); activePortal.onDidDismiss(() => {}); return; } let activeVC = this.nav.getActive(); let page = activeVC.instance; if (!(page instanceof TabsPage)) { if (!this.nav.canGoBack()) { return this.showExit(); } return this.nav.pop(); } let tabs = page.tabs; let activeNav = tabs.getSelected(); return activeNav.canGoBack() ? activeNav.pop() : this.showExit(); } showExit() { if (MyApp.backButtonPressed) { this.platform.exitApp(); } MyApp.backButtonPressed = true; this.toastService.presentToast("再点一次退出"); setTimeout(() => { MyApp.backButtonPressed = false; }, 2000); }}
首先对 localStorage 中检验是否有保留用户登录信息,若有则初始化用户认证并进入首页,否则进入登录页。
接下来就是业务中对 AuthenticationService 的操作,分别调用 doLogin 和 doLogout 来实现用户登录和登出。
当 activeUser 发生改变时就会触发订阅时的回调函数,判断用户状态来操作 rootPage 实现登录登出功能。
以后将进一步实现 Authentication 的功能。
2 0
- ionic2 初步实现 Authentication
- ionic2中实现 IndexedDB
- ionic2项目实现项目打包
- ionic2
- ionic2实现社会化分享Social Sharing
- ionic2 angular2自定义pipe ,orderby实现排序
- ionic2 一.基础 (8)登录页实现
- ionic2实现通讯录,联系人搜索功能
- Authentication
- 什么?在ionic2上实现支付宝支付
- ionic2上实现跳转应用市场的功能
- Ionic2实现: 下拉刷新和上拉加载
- Ionic2创建一个自定义pipe实现千分位号
- Ionic2学习笔记--Slide欢迎界面的实现
- Ionic2 使用loading组件实现下载进度显示效果
- ionic2长列表返回顶端控件的一个简单实现
- 关于ionic2 自定义插件并调用的简单实现
- ionic2下拉刷新和上拉加载功能实现
- OpenResty 技能图谱以及学习要点
- 九年开发生涯,而立之年带着一家老小离开了奋斗的广州
- 第一个chrome extension
- Java算法-剑指offer-二叉树的深度
- 我的第一篇文章,致我曾今努力的那些年
- ionic2 初步实现 Authentication
- Ubuntu下修改环境变量的三种方法
- 浏览器缓存知识及应用
- spring @Qualifier注解
- Jquery选择器
- 手动实现Aop
- Linux 库总结
- 好心情才会有好风景
- 教你十分钟快速搭建springBoot项目实战