update google login

This commit is contained in:
Chneemann 2024-05-11 10:53:30 +02:00
parent b0ca699c7b
commit 4885ffa214
4 changed files with 105 additions and 19 deletions

View file

@ -4,6 +4,7 @@
class="content"
(click)="addAssignedToTask(user.id)"
[ngClass]="{ selected: assigned.includes(user.id) }"
*ngIf="user.id"
>
<div
class="circle"

View file

@ -1,5 +1,5 @@
export interface User {
id: string;
id?: string;
uId: string;
firstName: string;
lastName: string;

View file

@ -6,8 +6,18 @@ import {
GoogleAuthProvider,
} from 'firebase/auth';
import { FirebaseService } from './firebase.service';
import { Firestore, collection, doc, updateDoc } from '@angular/fire/firestore';
import {
Firestore,
addDoc,
collection,
doc,
getDocs,
query,
updateDoc,
where,
} from '@angular/fire/firestore';
import { SharedService } from './shared.service';
import { User } from '../interfaces/user.interface';
@Injectable({
providedIn: 'root',
})
@ -18,21 +28,18 @@ export class LoginService {
constructor(
private firebaseService: FirebaseService,
public sharedService: SharedService
) {}
) {
console.log(this.sharedService.generateRandomColor());
}
login(loginData: { mail: string; password: string }) {
signInWithEmailAndPassword(getAuth(), 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
);
this.updateUserOnlineStatus(
this.firebaseService.checkUserUID(user.uid)[0].id
);
} else {
console.error('No user with this UID was found!');
const userData = this.firebaseService.checkUserUID(user.uid);
if (userData.length > 0 && userData[0].id) {
this.getUserIdInLocalStorage(userData[0].id);
this.updateUserOnlineStatus(userData[0].id);
}
this.sharedService.isBtnDisabled = false;
})
@ -48,20 +55,68 @@ export class LoginService {
const provider = new GoogleAuthProvider();
signInWithPopup(auth, provider)
.then((result) => {
const user = result.user;
console.log(
'Google User: ',
user.displayName,
user.email,
user.photoURL
.then((userCredential) => {
const user = userCredential.user;
const usersCollection = collection(this.firestore, 'users');
const querySnapshot = query(
usersCollection,
where('uId', '==', user.uid)
);
getDocs(querySnapshot).then((snapshot) => {
if (snapshot.empty) {
this.createUserInFirestore({
uId: user.uid,
email: user.email || 'no mail',
firstName: user.displayName ? user.displayName.split(' ')[0] : '',
lastName: user.displayName ? user.displayName.split(' ')[1] : '',
status: true,
phone: '',
initials: user.displayName
? user.displayName.split(' ')[0].slice(0, 1) +
user.displayName.split(' ')[1].slice(0, 1)
: '',
color: this.sharedService.generateRandomColor(),
lastLogin: 0,
});
} else {
this.ifExistUser(user.uid);
}
});
})
.catch((error) => {
console.error('Google login error:', error);
});
}
async createUserInFirestore(user: User) {
const userDataToSave: User = {
uId: user.uId,
email: user.email,
firstName: user.firstName || '',
lastName: user.lastName || '',
status: true,
phone: '',
initials: user.initials,
color: user.color,
lastLogin: new Date().getTime(),
};
const usersCollection = collection(this.firestore, 'users');
try {
const docRef = await addDoc(usersCollection, userDataToSave);
this.ifExistUser(user.uId);
} catch (error) {
console.error(error);
}
}
ifExistUser(user: string) {
const userData = this.firebaseService.checkUserUID(user);
if (userData.length > 0 && userData[0].id) {
this.getUserIdInLocalStorage(userData[0].id);
this.updateUserOnlineStatus(userData[0].id);
}
}
async updateUserOnlineStatus(userId: string) {
await updateDoc(doc(collection(this.firestore, 'users'), userId), {
status: true,

View file

@ -48,4 +48,34 @@ export class SharedService {
deleteUserIdInLocalStorage() {
localStorage.removeItem('currentUser');
}
// RANDOM COLOR
generateRandomColor(): string {
const letters = '0123456789ABCDEF';
let color = '#';
let isGrayScale = true;
while (isGrayScale) {
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
const rgb = this.hexToRgb(color);
if (rgb.r !== rgb.g || rgb.g !== rgb.b) {
isGrayScale = false;
} else {
color = '#';
}
}
return color;
}
hexToRgb(hex: string): { r: number; g: number; b: number } {
const bigint = parseInt(hex.substring(1), 16);
const r = (bigint >> 16) & 255;
const g = (bigint >> 8) & 255;
const b = bigint & 255;
return { r, g, b };
}
}