aboutsummaryrefslogtreecommitdiff
path: root/Timeline/ClientApp/src/app/user/internal-user-service/internal-user.service.ts
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2019-03-18 21:21:56 +0800
committercrupest <crupest@outlook.com>2019-03-18 21:21:56 +0800
commit110f2ab8d7bf5cf5fefee2fadb89f0e548dc0f4d (patch)
tree43b4694b7b1626b746f56d3937c1f58a4b8f15fb /Timeline/ClientApp/src/app/user/internal-user-service/internal-user.service.ts
parent442d784b2a3ed0c8f0c64d1aaae2b1e0fbefe01f (diff)
downloadtimeline-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/internal-user-service/internal-user.service.ts')
-rw-r--r--Timeline/ClientApp/src/app/user/internal-user-service/internal-user.service.ts23
1 files changed, 16 insertions, 7 deletions
diff --git a/Timeline/ClientApp/src/app/user/internal-user-service/internal-user.service.ts b/Timeline/ClientApp/src/app/user/internal-user-service/internal-user.service.ts
index 2098391e..d82e9613 100644
--- a/Timeline/ClientApp/src/app/user/internal-user-service/internal-user.service.ts
+++ b/Timeline/ClientApp/src/app/user/internal-user-service/internal-user.service.ts
@@ -3,7 +3,7 @@ import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { Router } from '@angular/router';
import { Observable, throwError, BehaviorSubject, of } from 'rxjs';
-import { map, catchError, retry, switchMap, tap } from 'rxjs/operators';
+import { map, catchError, retry, switchMap, tap, filter } from 'rxjs/operators';
import { AlreadyLoginError, BadCredentialsError, BadNetworkError, UnknownError } from './errors';
import {
@@ -35,14 +35,13 @@ export const TOKEN_STORAGE_KEY = 'token';
export class InternalUserService {
private token: string | null = null;
- private userInfoSubject = new BehaviorSubject<UserInfo | null>(null);
+ private userInfoSubject = new BehaviorSubject<UserInfo | null | undefined>(undefined);
- get currentUserInfo(): UserInfo | null {
- return this.userInfoSubject.value;
- }
+ readonly userInfo$: Observable<UserInfo | null> =
+ <Observable<UserInfo | null>>this.userInfoSubject.pipe(filter(value => value !== undefined));
- get userInfo$(): Observable<UserInfo | null> {
- return this.userInfoSubject;
+ get currentUserInfo(): UserInfo | null | undefined {
+ return this.userInfoSubject.value;
}
private openSnackBar(snackBar: MatSnackBar, textKey: SnackBarTextKey) {
@@ -129,4 +128,14 @@ export class InternalUserService {
})
);
}
+
+ logout() {
+ if (this.currentUserInfo === null) {
+ throw new Error('No login now. You can\'t logout.');
+ }
+
+ this.window.localStorage.removeItem(TOKEN_STORAGE_KEY);
+ this.token = null;
+ this.userInfoSubject.next(null);
+ }
}