Skip to content

Instantly share code, notes, and snippets.

@Sampath-Lokuge
Created October 8, 2018 13:07
Show Gist options
  • Save Sampath-Lokuge/c3f7cde8bf127fcf3a28f5d73fa2757e to your computer and use it in GitHub Desktop.
Save Sampath-Lokuge/c3f7cde8bf127fcf3a28f5d73fa2757e to your computer and use it in GitHub Desktop.
login.component.ts
import { Component, OnInit, Output, Input, ViewChild, OnDestroy, ChangeDetectionStrategy, ChangeDetectorRef, HostBinding } from '@angular/core';
import { AuthenticationService } from '../../../../core/auth/authentication.service';
import { Router } from '@angular/router';
import { Subject } from 'rxjs';
import { AuthNoticeService } from '../../../../core/auth/auth-notice.service';
import { NgForm } from '@angular/forms';
import { TranslateService } from '@ngx-translate/core';
import { SpinnerButtonOptions } from '../../../partials/content/general/spinner-button/button-options.interface';
@Component({
selector: 'm-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class LoginComponent implements OnInit, OnDestroy {
model: any = {
username: '',
password: ''
};
@HostBinding('class') classes: string = 'm-login__signin';
@Output() actionChange = new Subject<string>();
public loading = false;
@Input() action: string;
@ViewChild('f') login: NgForm;
errors: any = [];
spinner: SpinnerButtonOptions = {
active: false,
spinnerSize: 18,
raised: true,
buttonColor: 'primary',
spinnerColor: 'accent',
fullWidth: false
};
constructor(
private authService: AuthenticationService,
private router: Router,
public authNoticeService: AuthNoticeService,
private translate: TranslateService,
private cdr: ChangeDetectorRef
) { }
ngOnInit(): void {
}
submit() {
this.spinner.active = true;
if (this.login.valid) {
this.authService.login(this.model).subscribe(response => {
if (typeof response !== 'undefined') {
this.router.navigate(['/']);
} else {
this.authNoticeService.setNotice(this.translate.instant('AUTH.VALIDATION.INVALID_LOGIN'), 'error');
}
this.spinner.active = false;
this.cdr.detectChanges();
});
} else {
this.spinner.active = false;
}
}
forgotPasswordPage(event: Event) {
this.action = 'forgot-password';
this.actionChange.next(this.action);
}
register(event: Event) {
this.action = 'register';
this.actionChange.next(this.action);
}
ngOnDestroy(): void {
this.authNoticeService.setNotice(null);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment