diff options
author | 杨宇千 <crupest@outlook.com> | 2019-04-11 20:02:33 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-11 20:02:33 +0800 |
commit | 5b5ca3acb1b9decb5ad13798dc79ba2d58f2ce95 (patch) | |
tree | 695f7fc0bd2f6d940f64739a1f1f500c36806cef /Timeline/ClientApp/src/app/user/auth.guard.ts | |
parent | 1c9edc5914869a3bbde20742c483182636ee4d43 (diff) | |
parent | c28941c6d86f8ea33521bba49d811bf3ff60b3d1 (diff) | |
download | timeline-5b5ca3acb1b9decb5ad13798dc79ba2d58f2ce95.tar.gz timeline-5b5ca3acb1b9decb5ad13798dc79ba2d58f2ce95.tar.bz2 timeline-5b5ca3acb1b9decb5ad13798dc79ba2d58f2ce95.zip |
Merge pull request #17 from crupest/15-user
Remember me and log out feature.
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; + })); } } |