refactor(account): move login method to auth service

pull/2610/head
mehmet-erim 6 years ago
parent 3b2ff3e6e8
commit e8adc2bca9

@ -12,7 +12,7 @@
</abp-auth-wrapper>
<ng-template #mainContentRef>
<h4>{{ 'AbpAccount::Login' | abpLocalization }}</h4>
<strong>
<strong *ngIf="isSelfRegistrationEnabled">
{{ 'AbpAccount::AreYouANewUser' | abpLocalization }}
<a class="text-decoration-none" routerLink="/account/register">{{
'AbpAccount::Register' | abpLocalization

@ -1,15 +1,12 @@
import { GetAppConfiguration, ConfigState, SessionState } from '@abp/ng.core';
import { Component, Inject, Optional } from '@angular/core';
import { AuthService, SetRemember, ConfigState } from '@abp/ng.core';
import { ToasterService } from '@abp/ng.theme.shared';
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { Navigate } from '@ngxs/router-plugin';
import { Store } from '@ngxs/store';
import { OAuthService } from 'angular-oauth2-oidc';
import { from, throwError } from 'rxjs';
import { Options } from '../../models/options';
import { ToasterService } from '@abp/ng.theme.shared';
import { catchError, finalize, switchMap, tap } from 'rxjs/operators';
import { throwError } from 'rxjs';
import { catchError, finalize } from 'rxjs/operators';
import snq from 'snq';
import { HttpHeaders } from '@angular/common/http';
const { maxLength, minLength, required } = Validators;
@ -17,20 +14,28 @@ const { maxLength, minLength, required } = Validators;
selector: 'abp-login',
templateUrl: './login.component.html',
})
export class LoginComponent {
export class LoginComponent implements OnInit {
form: FormGroup;
inProgress: boolean;
isSelfRegistrationEnabled = true;
constructor(
private fb: FormBuilder,
private oauthService: OAuthService,
private store: Store,
private toasterService: ToasterService,
@Optional() @Inject('ACCOUNT_OPTIONS') private options: Options,
) {
this.oauthService.configure(this.store.selectSnapshot(ConfigState.getOne('environment')).oAuthConfig);
this.oauthService.loadDiscoveryDocument();
private authService: AuthService,
) {}
ngOnInit() {
this.isSelfRegistrationEnabled =
(
(this.store.selectSnapshot(
ConfigState.getSetting('Abp.Account.IsSelfRegistrationEnabled'),
) as string) || ''
).toLowerCase() !== 'false';
this.form = this.fb.group({
username: ['', [required, maxLength(255)]],
@ -41,23 +46,11 @@ export class LoginComponent {
onSubmit() {
if (this.form.invalid) return;
// this.oauthService.setStorage(this.form.value.remember ? localStorage : sessionStorage);
this.inProgress = true;
const tenant = this.store.selectSnapshot(SessionState.getTenant);
from(
this.oauthService.fetchTokenUsingPasswordFlow(
this.form.get('username').value,
this.form.get('password').value,
new HttpHeaders({ ...(tenant && tenant.id && { __tenant: tenant.id }) }),
),
)
this.authService
.login(this.form.get('username').value, this.form.get('password').value)
.pipe(
switchMap(() => this.store.dispatch(new GetAppConfiguration())),
tap(() => {
const redirectUrl = snq(() => window.history.state).redirectUrl || (this.options || {}).redirectUrl || '/';
this.store.dispatch(new Navigate([redirectUrl]));
}),
catchError(err => {
this.toasterService.error(
snq(() => err.error.error_description) ||
@ -69,6 +62,8 @@ export class LoginComponent {
}),
finalize(() => (this.inProgress = false)),
)
.subscribe();
.subscribe(() => {
this.store.dispatch(new SetRemember(this.form.get('remember').value));
});
}
}

@ -16,7 +16,13 @@
'AbpAccount::Login' | abpLocalization
}}</a>
</strong>
<form [formGroup]="form" (ngSubmit)="onSubmit()" validateOnSubmit class="mt-4">
<form
*ngIf="isSelfRegistrationEnabled"
[formGroup]="form"
(ngSubmit)="onSubmit()"
validateOnSubmit
class="mt-4"
>
<div class="form-group">
<label for="input-user-name">{{ 'AbpAccount::UserName' | abpLocalization }}</label
><span> * </span

@ -1,4 +1,4 @@
import { ConfigState, GetAppConfiguration, ABP, SessionState } from '@abp/ng.core';
import { ConfigState, GetAppConfiguration, ABP, SessionState, AuthService } from '@abp/ng.core';
import { ToasterService } from '@abp/ng.theme.shared';
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
@ -23,20 +23,36 @@ export class RegisterComponent implements OnInit {
inProgress: boolean;
isSelfRegistrationEnabled = true;
constructor(
private fb: FormBuilder,
private accountService: AccountService,
private oauthService: OAuthService,
private store: Store,
private toasterService: ToasterService,
) {
this.oauthService.configure(
this.store.selectSnapshot(ConfigState.getOne('environment')).oAuthConfig,
);
this.oauthService.loadDiscoveryDocument();
}
private authService: AuthService,
) {}
ngOnInit() {
this.isSelfRegistrationEnabled =
(
this.store.selectSnapshot(
ConfigState.getSetting('Abp.Account.IsSelfRegistrationEnabled'),
) || ''
).toLowerCase() !== 'false';
if (!this.isSelfRegistrationEnabled) {
this.toasterService.warn(
{
key: 'AbpAccount::SelfRegistrationDisabledMessage',
defaultValue: 'Self registration is disabled.',
},
null,
{ life: 10000 },
);
return;
}
const passwordRules: ABP.Dictionary<string> = this.store.selectSnapshot(
ConfigState.getSettings('Identity.Password'),
);
@ -85,25 +101,10 @@ export class RegisterComponent implements OnInit {
appName: 'Angular',
} as RegisterRequest;
const tenant = this.store.selectSnapshot(SessionState.getTenant);
this.accountService
.register(newUser)
.pipe(
switchMap(() =>
from(
this.oauthService.fetchTokenUsingPasswordFlow(
newUser.userName,
newUser.password,
new HttpHeaders({
...(tenant && tenant.id && { __tenant: tenant.id }),
}),
),
),
),
switchMap(() => this.store.dispatch(new GetAppConfiguration())),
tap(() => this.store.dispatch(new Navigate(['/']))),
take(1),
switchMap(() => this.authService.login(newUser.userName, newUser.password)),
catchError(err => {
this.toasterService.error(
snq(() => err.error.error_description) ||

@ -0,0 +1,51 @@
import { HttpHeaders } from '@angular/common/http';
import { Inject, Injectable, Optional } from '@angular/core';
import { Navigate } from '@ngxs/router-plugin';
import { Store } from '@ngxs/store';
import { OAuthService } from 'angular-oauth2-oidc';
import { from, Observable } from 'rxjs';
import { switchMap, tap, take } from 'rxjs/operators';
import snq from 'snq';
import { GetAppConfiguration } from '../actions/config.actions';
import { SessionState } from '../states/session.state';
import { RestService } from './rest.service';
import { ConfigState } from '../states/config.state';
@Injectable({
providedIn: 'root',
})
export class AuthService {
constructor(
private rest: RestService,
private oAuthService: OAuthService,
private store: Store,
@Optional() @Inject('ACCOUNT_OPTIONS') private options: any,
) {}
login(username: string, password: string): Observable<any> {
const tenant = this.store.selectSnapshot(SessionState.getTenant);
this.oAuthService.configure(
this.store.selectSnapshot(ConfigState.getOne('environment')).oAuthConfig,
);
return from(this.oAuthService.loadDiscoveryDocument()).pipe(
switchMap(() =>
from(
this.oAuthService.fetchTokenUsingPasswordFlow(
username,
password,
new HttpHeaders({ ...(tenant && tenant.id && { __tenant: tenant.id }) }),
),
),
),
switchMap(() => this.store.dispatch(new GetAppConfiguration())),
tap(() => {
const redirectUrl =
snq(() => window.history.state.redirectUrl) || (this.options || {}).redirectUrl || '/';
this.store.dispatch(new Navigate([redirectUrl]));
}),
take(1),
);
}
}

@ -1,4 +1,5 @@
export * from './application-configuration.service';
export * from './auth.service';
export * from './config-state.service';
export * from './lazy-load.service';
export * from './localization.service';

Loading…
Cancel
Save