Skip to content

Instantly share code, notes, and snippets.

@zkkmin
Created October 13, 2018 02:43
Show Gist options
  • Save zkkmin/3139a4edceab36af8147d1856352e5ee to your computer and use it in GitHub Desktop.
Save zkkmin/3139a4edceab36af8147d1856352e5ee to your computer and use it in GitHub Desktop.
Login component
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