clean code
This commit is contained in:
parent
17a00aa912
commit
8e48a3b3a3
4 changed files with 31 additions and 35 deletions
|
|
@ -40,7 +40,7 @@ export class LoginService {
|
||||||
const userData = this.firebaseService.getUserDataFromUid(user.uid);
|
const userData = this.firebaseService.getUserDataFromUid(user.uid);
|
||||||
if (userData.length > 0 && userData[0].id) {
|
if (userData.length > 0 && userData[0].id) {
|
||||||
this.getUserIdInLocalStorage(userData[0].id);
|
this.getUserIdInLocalStorage(userData[0].id);
|
||||||
this.updateUserOnlineStatus(userData[0].id);
|
this.updateUserOnlineStatus(userData[0].id, true);
|
||||||
}
|
}
|
||||||
this.sharedService.isBtnDisabled = false;
|
this.sharedService.isBtnDisabled = false;
|
||||||
})
|
})
|
||||||
|
|
@ -107,31 +107,23 @@ export class LoginService {
|
||||||
);
|
);
|
||||||
getDocs(querySnapshot).then((snapshot) => {
|
getDocs(querySnapshot).then((snapshot) => {
|
||||||
if (snapshot.empty) {
|
if (snapshot.empty) {
|
||||||
|
const displayName = user.displayName || '';
|
||||||
|
const [firstName = '', lastName = ''] = displayName.split(' ');
|
||||||
this.createUserInFirestore({
|
this.createUserInFirestore({
|
||||||
uId: user.uid,
|
uId: user.uid,
|
||||||
email: user.email || 'no mail',
|
email: user.email || 'no mail',
|
||||||
firstName: user.displayName
|
firstName: firstName.charAt(0).toUpperCase() + firstName.slice(1),
|
||||||
? user.displayName.split(' ')[0].charAt(0).toUpperCase() +
|
lastName: lastName.charAt(0).toUpperCase() + lastName.slice(1),
|
||||||
user.displayName.split(' ')[0].slice(1)
|
|
||||||
: '',
|
|
||||||
lastName: user.displayName
|
|
||||||
? user.displayName.split(' ')[1].charAt(0).toUpperCase() +
|
|
||||||
user.displayName.split(' ')[1].slice(1)
|
|
||||||
: '',
|
|
||||||
status: true,
|
status: true,
|
||||||
phone: '',
|
phone: '',
|
||||||
initials: user.displayName
|
initials: firstName.slice(0, 1) + lastName.slice(0, 1),
|
||||||
? user.displayName.split(' ')[0].slice(0, 1) +
|
|
||||||
user.displayName.split(' ')[1].slice(0, 1)
|
|
||||||
: '',
|
|
||||||
color: this.sharedService.generateRandomColor(),
|
color: this.sharedService.generateRandomColor(),
|
||||||
lastLogin: 0,
|
lastLogin: 0,
|
||||||
});
|
});
|
||||||
this.sharedService.isBtnDisabled = false;
|
|
||||||
} else {
|
} else {
|
||||||
this.ifExistUser(user.uid);
|
this.ifExistUser(user.uid);
|
||||||
this.sharedService.isBtnDisabled = false;
|
|
||||||
}
|
}
|
||||||
|
this.sharedService.isBtnDisabled = false;
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
|
|
@ -167,13 +159,13 @@ export class LoginService {
|
||||||
const userData = this.firebaseService.getUserDataFromUid(user);
|
const userData = this.firebaseService.getUserDataFromUid(user);
|
||||||
if (userData.length > 0 && userData[0].id) {
|
if (userData.length > 0 && userData[0].id) {
|
||||||
this.getUserIdInLocalStorage(userData[0].id);
|
this.getUserIdInLocalStorage(userData[0].id);
|
||||||
this.updateUserOnlineStatus(userData[0].id);
|
this.updateUserOnlineStatus(userData[0].id, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async updateUserOnlineStatus(userId: string) {
|
async updateUserOnlineStatus(userId: string, status: boolean) {
|
||||||
await updateDoc(doc(collection(this.firestore, 'users'), userId), {
|
await updateDoc(doc(collection(this.firestore, 'users'), userId), {
|
||||||
status: true,
|
status: status,
|
||||||
lastLogin: new Date().getTime(),
|
lastLogin: new Date().getTime(),
|
||||||
}).catch((err) => {
|
}).catch((err) => {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
|
|
@ -197,4 +189,15 @@ export class LoginService {
|
||||||
getUserIdInLocalStorage(userId: string) {
|
getUserIdInLocalStorage(userId: string) {
|
||||||
localStorage.setItem('currentUserJOIN', JSON.stringify(userId));
|
localStorage.setItem('currentUserJOIN', JSON.stringify(userId));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LOGOUT
|
||||||
|
|
||||||
|
logout(userId: string) {
|
||||||
|
this.deleteUserIdInLocalStorage();
|
||||||
|
this.updateUserOnlineStatus(userId, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
deleteUserIdInLocalStorage() {
|
||||||
|
localStorage.removeItem('currentUserJOIN');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -40,17 +40,6 @@ export class SharedService {
|
||||||
this.removeResizeListener();
|
this.removeResizeListener();
|
||||||
}
|
}
|
||||||
|
|
||||||
// LOGOUT
|
|
||||||
|
|
||||||
logout() {
|
|
||||||
this.deleteUserIdInLocalStorage();
|
|
||||||
window.location.reload();
|
|
||||||
}
|
|
||||||
|
|
||||||
deleteUserIdInLocalStorage() {
|
|
||||||
localStorage.removeItem('currentUserJOIN');
|
|
||||||
}
|
|
||||||
|
|
||||||
// RANDOM COLOR
|
// RANDOM COLOR
|
||||||
|
|
||||||
generateRandomColor(): string {
|
generateRandomColor(): string {
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,8 @@ import { HeaderComponent } from '../header.component';
|
||||||
import { LanguageService } from '../../../../services/language.service';
|
import { LanguageService } from '../../../../services/language.service';
|
||||||
import { CommonModule } from '@angular/common';
|
import { CommonModule } from '@angular/common';
|
||||||
import { TranslateModule } from '@ngx-translate/core';
|
import { TranslateModule } from '@ngx-translate/core';
|
||||||
import { SharedService } from '../../../../services/shared.service';
|
import { LoginService } from '../../../../services/login.service';
|
||||||
|
import { FirebaseService } from '../../../../services/firebase.service';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-navbar',
|
selector: 'app-navbar',
|
||||||
|
|
@ -22,7 +23,8 @@ export class NavbarComponent {
|
||||||
constructor(
|
constructor(
|
||||||
public langService: LanguageService,
|
public langService: LanguageService,
|
||||||
private router: Router,
|
private router: Router,
|
||||||
private sharedService: SharedService
|
private loginService: LoginService,
|
||||||
|
private firebaseService: FirebaseService
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
ngOnInit() {
|
ngOnInit() {
|
||||||
|
|
@ -30,6 +32,6 @@ export class NavbarComponent {
|
||||||
}
|
}
|
||||||
|
|
||||||
logout() {
|
logout() {
|
||||||
this.sharedService.logout();
|
this.loginService.logout(this.firebaseService.getCurrentUserId());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,8 @@ import { Component, OnInit } from '@angular/core';
|
||||||
import { NavigationEnd, Router, RouterModule } from '@angular/router';
|
import { NavigationEnd, Router, RouterModule } from '@angular/router';
|
||||||
import { TranslateModule } from '@ngx-translate/core';
|
import { TranslateModule } from '@ngx-translate/core';
|
||||||
import { LanguageService } from '../../../services/language.service';
|
import { LanguageService } from '../../../services/language.service';
|
||||||
import { SharedService } from '../../../services/shared.service';
|
import { LoginService } from '../../../services/login.service';
|
||||||
|
import { FirebaseService } from '../../../services/firebase.service';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-sidebar',
|
selector: 'app-sidebar',
|
||||||
|
|
@ -18,7 +19,8 @@ export class SidebarComponent implements OnInit {
|
||||||
constructor(
|
constructor(
|
||||||
private router: Router,
|
private router: Router,
|
||||||
public languageService: LanguageService,
|
public languageService: LanguageService,
|
||||||
private sharedService: SharedService
|
private loginService: LoginService,
|
||||||
|
private firebaseService: FirebaseService
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
ngOnInit(): void {
|
ngOnInit(): void {
|
||||||
|
|
@ -26,7 +28,7 @@ export class SidebarComponent implements OnInit {
|
||||||
}
|
}
|
||||||
|
|
||||||
logout() {
|
logout() {
|
||||||
this.sharedService.logout();
|
this.loginService.logout(this.firebaseService.getCurrentUserId());
|
||||||
}
|
}
|
||||||
|
|
||||||
getCurrentPath() {
|
getCurrentPath() {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue