Browse Source

refactor(account): login component

pull/1562/head
mehmet-erim 7 years ago
parent
commit
de58b9882c
  1. 2
      npm/ng-packs/packages/account/src/lib/components/login/login.component.html
  2. 34
      npm/ng-packs/packages/account/src/lib/components/login/login.component.ts

2
npm/ng-packs/packages/account/src/lib/components/login/login.component.html

@ -30,7 +30,7 @@
<button type="button" name="Action" value="Cancel" class="btn btn-secondary"> <button type="button" name="Action" value="Cancel" class="btn btn-secondary">
{{ 'AbpAccount::Cancel' | abpLocalization }} {{ 'AbpAccount::Cancel' | abpLocalization }}
</button> </button>
<button type="submit" name="Action" value="Login" class="btn btn-primary ml-1"> <button [disabled]="inProgress" type="submit" name="Action" value="Login" class="btn btn-primary ml-1">
{{ 'AbpAccount::Login' | abpLocalization }} {{ 'AbpAccount::Login' | abpLocalization }}
</button> </button>
</div> </div>

34
npm/ng-packs/packages/account/src/lib/components/login/login.component.ts

@ -4,8 +4,11 @@ import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { Navigate } from '@ngxs/router-plugin'; import { Navigate } from '@ngxs/router-plugin';
import { Store } from '@ngxs/store'; import { Store } from '@ngxs/store';
import { OAuthService } from 'angular-oauth2-oidc'; import { OAuthService } from 'angular-oauth2-oidc';
import { from } from 'rxjs'; import { from, throwError } from 'rxjs';
import { Options } from '../../models/options'; import { Options } from '../../models/options';
import { ToasterService } from '@abp/ng.theme.shared';
import { catchError, finalize, switchMap, tap } from 'rxjs/operators';
import snq from 'snq';
const { maxLength, minLength, required } = Validators; const { maxLength, minLength, required } = Validators;
@ -16,10 +19,13 @@ const { maxLength, minLength, required } = Validators;
export class LoginComponent { export class LoginComponent {
form: FormGroup; form: FormGroup;
inProgress: boolean;
constructor( constructor(
private fb: FormBuilder, private fb: FormBuilder,
private oauthService: OAuthService, private oauthService: OAuthService,
private store: Store, private store: Store,
private toasterService: ToasterService,
@Optional() @Inject('ACCOUNT_OPTIONS') private options: Options, @Optional() @Inject('ACCOUNT_OPTIONS') private options: Options,
) { ) {
this.oauthService.configure(this.store.selectSnapshot(ConfigState.getOne('environment')).oAuthConfig); this.oauthService.configure(this.store.selectSnapshot(ConfigState.getOne('environment')).oAuthConfig);
@ -34,20 +40,24 @@ export class LoginComponent {
onSubmit() { onSubmit() {
if (this.form.invalid) return; if (this.form.invalid) return;
this.oauthService.setStorage(this.form.value.remember ? localStorage : sessionStorage); this.oauthService.setStorage(this.form.value.remember ? localStorage : sessionStorage);
this.inProgress = true;
from( from(
this.oauthService.fetchTokenUsingPasswordFlow(this.form.get('username').value, this.form.get('password').value), this.oauthService.fetchTokenUsingPasswordFlow(this.form.get('username').value, this.form.get('password').value),
).subscribe({ )
next: () => { .pipe(
const redirectUrl = window.history.state.redirectUrl || this.options.redirectUrl; switchMap(() => this.store.dispatch(new ConfigGetAppConfiguration())),
tap(() => {
this.store const redirectUrl = (window.history.state || {}).redirectUrl || (this.options || {}).redirectUrl;
.dispatch(new ConfigGetAppConfiguration()) this.store.dispatch(new Navigate([redirectUrl || '/']));
.subscribe(() => this.store.dispatch(new Navigate([redirectUrl || '/']))); }),
}, catchError(err => {
error: () => console.error('an error occured'), this.toasterService.error(snq(() => err.error.error_description, 'An error occured.'), 'Error');
}); return throwError(err);
}),
finalize(() => (this.inProgress = false)),
)
.subscribe();
} }
} }

Loading…
Cancel
Save