Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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