update firebase login
This commit is contained in:
parent
1274615ee2
commit
f0712fa0c3
7 changed files with 74 additions and 12 deletions
|
|
@ -64,13 +64,15 @@
|
||||||
mail.invalid ||
|
mail.invalid ||
|
||||||
!loginData.mail ||
|
!loginData.mail ||
|
||||||
password.invalid ||
|
password.invalid ||
|
||||||
!loginData.password
|
!loginData.password ||
|
||||||
|
sharedService.isBtnDisabled
|
||||||
"
|
"
|
||||||
></app-form-btn>
|
></app-form-btn>
|
||||||
<app-form-btn
|
<app-form-btn
|
||||||
[class]="'btn-guest-login'"
|
[class]="'btn-guest-login'"
|
||||||
[type]="'button'"
|
[type]="'button'"
|
||||||
[value]="'Guest Log in'"
|
[value]="'Guest Log in'"
|
||||||
|
[disabled]="sharedService.isBtnDisabled"
|
||||||
(click)="guestLogin()"
|
(click)="guestLogin()"
|
||||||
></app-form-btn>
|
></app-form-btn>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,8 @@ import { FormsModule, NgForm } from '@angular/forms';
|
||||||
import { FormBtnComponent } from '../../shared/components/buttons/form-btn/form-btn.component';
|
import { FormBtnComponent } from '../../shared/components/buttons/form-btn/form-btn.component';
|
||||||
import { FirebaseService } from '../../services/firebase.service';
|
import { FirebaseService } from '../../services/firebase.service';
|
||||||
import { Router } from '@angular/router';
|
import { Router } from '@angular/router';
|
||||||
|
import { LoginService } from '../../services/login.service';
|
||||||
|
import { SharedService } from '../../services/shared.service';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-login',
|
selector: 'app-login',
|
||||||
|
|
@ -20,20 +22,22 @@ export class LoginComponent {
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private firebaseService: FirebaseService,
|
private firebaseService: FirebaseService,
|
||||||
|
private loginSerivce: LoginService,
|
||||||
|
public sharedService: SharedService,
|
||||||
private router: Router
|
private router: Router
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
onSubmit(ngForm: NgForm) {
|
onSubmit(ngForm: NgForm) {
|
||||||
|
this.sharedService.isBtnDisabled = true;
|
||||||
if (ngForm.submitted && ngForm.form.valid) {
|
if (ngForm.submitted && ngForm.form.valid) {
|
||||||
|
this.loginSerivce.login(this.loginData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
guestLogin() {
|
guestLogin() {
|
||||||
this.getUserIdInLocalStorage();
|
this.sharedService.isBtnDisabled = true;
|
||||||
window.location.reload();
|
this.loginData.mail = 'guest@guestaccount.com';
|
||||||
}
|
this.loginData.password = 'guest@guestaccount.com';
|
||||||
|
this.onSubmit({ submitted: true, form: { valid: true } } as NgForm);
|
||||||
getUserIdInLocalStorage() {
|
|
||||||
localStorage.setItem('currentUser', JSON.stringify('5EX7gnwPPGEDbN186Rdw'));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
export interface User {
|
export interface User {
|
||||||
id: string;
|
id: string;
|
||||||
|
uId: string;
|
||||||
firstName: string;
|
firstName: string;
|
||||||
lastName: string;
|
lastName: string;
|
||||||
email: string;
|
email: string;
|
||||||
|
|
|
||||||
|
|
@ -110,6 +110,10 @@ export class FirebaseService implements OnDestroy {
|
||||||
return this.getAllUsers().filter((user) => user.initials !== 'G');
|
return this.getAllUsers().filter((user) => user.initials !== 'G');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
checkUserUID(userUid: string): User[] {
|
||||||
|
return this.getAllUsers().filter((user) => user.uId === userUid);
|
||||||
|
}
|
||||||
|
|
||||||
getCurrentUserId() {
|
getCurrentUserId() {
|
||||||
let currentUser = localStorage.getItem('currentUser');
|
let currentUser = localStorage.getItem('currentUser');
|
||||||
if (currentUser !== null) {
|
if (currentUser !== null) {
|
||||||
|
|
|
||||||
43
src/app/services/login.service.ts
Normal file
43
src/app/services/login.service.ts
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
import { Injectable, inject } from '@angular/core';
|
||||||
|
import { getAuth, signInWithEmailAndPassword } from 'firebase/auth';
|
||||||
|
import { FirebaseService } from './firebase.service';
|
||||||
|
import { Firestore, collection, query, where } from '@angular/fire/firestore';
|
||||||
|
import { SharedService } from './shared.service';
|
||||||
|
@Injectable({
|
||||||
|
providedIn: 'root',
|
||||||
|
})
|
||||||
|
export class LoginService {
|
||||||
|
firestore: Firestore = inject(Firestore);
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
private firebaseService: FirebaseService,
|
||||||
|
public sharedService: SharedService
|
||||||
|
) {}
|
||||||
|
|
||||||
|
login(loginData: { mail: string; password: string }) {
|
||||||
|
const auth = getAuth();
|
||||||
|
|
||||||
|
signInWithEmailAndPassword(auth, loginData.mail, loginData.password)
|
||||||
|
.then((userCredential) => {
|
||||||
|
const user = userCredential.user;
|
||||||
|
if (this.firebaseService.checkUserUID(user.uid).length > 0) {
|
||||||
|
this.getUserIdInLocalStorage(
|
||||||
|
this.firebaseService.checkUserUID(user.uid)[0].id
|
||||||
|
);
|
||||||
|
window.location.reload();
|
||||||
|
} else {
|
||||||
|
console.error('No user with this UID was found!');
|
||||||
|
}
|
||||||
|
this.sharedService.isBtnDisabled = false;
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
console.error(error);
|
||||||
|
alert('Invalid email or password! Please try again.');
|
||||||
|
this.sharedService.isBtnDisabled = false;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
getUserIdInLocalStorage(userId: string) {
|
||||||
|
localStorage.setItem('currentUser', JSON.stringify(userId));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -4,6 +4,7 @@ import { Injectable } from '@angular/core';
|
||||||
providedIn: 'root',
|
providedIn: 'root',
|
||||||
})
|
})
|
||||||
export class SharedService {
|
export class SharedService {
|
||||||
|
isBtnDisabled: boolean = false;
|
||||||
isAnyDialogOpen: boolean = false;
|
isAnyDialogOpen: boolean = false;
|
||||||
isEditContactDialogOpen: boolean = false;
|
isEditContactDialogOpen: boolean = false;
|
||||||
isDeleteContactDialogOpen: boolean = false;
|
isDeleteContactDialogOpen: boolean = false;
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,7 @@
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
&:hover {
|
&:hover {
|
||||||
color: var(--light-blue);
|
color: var(--light-blue);
|
||||||
border-color: var(--light-bluek);
|
border-color: var(--light-blue);
|
||||||
box-shadow: 2px 2px 2px 0px rgba(0, 0, 0, 0.3);
|
box-shadow: 2px 2px 2px 0px rgba(0, 0, 0, 0.3);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -94,17 +94,24 @@
|
||||||
padding: 12px 18px;
|
padding: 12px 18px;
|
||||||
margin: 12px;
|
margin: 12px;
|
||||||
border-radius: 10px;
|
border-radius: 10px;
|
||||||
color: var(--black);
|
background-color: var(--light-gray);
|
||||||
background-color: var(--white);
|
|
||||||
border: 1px solid var(--black);
|
border: 1px solid var(--black);
|
||||||
font-size: 23px;
|
font-size: 23px;
|
||||||
font-weight: 700;
|
font-weight: 700;
|
||||||
transition: 125ms ease-in-out;
|
transition: 125ms ease-in-out;
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
&:hover {
|
&:hover:not(:disabled) {
|
||||||
color: var(--light-blue);
|
color: var(--light-blue);
|
||||||
border-color: var(--light-bluek);
|
border-color: var(--light-blue);
|
||||||
box-shadow: 2px 2px 2px 0px rgba(0, 0, 0, 0.3);
|
box-shadow: 2px 2px 2px 0px rgba(0, 0, 0, 0.3);
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
&:not(:disabled) {
|
||||||
|
background-color: var(--white) !important;
|
||||||
|
color: var(--black);
|
||||||
|
}
|
||||||
|
&:hover {
|
||||||
|
cursor: default;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue