From e8adc2bca91fd3c2a5bb85c2be8cbf74ded5f17b Mon Sep 17 00:00:00 2001 From: mehmet-erim Date: Thu, 9 Jan 2020 22:47:52 +0300 Subject: [PATCH] refactor(account): move login method to auth service --- .../lib/components/login/login.component.html | 2 +- .../lib/components/login/login.component.ts | 51 +++++++++---------- .../register/register.component.html | 8 ++- .../components/register/register.component.ts | 47 ++++++++--------- .../core/src/lib/services/auth.service.ts | 51 +++++++++++++++++++ .../packages/core/src/lib/services/index.ts | 1 + 6 files changed, 107 insertions(+), 53 deletions(-) create mode 100644 npm/ng-packs/packages/core/src/lib/services/auth.service.ts diff --git a/npm/ng-packs/packages/account/src/lib/components/login/login.component.html b/npm/ng-packs/packages/account/src/lib/components/login/login.component.html index c448e2a228..09e5d0314d 100644 --- a/npm/ng-packs/packages/account/src/lib/components/login/login.component.html +++ b/npm/ng-packs/packages/account/src/lib/components/login/login.component.html @@ -12,7 +12,7 @@

{{ 'AbpAccount::Login' | abpLocalization }}

- + {{ 'AbpAccount::AreYouANewUser' | abpLocalization }} {{ 'AbpAccount::Register' | abpLocalization diff --git a/npm/ng-packs/packages/account/src/lib/components/login/login.component.ts b/npm/ng-packs/packages/account/src/lib/components/login/login.component.ts index 2fedb0e8f5..21a6ac31a2 100644 --- a/npm/ng-packs/packages/account/src/lib/components/login/login.component.ts +++ b/npm/ng-packs/packages/account/src/lib/components/login/login.component.ts @@ -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)); + }); } } diff --git a/npm/ng-packs/packages/account/src/lib/components/register/register.component.html b/npm/ng-packs/packages/account/src/lib/components/register/register.component.html index 4a0f75d010..f803d473cd 100644 --- a/npm/ng-packs/packages/account/src/lib/components/register/register.component.html +++ b/npm/ng-packs/packages/account/src/lib/components/register/register.component.html @@ -16,7 +16,13 @@ 'AbpAccount::Login' | abpLocalization }} -
+
* = 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) || diff --git a/npm/ng-packs/packages/core/src/lib/services/auth.service.ts b/npm/ng-packs/packages/core/src/lib/services/auth.service.ts new file mode 100644 index 0000000000..0fc69f62ff --- /dev/null +++ b/npm/ng-packs/packages/core/src/lib/services/auth.service.ts @@ -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 { + 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), + ); + } +} diff --git a/npm/ng-packs/packages/core/src/lib/services/index.ts b/npm/ng-packs/packages/core/src/lib/services/index.ts index 6b90b997cc..e7f7f9b985 100644 --- a/npm/ng-packs/packages/core/src/lib/services/index.ts +++ b/npm/ng-packs/packages/core/src/lib/services/index.ts @@ -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';