update google login
This commit is contained in:
parent
b0ca699c7b
commit
4885ffa214
4 changed files with 105 additions and 19 deletions
|
|
@ -4,6 +4,7 @@
|
||||||
class="content"
|
class="content"
|
||||||
(click)="addAssignedToTask(user.id)"
|
(click)="addAssignedToTask(user.id)"
|
||||||
[ngClass]="{ selected: assigned.includes(user.id) }"
|
[ngClass]="{ selected: assigned.includes(user.id) }"
|
||||||
|
*ngIf="user.id"
|
||||||
>
|
>
|
||||||
<div
|
<div
|
||||||
class="circle"
|
class="circle"
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
export interface User {
|
export interface User {
|
||||||
id: string;
|
id?: string;
|
||||||
uId: string;
|
uId: string;
|
||||||
firstName: string;
|
firstName: string;
|
||||||
lastName: string;
|
lastName: string;
|
||||||
|
|
|
||||||
|
|
@ -6,8 +6,18 @@ import {
|
||||||
GoogleAuthProvider,
|
GoogleAuthProvider,
|
||||||
} from 'firebase/auth';
|
} from 'firebase/auth';
|
||||||
import { FirebaseService } from './firebase.service';
|
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 { SharedService } from './shared.service';
|
||||||
|
import { User } from '../interfaces/user.interface';
|
||||||
@Injectable({
|
@Injectable({
|
||||||
providedIn: 'root',
|
providedIn: 'root',
|
||||||
})
|
})
|
||||||
|
|
@ -18,21 +28,18 @@ export class LoginService {
|
||||||
constructor(
|
constructor(
|
||||||
private firebaseService: FirebaseService,
|
private firebaseService: FirebaseService,
|
||||||
public sharedService: SharedService
|
public sharedService: SharedService
|
||||||
) {}
|
) {
|
||||||
|
console.log(this.sharedService.generateRandomColor());
|
||||||
|
}
|
||||||
|
|
||||||
login(loginData: { mail: string; password: string }) {
|
login(loginData: { mail: string; password: string }) {
|
||||||
signInWithEmailAndPassword(getAuth(), loginData.mail, loginData.password)
|
signInWithEmailAndPassword(getAuth(), loginData.mail, loginData.password)
|
||||||
.then((userCredential) => {
|
.then((userCredential) => {
|
||||||
const user = userCredential.user;
|
const user = userCredential.user;
|
||||||
if (this.firebaseService.checkUserUID(user.uid).length > 0) {
|
const userData = this.firebaseService.checkUserUID(user.uid);
|
||||||
this.getUserIdInLocalStorage(
|
if (userData.length > 0 && userData[0].id) {
|
||||||
this.firebaseService.checkUserUID(user.uid)[0].id
|
this.getUserIdInLocalStorage(userData[0].id);
|
||||||
);
|
this.updateUserOnlineStatus(userData[0].id);
|
||||||
this.updateUserOnlineStatus(
|
|
||||||
this.firebaseService.checkUserUID(user.uid)[0].id
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
console.error('No user with this UID was found!');
|
|
||||||
}
|
}
|
||||||
this.sharedService.isBtnDisabled = false;
|
this.sharedService.isBtnDisabled = false;
|
||||||
})
|
})
|
||||||
|
|
@ -48,20 +55,68 @@ export class LoginService {
|
||||||
const provider = new GoogleAuthProvider();
|
const provider = new GoogleAuthProvider();
|
||||||
|
|
||||||
signInWithPopup(auth, provider)
|
signInWithPopup(auth, provider)
|
||||||
.then((result) => {
|
.then((userCredential) => {
|
||||||
const user = result.user;
|
const user = userCredential.user;
|
||||||
console.log(
|
const usersCollection = collection(this.firestore, 'users');
|
||||||
'Google User: ',
|
const querySnapshot = query(
|
||||||
user.displayName,
|
usersCollection,
|
||||||
user.email,
|
where('uId', '==', user.uid)
|
||||||
user.photoURL
|
|
||||||
);
|
);
|
||||||
|
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) => {
|
.catch((error) => {
|
||||||
console.error('Google login error:', 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) {
|
async updateUserOnlineStatus(userId: string) {
|
||||||
await updateDoc(doc(collection(this.firestore, 'users'), userId), {
|
await updateDoc(doc(collection(this.firestore, 'users'), userId), {
|
||||||
status: true,
|
status: true,
|
||||||
|
|
|
||||||
|
|
@ -48,4 +48,34 @@ export class SharedService {
|
||||||
deleteUserIdInLocalStorage() {
|
deleteUserIdInLocalStorage() {
|
||||||
localStorage.removeItem('currentUser');
|
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 };
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue