Skip to content
Open
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
Empty file removed front/src/app/app.component.scss
Empty file.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ <h1 class="text-gray-900 text-2xl md:ml-10 text-primaryColor font-medium md:pt-0

<app-button
type="button"
[customClass]="getCurrentsTabClasses()"
[customClass]="currentsTabClasses()"
[buttonHandler]="getCurrentsTabHandler()"
[disabled]="isLoading()"
[ariaLabel]="'Show current events'"
[attr.aria-selected]="activeTab() === 'currents'">
[ariaSelected]="activeTab() === 'currents'">
Currents
</app-button>

<app-button
type="button"
[customClass]="getPassedTabClasses()"
[customClass]="passedTabClasses()"
[buttonHandler]="getPassedTabHandler()"
[disabled]="isLoading()"
[ariaLabel]="'Show past events'"
[attr.aria-selected]="activeTab() === 'passed'">
[ariaSelected]="activeTab() === 'currents'">
Passed
</app-button>
</nav>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,7 @@ export class IsLoginHomePageComponent {

return `${baseClasses} ${activeClasses}`.trim();
}

getCurrentsTabClasses(): string {
return this.currentsTabClasses();
}

getPassedTabClasses(): string {
return this.passedTabClasses();
}


getCurrentsTabHandler(): () => void {
return () => this.setActiveTab('currents');
}
Expand Down
Empty file.
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { LoginFormComponent } from './login-form.component';
import { AuthService } from '../services/auth.service';
import { Router } from '@angular/router';
import { FormsModule } from '@angular/forms';
import { ButtonWithIconComponent } from "../../../shared/button-with-icon/button-with-icon.component";
import { MatDialog } from '@angular/material/dialog';
import { AuthErrorDialogComponent } from '../../../shared/auth-error-dialog/auth-error-dialog.component';
import { of } from 'rxjs';
import {ButtonComponent} from '../../../shared/button/button.component';

