This repository has been archived by the owner on Apr 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change-Id: I373b412c2e710c104a5fad8b284d7a55759d3083
- Loading branch information
Showing
6 changed files
with
132 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { | ||
HttpHandler, | ||
HttpRequest, | ||
HttpInterceptor, | ||
HttpErrorResponse, | ||
HttpEvent, | ||
} from '@angular/common/http'; | ||
import { Injectable } from '@angular/core'; | ||
import { Observable, of, throwError } from 'rxjs'; | ||
import { catchError, take, first, switchMap, map } from 'rxjs/operators'; | ||
|
||
import { LoginService } from './login.service'; | ||
import { AuthService } from './auth.service'; | ||
|
||
@Injectable() | ||
export class AuthInterceptor implements HttpInterceptor { | ||
constructor(private authService: AuthService) {} | ||
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { | ||
return this.authService.token$.pipe( | ||
first(), | ||
map(token => (!token ? req : req.clone({ setHeaders: { 'Access-Token': token } }))), | ||
switchMap(authReq => next.handle(authReq)), | ||
catchError((err: HttpErrorResponse) => { | ||
if (err.status === 401) { | ||
this.authService.authorized(); | ||
} | ||
throw err; | ||
}), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,46 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { BehaviorSubject } from 'rxjs'; | ||
import { Subject, BehaviorSubject } from 'rxjs'; | ||
import * as JwtDecode from 'jwt-decode'; | ||
import { map, skip } from 'rxjs/operators'; | ||
import { map, first, distinctUntilChanged } from 'rxjs/operators'; | ||
|
||
import { DstoreObject } from 'app/modules/client/utils/dstore-objects'; | ||
import { LoginService } from './login.service'; | ||
import { BaseService } from '../dstore/services/base.service'; | ||
import { environment } from 'environments/environment'; | ||
|
||
export interface UserInfo { | ||
username: string; | ||
userID: number; | ||
} | ||
console.log(environment); | ||
@Injectable() | ||
export class AuthService { | ||
constructor(private loginService: LoginService) { | ||
this.token$.subscribe(token => { | ||
if (token) { | ||
this.loginService.SetLoginStatue(true); | ||
localStorage.setItem(this.tokenStorageKey, token); | ||
} else { | ||
this.loginService.SetLoginStatue(false); | ||
localStorage.removeItem(this.tokenStorageKey); | ||
} | ||
}); | ||
} | ||
constructor() {} | ||
|
||
private tokenStorageKey = 'auth-token:' + BaseService.domainName; | ||
private tokenSubject = new BehaviorSubject<string>(localStorage.getItem(this.tokenStorageKey)); | ||
private loginSubject = new Subject<boolean>(); | ||
|
||
token$ = this.tokenSubject.asObservable(); | ||
logged$ = this.token$.pipe(map(token => token !== null)); | ||
info$ = this.token$.pipe( | ||
map(token => { | ||
if (token) { | ||
return JwtDecode(token) as UserInfo; | ||
} else { | ||
return null; | ||
} | ||
}), | ||
); | ||
|
||
token$ = this.tokenSubject.pipe(distinctUntilChanged()); | ||
logged$ = this.token$.pipe(map(Boolean)); | ||
info$ = this.token$.pipe(map(token => (token ? JwtDecode<UserInfo>(token) : null))); | ||
auth$ = this.loginSubject.asObservable(); | ||
// 登录方法 | ||
login(token: string) { | ||
localStorage.setItem(this.tokenStorageKey, token); | ||
this.tokenSubject.next(token); | ||
} | ||
|
||
// 登出方法 | ||
logout() { | ||
localStorage.removeItem(this.tokenStorageKey); | ||
this.tokenSubject.next(null); | ||
} | ||
|
||
// 需要验证事件 | ||
authorized() { | ||
this.logged$.pipe(first()).subscribe(logged => this.loginSubject.next(logged)); | ||
} | ||
// 打开注册页面 | ||
register() { | ||
DstoreObject.openURL(`https://account.deepin.org/register`); | ||
} | ||
} | ||
|
||
export interface UserInfo { | ||
username: string; | ||
userID: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { HttpClient } from '@angular/common/http'; | ||
import { environment } from 'environments/environment'; | ||
import { DeepinInfo } from 'app/dstore/services/deepin-info.model'; | ||
import { UserInfo } from './auth.service'; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class DeepinUserInfoService { | ||
private apiURL = environment.metadataServer + '/api/deepin_user'; | ||
constructor(private http: HttpClient) {} | ||
|
||
getDeepinUserInfo(uid: number): Promise<DeepinInfo>; | ||
getDeepinUserInfo(uidList: Array<number>): Promise<Array<DeepinInfo>>; | ||
|
||
getDeepinUserInfo(param: number | Array<number>) { | ||
if (typeof param === 'number') { | ||
return this.http | ||
.get<DeepinInfo[]>(this.apiURL, { params: { uid: String(param) } }) | ||
.toPromise() | ||
.then(users => users.find(user => user.uid === param)); | ||
} else if (param instanceof Array) { | ||
return this.http | ||
.get<DeepinInfo[]>(this.apiURL, { params: { uid: param.map(String) } }) | ||
.toPromise() | ||
.then(users => users); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters