change to firebase auth
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { Component } from '@angular/core';
|
||||
import { FormsModule } from '@angular/forms';
|
||||
import { ActivatedRoute, Router } from '@angular/router';
|
||||
import { AuthService } from '../../services/auth.service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-login-register',
|
||||
standalone: true,
|
||||
imports: [CommonModule, FormsModule],
|
||||
templateUrl: './login-register.component.html',
|
||||
})
|
||||
export class LoginRegisterComponent {
|
||||
email: string = '';
|
||||
password: string = '';
|
||||
confirmPassword: string = '';
|
||||
isLoginMode: boolean = true; // true: Login, false: Registration
|
||||
errorMessage: string = '';
|
||||
|
||||
constructor(private authService: AuthService, private route: ActivatedRoute, private router: Router) {}
|
||||
|
||||
ngOnInit(): void {
|
||||
// Set mode based on query parameter "mode"
|
||||
this.route.queryParamMap.subscribe(params => {
|
||||
const mode = params.get('mode');
|
||||
this.isLoginMode = mode !== 'register';
|
||||
});
|
||||
}
|
||||
|
||||
toggleMode(): void {
|
||||
this.isLoginMode = !this.isLoginMode;
|
||||
this.errorMessage = '';
|
||||
}
|
||||
|
||||
// Login with Email
|
||||
onSubmit(): void {
|
||||
this.errorMessage = '';
|
||||
if (this.isLoginMode) {
|
||||
this.authService
|
||||
.loginWithEmail(this.email, this.password)
|
||||
.then(userCredential => {
|
||||
console.log('Successfully logged in:', userCredential);
|
||||
this.router.navigate([`home`]);
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error during email login:', error);
|
||||
this.errorMessage = error.message;
|
||||
});
|
||||
} else {
|
||||
// Registration mode: also check if passwords match
|
||||
if (this.password !== this.confirmPassword) {
|
||||
console.error('Passwords do not match');
|
||||
this.errorMessage = 'Passwords do not match.';
|
||||
return;
|
||||
}
|
||||
this.authService
|
||||
.registerWithEmail(this.email, this.password)
|
||||
.then(userCredential => {
|
||||
console.log('Successfully registered:', userCredential);
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error during registration:', error);
|
||||
if (error.code === 'auth/email-already-in-use') {
|
||||
this.errorMessage = 'This email address is already in use. Please try logging in.';
|
||||
} else {
|
||||
this.errorMessage = error.message;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Login with Google
|
||||
loginWithGoogle(): void {
|
||||
this.errorMessage = '';
|
||||
this.authService
|
||||
.loginWithGoogle()
|
||||
.then(userCredential => {
|
||||
console.log('Successfully logged in with Google:', userCredential);
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error during Google login:', error);
|
||||
this.errorMessage = error.message;
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user