Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions apps/main-dashboard/src/app/app.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { provideAnimations } from '@angular/platform-browser/animations';
import { provideTranslateService, provideTranslateLoader } from '@ngx-translate/core';
import { provideTranslateHttpLoader } from '@ngx-translate/http-loader';
import { provideFirebaseApp, initializeApp } from '@angular/fire/app';
import { provideAuth, getAuth, browserLocalPersistence, setPersistence } from '@angular/fire/auth';
import { provideAuth, getAuth } from '@angular/fire/auth';
import { providePrimeNG } from 'primeng/config';
import { routes } from './app.routes';
import { environment } from '../environments/environment';
Expand All @@ -27,14 +27,7 @@ export const appConfig: ApplicationConfig = {
provideHttpClient(withInterceptors([authInterceptor])),
provideAnimations(),
provideFirebaseApp(() => initializeApp(environment.firebase)),
provideAuth(() => {
const auth = getAuth();
// Utiliser indexedDB au lieu de sessionStorage pour meilleure compatibilité mobile
setPersistence(auth, browserLocalPersistence).catch((error) => {
console.error('Erreur lors de la configuration de la persistance Firebase:', error);
});
return auth;
}),
provideAuth(() => getAuth()),
providePrimeNG({
theme: {
preset: MyPreset,
Expand Down
56 changes: 4 additions & 52 deletions apps/main-dashboard/src/app/modules/auth/services/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { HttpClient } from '@angular/common/http';
import { inject, Injectable, forwardRef } from '@angular/core';
import { Router } from '@angular/router';
import { TokenService } from '../../../shared/services/token.service';
import { CookieService } from '../../../shared/services/cookie.service';
import {
Expand All @@ -9,8 +8,6 @@ import {
GoogleAuthProvider,
signInWithEmailAndPassword,
signInWithPopup,
signInWithRedirect,
getRedirectResult,
signOut,
user,
User,
Expand All @@ -23,7 +20,6 @@ import { environment } from '../../../../environments/environment';
})
export class AuthService {
private auth = inject(Auth);
private router = inject(Router);
user$: Observable<User | null>;
private http = inject(HttpClient);
private tokenService = inject(forwardRef(() => TokenService));
Expand All @@ -33,34 +29,6 @@ export class AuthService {

constructor() {
this.user$ = user(this.auth);
this.handleRedirectResult();
}

/**
* Détecte si l'appareil est mobile
*/
private isMobileDevice(): boolean {
return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
navigator.userAgent,
);
}

/**
* Gère le résultat de la redirection OAuth
*/
private async handleRedirectResult(): Promise<void> {
try {
const result = await getRedirectResult(this.auth);
if (result?.user) {
console.log('Utilisateur authentifié via redirect, traitement du login...');
await this.postLogin(result.user);
// Rediriger vers le dashboard après une authentification réussie
console.log('Navigation vers /console après authentification mobile réussie');
await this.router.navigate(['/console']);
}
} catch (error) {
console.error('Erreur lors de la gestion du résultat de redirection:', error);
}
}

login(email: string, password: string): Observable<void> {
Expand All @@ -72,30 +40,14 @@ export class AuthService {

async loginWithGithub() {
const provider = new GithubAuthProvider();

if (this.isMobileDevice()) {
// Sur mobile, utiliser redirect pour éviter les problèmes de sessionStorage
await signInWithRedirect(this.auth, provider);
// Le résultat sera géré par handleRedirectResult() au prochain chargement
} else {
// Sur desktop, utiliser popup
const result = await signInWithPopup(this.auth, provider);
await this.postLogin(result.user);
}
const result = await signInWithPopup(this.auth, provider);
await this.postLogin(result.user);
}

async loginWithGoogle() {
const provider = new GoogleAuthProvider();

if (this.isMobileDevice()) {
// Sur mobile, utiliser redirect pour éviter les problèmes de sessionStorage
await signInWithRedirect(this.auth, provider);
// Le résultat sera géré par handleRedirectResult() au prochain chargement
} else {
// Sur desktop, utiliser popup
const result = await signInWithPopup(this.auth, provider);
await this.postLogin(result.user);
}
const result = await signInWithPopup(this.auth, provider);
await this.postLogin(result.user);
}

private async postLogin(user: User) {
Expand Down