diff --git a/src/app/components/add-task/assigned/assigned.component.html b/src/app/components/add-task/assigned/assigned.component.html
index cb8fa09..70b9cf2 100644
--- a/src/app/components/add-task/assigned/assigned.component.html
+++ b/src/app/components/add-task/assigned/assigned.component.html
@@ -4,6 +4,7 @@
class="content"
(click)="addAssignedToTask(user.id)"
[ngClass]="{ selected: assigned.includes(user.id) }"
+ *ngIf="user.id"
>
{
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,
diff --git a/src/app/services/shared.service.ts b/src/app/services/shared.service.ts
index 8d2e976..57b227a 100644
--- a/src/app/services/shared.service.ts
+++ b/src/app/services/shared.service.ts
@@ -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 };
+ }
}