Fixed projects not showing on login

This commit is contained in:
Marta Borgia Leiva 2026-02-11 11:56:40 +01:00
parent 7202ed8806
commit cd041d04cd
6 changed files with 37 additions and 29 deletions

View file

@ -12,8 +12,11 @@ export interface LoginRequest {
export interface LoginResponse { export interface LoginResponse {
success: boolean; success: boolean;
message?: string; message?: string;
user?: User; user?: LoginUserResponse;
token?: string; // Optional: if you need the JWT on client side }
export interface LoginUserResponse {
id: number;
} }
export interface RegisterRequest { export interface RegisterRequest {

View file

@ -62,7 +62,8 @@
} }
.loading, .loading,
.error-state { .error-state,
.loading-state {
background: white; background: white;
padding: 30px 24px; padding: 30px 24px;
border-radius: 12px; border-radius: 12px;

View file

@ -8,9 +8,9 @@
</div> </div>
<div class="projects-grid"> <div class="projects-grid">
<ng-container *ngIf="projectList.length > 0; else noProjects"> <ng-container *ngIf="projects.length > 0; else noProjects">
<app-project-item <app-project-item
*ngFor="let project of projectList; trackBy: trackByProjectId" *ngFor="let project of projects; trackBy: trackByProjectId"
[project]="project" [project]="project"
/> />
</ng-container> </ng-container>

View file

@ -1,4 +1,4 @@
import { Component, inject } from '@angular/core'; import { Component, inject, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common'; import { CommonModule } from '@angular/common';
import { Router } from '@angular/router'; import { Router } from '@angular/router';
import { AuthService } from '../../services/auth.service'; import { AuthService } from '../../services/auth.service';
@ -12,12 +12,14 @@ import { Project } from '../../models/projects.models';
templateUrl: './home.component.html', templateUrl: './home.component.html',
styleUrl: './home.component.css', styleUrl: './home.component.css',
}) })
export class HomeComponent { export class HomeComponent implements OnInit {
protected authService = inject(AuthService); protected authService = inject(AuthService);
private router = inject(Router); private router = inject(Router);
protected get projectList(): Project[] { protected projects: Project[] = [];
return this.authService.currentUser()?.projects ?? [];
ngOnInit(): void {
this.projects = this.authService.currentUser()?.projects ?? [];
} }
onCreateProject() { onCreateProject() {
@ -27,4 +29,5 @@ export class HomeComponent {
trackByProjectId(_index: number, project: Project): number { trackByProjectId(_index: number, project: Project): number {
return project.id; return project.id;
} }
} }

View file

@ -67,7 +67,6 @@ export class LoginComponent implements OnInit {
next: (response) => { next: (response) => {
this.isLoading.set(false); this.isLoading.set(false);
if (response.success || response.user) { if (response.success || response.user) {
// Redirect to home/dashboard after successful login
this.router.navigate(['/']); this.router.navigate(['/']);
} else { } else {
this.errorMessage.set(response.message || 'Login failed'); this.errorMessage.set(response.message || 'Login failed');

View file

@ -10,7 +10,7 @@ import { environment } from '../config/environment';
* Uses signals for reactive state management * Uses signals for reactive state management
*/ */
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root',
}) })
export class AuthService { export class AuthService {
private http = inject(HttpClient); private http = inject(HttpClient);
@ -19,7 +19,7 @@ export class AuthService {
// Reactive auth state using signals // Reactive auth state using signals
private authState = signal<AuthState>({ private authState = signal<AuthState>({
isAuthenticated: false, isAuthenticated: false,
user: null user: null,
}); });
// Public computed signals for components to consume // Public computed signals for components to consume
@ -31,19 +31,21 @@ export class AuthService {
* The JWT will be set as an HTTP-only cookie by the backend * The JWT will be set as an HTTP-only cookie by the backend
*/ */
login(credentials: LoginRequest): Observable<LoginResponse> { login(credentials: LoginRequest): Observable<LoginResponse> {
return this.http.post<LoginResponse>( return this.http.post<LoginResponse>(`${environment.apiBaseUrl}/auth/login`, credentials).pipe(
`${environment.apiBaseUrl}/auth/login`, tap((response) => {
credentials
).pipe(
tap(response => {
if (response.success || response.user) { if (response.success || response.user) {
// Update auth state even if the backend only sets a cookie this.http.get<User>(`${environment.apiBaseUrl}/me`).subscribe({
this.authState.set({ next: (user) => {
isAuthenticated: true, this.authState.set({ isAuthenticated: true, user });
user: response.user ?? null // Redirect to home/dashboard after successful login
}); this.router.navigate(['/']);
} },
error: () => {
this.authState.set({ isAuthenticated: false, user: null });
},
}) })
}
}),
); );
} }
@ -57,7 +59,7 @@ export class AuthService {
// Clear auth state // Clear auth state
this.authState.set({ this.authState.set({
isAuthenticated: false, isAuthenticated: false,
user: null user: null,
}); });
// Redirect to login // Redirect to login
this.router.navigate(['/login']); this.router.navigate(['/login']);
@ -66,7 +68,7 @@ export class AuthService {
// Even if logout fails on backend, clear local state // Even if logout fails on backend, clear local state
this.clearAuthState(); this.clearAuthState();
return throwError(() => error); return throwError(() => error);
}) }),
); );
} }
@ -76,12 +78,12 @@ export class AuthService {
*/ */
checkSession(): Observable<User> { checkSession(): Observable<User> {
return this.http.get<User>(`${environment.apiBaseUrl}/me`).pipe( return this.http.get<User>(`${environment.apiBaseUrl}/me`).pipe(
tap(user => { tap((user) => {
this.authState.set({ this.authState.set({
isAuthenticated: true, isAuthenticated: true,
user: user user: user,
}); });
}) }),
); );
} }
@ -91,7 +93,7 @@ export class AuthService {
clearAuthState(): void { clearAuthState(): void {
this.authState.set({ this.authState.set({
isAuthenticated: false, isAuthenticated: false,
user: null user: null,
}); });
this.router.navigate(['/login']); this.router.navigate(['/login']);
} }