Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Component, ViewEncapsulation } from '@angular/core';
- import { FormGroup, AbstractControl, FormBuilder, Validators } from '@angular/forms';
- import { Router } from '@angular/router';
- import { AuthService } from '../../auth.service';
- @Component({
- selector: 'login',
- encapsulation: ViewEncapsulation.None,
- styles: [require('./login.scss')],
- template: require('./login.html')
- })
- export class Login {
- public form:FormGroup;
- public username:AbstractControl;
- public password:AbstractControl;
- public submitted:boolean = false;
- private isRequesting:boolean = false;
- error;
- constructor(fb:FormBuilder,private router: Router, public authService:AuthService) {
- this.form = fb.group({
- 'username': ['', Validators.compose([Validators.required, Validators.minLength(1)])],
- 'password': ['', Validators.compose([Validators.required, Validators.minLength(1)])]
- });
- this.username = this.form.controls['username'];
- this.password = this.form.controls['password'];
- this.username.valueChanges.subscribe(value=>{
- this.error = null;
- })
- this.password.valueChanges.subscribe(value=>{
- this.error = null;
- })
- }
- ngOnInit(){}
- public onSubmit(values:Object):void {
- this.submitted = true;
- this.isRequesting = true;
- if (this.form.valid) {
- values['grant_type'] ='password';
- this.authService.doLogin(values).then(response=>{
- let loginData = response.json();
- this.authService.setLoggedIn(`${loginData.token_type} ${loginData.access_token}`);
- this.authService.setRefreshToken(`${loginData.refresh_token}`);
- this.authService.setUserId(`${loginData.user_id}`);
- localStorage.setItem('logindata',`${loginData.token_type} ${loginData.access_token}`);
- localStorage.setItem('user_id',`${loginData.user_id}`);
- localStorage.setItem('refresh_token',`${loginData.refresh_token}`);
- this.router.navigate(['/pages']);
- }).catch(err=>{
- this.isRequesting = false;
- if(err.status === 0 && err.statusText === "") {
- this.error = err;
- this.error.Message = "There is no Internet connection or Server is unreachable";
- }else{
- this.error = err.json();
- }
- })
- }
- }
- }
Add Comment
Please, Sign In to add comment