Created
October 13, 2018 02:43
-
-
Save zkkmin/3139a4edceab36af8147d1856352e5ee to your computer and use it in GitHub Desktop.
Login component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, OnInit } from '@angular/core'; | |
import {FormGroup, Validators, FormBuilder} from '@angular/forms'; | |
import { Router } from '@angular/router'; | |
import { AuthService } from '../auth.service'; | |
@Component({ | |
selector: 'app-login', | |
templateUrl: './login.component.html', | |
styleUrls: ['./login.component.css'] | |
}) | |
export class LoginComponent implements OnInit { | |
form: FormGroup; | |
error: string; | |
constructor(private fb: FormBuilder, | |
private authService: AuthService, | |
private router: Router | |
) { | |
this.form = fb.group({ | |
email: ['', Validators.required], | |
password: ['', Validators.required] | |
}); | |
} | |
login() { | |
const val = this.form.value; | |
if (val.email && val.password) { | |
this.authService.login(val.email, val.password) | |
.subscribe( | |
(result) => { | |
// Unsuccessful messages | |
this.error = result['error']; | |
// Successful case | |
if (this.authService.isLoggedIn()){ | |
let redirect = this.authService.redirectUrl ? | |
this.authService.redirectUrl : '/dashboard'; | |
this.router.navigate([redirect]) | |
} | |
} | |
) | |
} | |
} | |
ngOnInit() { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment