Skip to content

Instantly share code, notes, and snippets.

@ShekMak
Created September 26, 2021 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShekMak/61606fb4c68d40675a040e8aa9f8f0fd to your computer and use it in GitHub Desktop.
Save ShekMak/61606fb4c68d40675a040e8aa9f8f0fd to your computer and use it in GitHub Desktop.
import { Component, OnInit } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { Router } from '@angular/router';
import { AuthService } from '../services/auth.service';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
loginForm: FormGroup = new FormGroup({
email: new FormControl('', [
Validators.required,
Validators.email,
Validators.pattern('^[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,4}$')
]),
password: new FormControl('', [
Validators.required,
Validators.minLength(6),
Validators.pattern(/[0-9a-zA-Z]{6,}/)
])
});;
constructor(private authService: AuthService,
private router: Router) { }
ngOnInit(): void {
}
loginWithEmailAndPassword(): void {
const email = this.loginForm.get('email')?.value;
const password = this.loginForm.get('password')?.value;
this.authService.signInWithEmailAndPassword(email, password).then(
(user) => {
this.router.navigate(['home']);
}
).catch(
(error) => {
alert('Authentification Problem');
// master error message with the link bellow
// https://firebase.google.com/docs/reference/js/v8/firebase.auth.Auth#signinwithemailandpassword
}
);
}
loginWithGoogle(): void {
this.authService.signInWithGoogle().then(
(user) => {
this.router.navigate(['home']);
}
).catch(
(error) => {
alert('Authentification Problem');
// master error message with the link bellow
// https://firebase.google.com/docs/reference/js/v8/firebase.auth.Auth#signinwithredirect
}
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment