diff options
author | crupest <crupest@outlook.com> | 2019-03-18 21:21:56 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2019-03-18 21:21:56 +0800 |
commit | 110f2ab8d7bf5cf5fefee2fadb89f0e548dc0f4d (patch) | |
tree | 43b4694b7b1626b746f56d3937c1f58a4b8f15fb /Timeline/ClientApp/src/app/user/auth.guard.ts | |
parent | 442d784b2a3ed0c8f0c64d1aaae2b1e0fbefe01f (diff) | |
download | timeline-110f2ab8d7bf5cf5fefee2fadb89f0e548dc0f4d.tar.gz timeline-110f2ab8d7bf5cf5fefee2fadb89f0e548dc0f4d.tar.bz2 timeline-110f2ab8d7bf5cf5fefee2fadb89f0e548dc0f4d.zip |
Add logout. Fix a bug.
The bug is it always goes to login page whether you have login or not before when user is presented in url.
Diffstat (limited to 'Timeline/ClientApp/src/app/user/auth.guard.ts')
-rw-r--r-- | Timeline/ClientApp/src/app/user/auth.guard.ts | 33 |
1 files changed, 17 insertions, 16 deletions
diff --git a/Timeline/ClientApp/src/app/user/auth.guard.ts b/Timeline/ClientApp/src/app/user/auth.guard.ts index 561a0c53..1fc7a7c0 100644 --- a/Timeline/ClientApp/src/app/user/auth.guard.ts +++ b/Timeline/ClientApp/src/app/user/auth.guard.ts @@ -1,6 +1,7 @@ import { Injectable } from '@angular/core'; import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree } from '@angular/router'; import { Observable } from 'rxjs'; +import { take, map } from 'rxjs/operators'; import { InternalUserService } from './internal-user-service/internal-user.service'; @@ -23,26 +24,26 @@ export abstract class AuthGuard implements CanActivate { return true; } - const { currentUserInfo } = this.internalUserService; - - if (currentUserInfo === null) { - if (authStrategy === 'requirenologin') { - return true; - } - } else { - if (authStrategy === 'requirelogin') { - return true; - } else if (authStrategy instanceof Array) { - const { roles } = currentUserInfo; - if (authStrategy.every(value => roles.includes(value))) { + return this.internalUserService.userInfo$.pipe(take(1), map(userInfo => { + if (userInfo === null) { + if (authStrategy === 'requirenologin') { return true; } + } else { + if (authStrategy === 'requirelogin') { + return true; + } else if (authStrategy instanceof Array) { + const { roles } = userInfo; + if (authStrategy.every(value => roles.includes(value))) { + return true; + } + } } - } - // reach here means auth fails - this.onAuthFailed(); - return false; + // reach here means auth fails + this.onAuthFailed(); + return false; + })); } } |