angular/projects/researchdatabox/local-auth/src/app/local-auth.component.ts
Local Authentication Component
Author: Shilo Banihit
selector | local-auth |
templateUrl | ./local-auth.component.html |
Properties |
|
Methods |
|
constructor(loggerService: LoggerService, userService: UserService, utilService: UtilityService, fb: FormBuilder, document: Document, translationService: TranslationService)
|
|||||||||||||||||||||
Parameters :
|
Private getErrors |
getErrors()
|
Returns :
void
|
Protected Async initComponent |
initComponent()
|
Inherited from
BaseComponent
|
Defined in
BaseComponent:56
|
Returns :
Promise<void>
|
Async onLogin | ||||||
onLogin(event: any)
|
||||||
Parameters :
Returns :
any
|
getInitSubject |
getInitSubject()
|
Inherited from
BaseComponent
|
Defined in
BaseComponent:71
|
For those interested in the init from RXJS-land. Note that it returns a BehaviorSubject instance, and will have an initial value of false, so process the return value as needed.
Returns :
Subject<any>
|
isInitializing |
isInitializing()
|
Inherited from
BaseComponent
|
Defined in
BaseComponent:95
|
Main flag to indicate the init status
Returns :
boolean
|
ngOnInit |
ngOnInit()
|
Inherited from
BaseComponent
|
Defined in
BaseComponent:50
|
Returns :
void
|
Async waitForDeps |
waitForDeps()
|
Inherited from
BaseComponent
|
Defined in
BaseComponent:78
|
Wait for dependencies to initialise
Returns :
Promise<any>
|
Async waitForInit |
waitForInit()
|
Inherited from
BaseComponent
|
Defined in
BaseComponent:89
|
For those interested in the init from the Promise-land
Returns :
Promise<any>
|
form |
Type : FormGroup
|
Default value : null as any
|
isLoginDisabled |
Type : boolean
|
Default value : false
|
loginMessage |
Type : string
|
Default value : null as any
|
loginResult |
Type : UserLoginResult
|
Default value : null as any
|
window |
Type : any
|
Protected brandingAndPortalUrl |
Type : string
|
Default value : ''
|
Inherited from
BaseComponent
|
Defined in
BaseComponent:42
|
Private filterFn |
Default value : function(initStat: boolean) { return initStat; }
|
Inherited from
BaseComponent
|
Defined in
BaseComponent:40
|
Protected initDependencies |
Type : Initable[]
|
Default value : []
|
Inherited from
BaseComponent
|
Defined in
BaseComponent:39
|
Protected initSubject |
Type : BehaviorSubject<any>
|
Default value : new BehaviorSubject(false)
|
Inherited from
BaseComponent
|
Defined in
BaseComponent:38
|
Protected isReady |
Type : boolean
|
Default value : false
|
Inherited from
BaseComponent
|
Defined in
BaseComponent:37
|
import { Component, Inject } from '@angular/core';
import { FormGroup, Validators, FormBuilder } from '@angular/forms';
import { DOCUMENT } from "@angular/common"
import { BaseComponent, UserService, UserLoginResult, UtilityService, LoggerService, TranslationService } from '@researchdatabox/portal-ng-common';
/**
* Local Authentication Component
*
* Author: <a href='https://github.com/shilob' target='_blank'>Shilo Banihit</a>
*
*/
@Component({
selector: 'local-auth',
templateUrl: './local-auth.component.html'
})
export class LocalAuthComponent extends BaseComponent {
form: FormGroup = null as any;
loginMessage: string = null as any;
isLoginDisabled: boolean = false;
loginResult: UserLoginResult = null as any;
window: any;
constructor(
@Inject(LoggerService) private loggerService: LoggerService,
@Inject(UserService) private userService: UserService,
@Inject(UtilityService) protected utilService: UtilityService,
@Inject(FormBuilder) private fb: FormBuilder,
@Inject(DOCUMENT) private document: Document,
@Inject(TranslationService) private translationService: TranslationService
) {
super();
this.loggerService.debug(`LocalAuth waiting for deps to init...`);
this.window = this.document.defaultView;
// set this component's dependencies
this.initDependencies = [translationService, userService];
}
protected override async initComponent():Promise<void> {
this.form = this.fb.group({
"username": ["", Validators.required],
"password":["", Validators.required]
});
this.form.valueChanges.subscribe((data:any) => {
this.isLoginDisabled = this.form.status == 'INVALID';
if (this.isLoginDisabled) {
this.getErrors();
}
});
this.loggerService.debug(`LocalAuth initialised.`);
}
async onLogin(event:any) {
if (this.isLoginDisabled || this.form.status == 'INVALID') {
this.getErrors();
return;
}
event.preventDefault();
this.isLoginDisabled = true;
this.loginResult = await this.userService.loginLocal(this.form.value.username, this.form.value.password);
this.loggerService.debug(`LocalAuth, login result: `, this.loginResult);
if (this.loginResult.user) {
this.loggerService.debug(`LocalAuth, login success, redirecting...${this.loginResult.url}`);
this.window.location.href = this.loginResult.url;
} else {
this.loginMessage = this.loginResult.message;
this.isLoginDisabled = false;
}
}
private getErrors():void {
if (this.form.controls['username'].hasError('required')) {
this.loginMessage = "Please provide a username.";
} else {
this.loginMessage = "Please provide a password.";
}
}
}
<!-- ngIf is needed so init completes and view has all the information it needs -->
<ng-container *ngIf="isReady">
<div class="col-md-offset-5 col-md-3" >
<div class="panel panel-default">
<div class="panel-heading">
<span class="panel-title">{{ 'local-creds-header' | i18next }}</span>
</div>
<div class="panel-body">
<div class="form-login">
<form [formGroup]="form" (ngSubmit)='onLogin($event)'>
<input type="text" id="username" class="form-control input-sm chat-input" [placeholder]="'local-creds-input-username-label' | i18next" formControlName="username" [attr.aria-label]="'local-creds-input-username-label' | i18next " autocomplete="off"/>
<br/>
<input type="password" id="password" class="form-control input-sm chat-input" [placeholder]="'local-creds-input-password-label' | i18next" formControlName="password" [attr.aria-label]="'local-creds-input-password-label' | i18next " autocomplete="off"/>
<br/>
<div class="wrapper">
<span class="group-btn">
<button type='submit' class="btn btn-primary btn-md {{isLoginDisabled ? 'disabled' : ''}}" (click)="onLogin($event)">{{ 'local-auth-login' | i18next }} <i class="fa fa-sign-in"></i></button>
</span>
</div>
<br/>
<div class="alert alert-danger" *ngIf="loginMessage != null">
{{loginMessage}}
</div>
</form>
</div>
</div>
</div>
</div>
</ng-container>