Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Component, OnInit } from '@angular/core';
- import { FormGroup, FormControl } from '@angular/forms';
- import { Router } from '@angular/router';
- import { OidcService } from '../services/oidc.service';
- import { TokenModel } from '../models/token.model';
- import { CanActivateGuard } from '../services/can-activate.service';
- @Component({
- selector: 'app-login',
- templateUrl: './login.component.html',
- styleUrls: ['./login.component.scss']
- })
- export class LoginComponent implements OnInit {
- public loginFormGroup: FormGroup;
- public isLoading: boolean;
- public loginError: boolean;
- public isLoggedIn: boolean;
- constructor(
- public oidc: OidcService,
- public router: Router,
- public activateGuard: CanActivateGuard
- ) {
- this.loginError = false;
- if (this.checkLoginStatus()) {
- this.router.navigate(['/crm/']);
- }
- }
- public checkLoginStatus() {
- if (localStorage.access_token) {
- return true;
- } else {
- return false;
- }
- }
- public submitLoginForm(loginFormGroup: FormGroup) {
- if (!loginFormGroup) { return; }
- this.isLoading = true;
- this.oidc.postForKey(loginFormGroup.value.username, loginFormGroup.value.password)
- .subscribe((tokenObject: TokenModel) => {
- this.activateGuard.setAllowance(false);
- localStorage.setItem('access_token', tokenObject.access_token);
- location.reload();
- this.router.navigate(['/crm/']);
- }, error => {
- this.loginError = true;
- this.isLoading = false;
- });
- }
- ngOnInit() {
- this.loginFormGroup = new FormGroup({
- username: new FormControl(''),
- password: new FormControl('')
- });
- }
- }
Add Comment
Please, Sign In to add comment