describe('LoginFormComponent', () => {
let component: LoginFormComponent;
Expand Down Expand Up @@ -52,7 +52,7 @@ describe('LoginFormComponent', () => {
imports: [
FormsModule,
LoginFormComponent,
ButtonWithIconComponent
ButtonComponent
],
providers: [
{ provide: AuthService, useValue: authServiceMock },
Expand Down
Empty file.
16 changes: 16 additions & 0 deletions front/src/app/core/login/services/auth-backend.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';

import { AuthBackendService } from './auth-backend.service';

describe('AuthBackendService', () => {
let service: AuthBackendService;

beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(AuthBackendService);
});

it('should be created', () => {
expect(service).toBeTruthy();
});
});
147 changes: 147 additions & 0 deletions front/src/app/core/login/services/auth-backend.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import { Injectable, inject } from '@angular/core';
import {
Auth,
User as FirebaseUser,
} from '@angular/fire/auth';
import {HttpClient} from '@angular/common/http';
import {UserStateService} from '../../services/user-services/user-state.service';
import {User} from '../../models/user.model';
import {firstValueFrom} from 'rxjs';
import {environment} from '../../../../environments/environment.development';

@Injectable({
providedIn: 'root'
})
export class AuthBackendService {
private readonly http = inject(HttpClient);
private readonly auth = inject(Auth);
private readonly userState = inject(UserStateService);

async syncUserData(firebaseUser: FirebaseUser): Promise<void> {
try {
const userData = await this.fetchUserData(firebaseUser.uid);

if (userData) {
const mergedUser = this.mergeUserData(firebaseUser, userData);
this.userState.updateUser(mergedUser);
this.userState.saveToStorage();
}
} catch (error) {
console.error('Error syncing user data:', error);
}
}

async processUserLogin(user: FirebaseUser): Promise<void> {
const token = await user.getIdToken();

await Promise.all([
this.sendTokenToBackend(token),
this.processInvitations(user),
this.saveUserToBackend(this.createUserPayload(user))
]);

this.userState.updateUser(this.createUserPayload(user));
this.userState.saveToStorage();
}

private mergeUserData(firebaseUser: FirebaseUser, userData: User): User {
return {
uid: firebaseUser.uid,
email: userData.email || firebaseUser.email || '',
displayName: userData.displayName || firebaseUser.displayName || '',
photoURL: userData.photoURL || firebaseUser.photoURL || '',
company: userData.company || '',
city: userData.city || '',
phoneNumber: userData.phoneNumber || '',
githubLink: userData.githubLink || '',
twitterLink: userData.twitterLink || '',
blueSkyLink: userData.blueSkyLink || '',
linkedInLink: userData.linkedInLink || '',
biography: userData.biography || '',
otherLink: userData.otherLink || ''
};
}

private createUserPayload(user: FirebaseUser): Partial<User> {
return {
uid: user.uid,
email: user.email,
displayName: user.displayName,
photoURL: user.photoURL
};
}

async getIdToken(forceRefresh = true): Promise<string | null> {
try {
if (!this.auth.currentUser) return null;

const token = await this.auth.currentUser.getIdToken(forceRefresh);
await this.sendTokenToBackend(token);
return token;
} catch {
return null;
}
}

async logout(): Promise<void> {
try {
await firstValueFrom(
this.http.post(`${environment.apiUrl}/auth/logout`, {}, { withCredentials: true })
);
} catch (error) {
console.error('Backend logout error:', error);
}
}

private async fetchUserData(uid: string): Promise<User | null> {
try {
return await firstValueFrom(
this.http.get<User>(`${environment.apiUrl}/auth/user/${uid}`, { withCredentials: true })
);
} catch {
return null;
}
}

private async sendTokenToBackend(token: string): Promise<void> {
await firstValueFrom(
this.http.post(`${environment.apiUrl}/auth/login`, { idToken: token }, { withCredentials: true })
);
}

private async saveUserToBackend(user: Partial<User>): Promise<void> {
if (!user?.uid) return;

await firstValueFrom(
this.http.post(`${environment.apiUrl}/auth`, user, { withCredentials: true })
);
}

async processInvitations(user: FirebaseUser): Promise<void> {
if (!user?.email) return;

try {
await firstValueFrom(
this.http.post(`${environment.apiUrl}/public/invitations/process`, {
email: user.email.toLowerCase(),
uid: user.uid
})
);
} catch (error) {
console.error('Error processing invitations:', error);
}
}

async getCurrentUserToken(): Promise<string | null> {
try {
const currentUser = this.auth.currentUser;
if (currentUser) {
return await currentUser.getIdToken(true);
}
return null;
} catch (error) {
console.error('Error getting user token:', error);
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';

import { EmailLinkService } from './email-link.service';

describe('EmailLinkService', () => {
let service: EmailLinkService;

beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(EmailLinkService);
});

it('should be created', () => {
expect(service).toBeTruthy();
});
});
39 changes: 39 additions & 0 deletions front/src/app/core/login/services/auth-error-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import {inject, Injectable} from '@angular/core';
import {MatDialog} from '@angular/material/dialog';
import {AuthErrorDialogComponent} from '../../../shared/auth-error-dialog/auth-error-dialog.component';

@Injectable({
providedIn: 'root'
})
export class AuthErrorHandlerService {
private readonly dialog = inject(MatDialog);

handleProviderError(error: any): null {
if (error.code === 'auth/account-exists-with-different-credential') {
this.showAuthErrorDialog(error.customData.email);
}
return null;
}

showAuthErrorDialog(email: string): void {
this.dialog.open(AuthErrorDialogComponent, {
width: '400px',
data: {
title: 'Authentication Error',
email,
message: `The email address "${email}" is already associated with another sign-in method.`
}
});
}

showSuccessDialog(title: string, message: string): void {
this.dialog.open(AuthErrorDialogComponent, {
width: '400px',
data: { title, message }
});
}

openDialog(component: any, config: any) {
return this.dialog.open(component, config);
}
}
16 changes: 16 additions & 0 deletions front/src/app/core/login/services/auth-provider.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';

import { AuthProviderService } from './auth-provider.service';

describe('AuthProviderService', () => {
let service: AuthProviderService;

beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(AuthProviderService);
});

it('should be created', () => {
expect(service).toBeTruthy();
});
});
47 changes: 47 additions & 0 deletions front/src/app/core/login/services/auth-providers.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Injectable, inject } from '@angular/core';
import { Router} from '@angular/router';
import {
Auth,
User as FirebaseUser,
signInWithPopup,
GoogleAuthProvider,
GithubAuthProvider,
} from '@angular/fire/auth';
import {AuthBackendService} from './auth-backend.service';
import {AuthErrorHandlerService} from './auth-error-handler';

@Injectable({
providedIn: 'root'
})
export class AuthProvidersService {
private readonly auth = inject(Auth);
private readonly router = inject(Router);
private readonly authBackend = inject(AuthBackendService);
private readonly errorHandler = inject(AuthErrorHandlerService);

async loginWithGoogle() {
return this.loginWithProvider('google', new GoogleAuthProvider());
}

async loginWithGitHub() {
return this.loginWithProvider('github', new GithubAuthProvider());
}

private async loginWithProvider(
providerType: 'google' | 'github',
provider: GoogleAuthProvider | GithubAuthProvider
): Promise<FirebaseUser | null> {
try {
const result = await signInWithPopup(this.auth, provider);

if (result.user) {
await this.authBackend.processUserLogin(result.user);
this.router.navigate(['/']);
}

return result.user;
} catch (error: any) {
return this.errorHandler.handleProviderError(error);
}
}
}
Loading