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