|
| 1 | +import { Injectable } from '@angular/core'; |
| 2 | +import { Router } from '@angular/router'; |
1 | 3 | import { TokenService } from '@abp/auth/token.service';
|
2 | 4 | import { LogService } from '@abp/log/log.service';
|
3 |
| -import { MessageService } from '@abp/message/message.service'; |
4 | 5 | import { UtilsService } from '@abp/utils/utils.service';
|
5 |
| -import { Injectable } from '@angular/core'; |
6 |
| -import { Router } from '@angular/router'; |
7 | 6 | import { AppConsts } from '@shared/AppConsts';
|
8 | 7 | import { UrlHelper } from '@shared/helpers/UrlHelper';
|
9 |
| -import { AuthenticateModel, AuthenticateResultModel, TokenAuthServiceProxy } from '@shared/service-proxies/service-proxies'; |
| 8 | +import { |
| 9 | + AuthenticateModel, |
| 10 | + AuthenticateResultModel, |
| 11 | + TokenAuthServiceProxy, |
| 12 | +} from '@shared/service-proxies/service-proxies'; |
10 | 13 | import { finalize } from 'rxjs/operators';
|
11 | 14 |
|
12 |
| - |
13 | 15 | @Injectable()
|
14 | 16 | export class LoginService {
|
15 |
| - |
16 |
| - static readonly twoFactorRememberClientTokenName = 'TwoFactorRememberClientToken'; |
17 |
| - |
18 |
| - authenticateModel: AuthenticateModel; |
19 |
| - authenticateResult: AuthenticateResultModel; |
20 |
| - |
21 |
| - rememberMe: boolean; |
22 |
| - |
23 |
| - constructor( |
24 |
| - private _tokenAuthService: TokenAuthServiceProxy, |
25 |
| - private _router: Router, |
26 |
| - private _utilsService: UtilsService, |
27 |
| - private _messageService: MessageService, |
28 |
| - private _tokenService: TokenService, |
29 |
| - private _logService: LogService |
30 |
| - ) { |
31 |
| - this.clear(); |
| 17 | + authenticateModel: AuthenticateModel; |
| 18 | + authenticateResult: AuthenticateResultModel; |
| 19 | + rememberMe: boolean; |
| 20 | + |
| 21 | + constructor( |
| 22 | + private _tokenAuthService: TokenAuthServiceProxy, |
| 23 | + private _router: Router, |
| 24 | + private _utilsService: UtilsService, |
| 25 | + private _tokenService: TokenService, |
| 26 | + private _logService: LogService |
| 27 | + ) { |
| 28 | + this.clear(); |
| 29 | + } |
| 30 | + |
| 31 | + authenticate(finallyCallback?: () => void): void { |
| 32 | + finallyCallback = finallyCallback || (() => {}); |
| 33 | + |
| 34 | + this._tokenAuthService |
| 35 | + .authenticate(this.authenticateModel) |
| 36 | + .pipe( |
| 37 | + finalize(() => { |
| 38 | + finallyCallback(); |
| 39 | + }) |
| 40 | + ) |
| 41 | + .subscribe((result: AuthenticateResultModel) => { |
| 42 | + this.processAuthenticateResult(result); |
| 43 | + }); |
| 44 | + } |
| 45 | + |
| 46 | + private processAuthenticateResult( |
| 47 | + authenticateResult: AuthenticateResultModel |
| 48 | + ) { |
| 49 | + this.authenticateResult = authenticateResult; |
| 50 | + |
| 51 | + if (authenticateResult.accessToken) { |
| 52 | + // Successfully logged in |
| 53 | + this.login( |
| 54 | + authenticateResult.accessToken, |
| 55 | + authenticateResult.encryptedAccessToken, |
| 56 | + authenticateResult.expireInSeconds, |
| 57 | + this.rememberMe |
| 58 | + ); |
| 59 | + } else { |
| 60 | + // Unexpected result! |
| 61 | + |
| 62 | + this._logService.warn('Unexpected authenticateResult!'); |
| 63 | + this._router.navigate(['account/login']); |
32 | 64 | }
|
33 |
| - |
34 |
| - authenticate(finallyCallback?: () => void): void { |
35 |
| - finallyCallback = finallyCallback || (() => { }); |
36 |
| - |
37 |
| - this._tokenAuthService |
38 |
| - .authenticate(this.authenticateModel) |
39 |
| - .pipe(finalize(() => { finallyCallback(); })) |
40 |
| - .subscribe((result: AuthenticateResultModel) => { |
41 |
| - this.processAuthenticateResult(result); |
42 |
| - }); |
| 65 | + } |
| 66 | + |
| 67 | + private login( |
| 68 | + accessToken: string, |
| 69 | + encryptedAccessToken: string, |
| 70 | + expireInSeconds: number, |
| 71 | + rememberMe?: boolean |
| 72 | + ): void { |
| 73 | + const tokenExpireDate = rememberMe |
| 74 | + ? new Date(new Date().getTime() + 1000 * expireInSeconds) |
| 75 | + : undefined; |
| 76 | + |
| 77 | + this._tokenService.setToken(accessToken, tokenExpireDate); |
| 78 | + |
| 79 | + this._utilsService.setCookieValue( |
| 80 | + AppConsts.authorization.encryptedAuthTokenName, |
| 81 | + encryptedAccessToken, |
| 82 | + tokenExpireDate, |
| 83 | + abp.appPath |
| 84 | + ); |
| 85 | + |
| 86 | + let initialUrl = UrlHelper.initialUrl; |
| 87 | + if (initialUrl.indexOf('/login') > 0) { |
| 88 | + initialUrl = AppConsts.appBaseUrl; |
43 | 89 | }
|
44 | 90 |
|
45 |
| - private processAuthenticateResult(authenticateResult: AuthenticateResultModel) { |
46 |
| - this.authenticateResult = authenticateResult; |
47 |
| - |
48 |
| - if (authenticateResult.accessToken) { |
49 |
| - // Successfully logged in |
50 |
| - this.login( |
51 |
| - authenticateResult.accessToken, |
52 |
| - authenticateResult.encryptedAccessToken, |
53 |
| - authenticateResult.expireInSeconds, |
54 |
| - this.rememberMe); |
| 91 | + location.href = initialUrl; |
| 92 | + } |
55 | 93 |
|
56 |
| - } else { |
57 |
| - // Unexpected result! |
58 |
| - |
59 |
| - this._logService.warn('Unexpected authenticateResult!'); |
60 |
| - this._router.navigate(['account/login']); |
61 |
| - } |
62 |
| - } |
63 |
| - |
64 |
| - private login(accessToken: string, encryptedAccessToken: string, expireInSeconds: number, rememberMe?: boolean): void { |
65 |
| - |
66 |
| - const tokenExpireDate = rememberMe ? (new Date(new Date().getTime() + 1000 * expireInSeconds)) : undefined; |
67 |
| - |
68 |
| - this._tokenService.setToken( |
69 |
| - accessToken, |
70 |
| - tokenExpireDate |
71 |
| - ); |
72 |
| - |
73 |
| - this._utilsService.setCookieValue( |
74 |
| - AppConsts.authorization.encryptedAuthTokenName, |
75 |
| - encryptedAccessToken, |
76 |
| - tokenExpireDate, |
77 |
| - abp.appPath |
78 |
| - ); |
79 |
| - |
80 |
| - let initialUrl = UrlHelper.initialUrl; |
81 |
| - if (initialUrl.indexOf('/login') > 0) { |
82 |
| - initialUrl = AppConsts.appBaseUrl; |
83 |
| - } |
84 |
| - |
85 |
| - location.href = initialUrl; |
86 |
| - } |
87 |
| - |
88 |
| - private clear(): void { |
89 |
| - this.authenticateModel = new AuthenticateModel(); |
90 |
| - this.authenticateModel.rememberClient = false; |
91 |
| - this.authenticateResult = null; |
92 |
| - this.rememberMe = false; |
93 |
| - } |
| 94 | + private clear(): void { |
| 95 | + this.authenticateModel = new AuthenticateModel(); |
| 96 | + this.authenticateModel.rememberClient = false; |
| 97 | + this.authenticateResult = null; |
| 98 | + this.rememberMe = false; |
| 99 | + } |
94 | 100 | }
|
0 commit comments