Skip to content

Commit c4a50b1

Browse files
authored
Merge pull request #535 from iyilm4z/ng-merge-login&auth-services
login.service merged into app-auth.service
2 parents 175f8be + f16128e commit c4a50b1

File tree

6 files changed

+117
-127
lines changed

6 files changed

+117
-127
lines changed

angular/src/account/account.module.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ import { AccountLanguagesComponent } from './layout/account-languages.component'
2121
import { AccountHeaderComponent } from './layout/account-header.component';
2222
import { AccountFooterComponent } from './layout/account-footer.component';
2323

24-
import { LoginService } from './login/login.service';
25-
2624
// tenants
2725
import { TenantChangeComponent } from './tenant/tenant-change.component';
2826
import { TenantChangeDialogComponent } from './tenant/tenant-change-dialog.component';
@@ -50,9 +48,6 @@ import { TenantChangeDialogComponent } from './tenant/tenant-change-dialog.compo
5048
TenantChangeComponent,
5149
TenantChangeDialogComponent,
5250
],
53-
providers: [
54-
LoginService
55-
],
5651
entryComponents: [
5752
// tenant
5853
TenantChangeDialogComponent

angular/src/account/login/login.component.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ <h4 class="text-center mb-3">{{ "LogIn" | localize }}</h4>
77
type="text"
88
class="form-control"
99
name="userNameOrEmailAddress"
10-
[(ngModel)]="loginService.authenticateModel.userNameOrEmailAddress"
10+
[(ngModel)]="authService.authenticateModel.userNameOrEmailAddress"
1111
[placeholder]="'UserNameOrEmail' | localize"
1212
required
1313
maxlength="256"
@@ -31,7 +31,7 @@ <h4 class="text-center mb-3">{{ "LogIn" | localize }}</h4>
3131
type="password"
3232
class="form-control"
3333
name="password"
34-
[(ngModel)]="loginService.authenticateModel.password"
34+
[(ngModel)]="authService.authenticateModel.password"
3535
[placeholder]="'Password' | localize"
3636
required
3737
maxlength="32"
@@ -57,7 +57,7 @@ <h4 class="text-center mb-3">{{ "LogIn" | localize }}</h4>
5757
class="custom-control-input"
5858
id="rememberMe"
5959
name="rememberMe"
60-
[(ngModel)]="loginService.rememberMe"
60+
[(ngModel)]="authService.rememberMe"
6161
/>
6262
<label for="rememberMe" class="custom-control-label">
6363
{{ "RememberMe" | localize }}

angular/src/account/login/login.component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Component, Injector } from '@angular/core';
22
import { AbpSessionService } from '@abp/session/abp-session.service';
33
import { AppComponentBase } from '@shared/app-component-base';
4-
import { LoginService } from './login.service';
54
import { accountModuleAnimation } from '@shared/animations/routerTransition';
5+
import { AppAuthService } from '@shared/auth/app-auth.service';
66

77
@Component({
88
templateUrl: './login.component.html',
@@ -13,7 +13,7 @@ export class LoginComponent extends AppComponentBase {
1313

1414
constructor(
1515
injector: Injector,
16-
public loginService: LoginService,
16+
public authService: AppAuthService,
1717
private _sessionService: AbpSessionService
1818
) {
1919
super(injector);
@@ -33,6 +33,6 @@ export class LoginComponent extends AppComponentBase {
3333

3434
login(): void {
3535
this.submitting = true;
36-
this.loginService.authenticate(() => (this.submitting = false));
36+
this.authService.authenticate(() => (this.submitting = false));
3737
}
3838
}

angular/src/account/login/login.service.ts

Lines changed: 0 additions & 100 deletions
This file was deleted.

angular/src/account/register/register.component.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
RegisterOutput
99
} from '@shared/service-proxies/service-proxies';
1010
import { accountModuleAnimation } from '@shared/animations/routerTransition';
11-
import { LoginService } from '../login/login.service';
11+
import { AppAuthService } from '@shared/auth/app-auth.service';
1212

1313
@Component({
1414
templateUrl: './register.component.html',
@@ -22,7 +22,7 @@ export class RegisterComponent extends AppComponentBase {
2222
injector: Injector,
2323
private _accountService: AccountServiceProxy,
2424
private _router: Router,
25-
private _loginService: LoginService
25+
private authService: AppAuthService
2626
) {
2727
super(injector);
2828
}
@@ -45,9 +45,9 @@ export class RegisterComponent extends AppComponentBase {
4545

4646
// Autheticate
4747
this.saving = true;
48-
this._loginService.authenticateModel.userNameOrEmailAddress = this.model.userName;
49-
this._loginService.authenticateModel.password = this.model.password;
50-
this._loginService.authenticate(() => {
48+
this.authService.authenticateModel.userNameOrEmailAddress = this.model.userName;
49+
this.authService.authenticateModel.password = this.model.password;
50+
this.authService.authenticate(() => {
5151
this.saving = false;
5252
});
5353
});
Lines changed: 106 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,113 @@
11
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';
27
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';
314

415
@Injectable()
516
export class AppAuthService {
6-
logout(reload?: boolean): void {
7-
abp.auth.clearToken();
8-
abp.utils.setCookieValue(
9-
AppConsts.authorization.encryptedAuthTokenName,
10-
undefined,
11-
undefined,
12-
abp.appPath
13-
);
14-
if (reload !== false) {
15-
location.href = AppConsts.appBaseUrl;
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+
logout(reload?: boolean): void {
32+
abp.auth.clearToken();
33+
abp.utils.setCookieValue(
34+
AppConsts.authorization.encryptedAuthTokenName,
35+
undefined,
36+
undefined,
37+
abp.appPath
38+
);
39+
if (reload !== false) {
40+
location.href = AppConsts.appBaseUrl;
41+
}
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;
16112
}
17-
}
18113
}

0 commit comments

Comments
 (0)