Skip to content

Commit f7e67c2

Browse files
committed
profile work
1 parent e28399c commit f7e67c2

File tree

20 files changed

+291
-129
lines changed

20 files changed

+291
-129
lines changed

web/src/app/_components/layout/toolbar/toolbar.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { CommonModule } from '@angular/common';
88
import { AuthService } from '../../../_services/auth-service';
99
import { Profile } from '../../../_pages/user/profile/profile';
1010
import { ProfileDto } from '../../../_models/user-model';
11+
import { UserService } from '../../../_services/user-service';
1112

1213
@Component({
1314
selector: 'app-toolbar',
@@ -28,6 +29,7 @@ export class Toolbar implements OnInit {
2829
@Output() toggle = new EventEmitter<boolean>();
2930

3031
private authService = inject(AuthService);
32+
private userService = inject(UserService);
3133
private router = inject(Router);
3234

3335

@@ -36,7 +38,7 @@ export class Toolbar implements OnInit {
3638
hasShadow = signal(false);
3739

3840
isAuthenticated = this.authService.isAuthenticated;
39-
profile = this.authService.userProfile;
41+
profile = this.userService.userProfile;
4042

4143
constructor() {
4244

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { TestBed } from '@angular/core/testing';
2+
import { CanActivateFn } from '@angular/router';
3+
4+
import { loginGuard } from './login-guard';
5+
6+
describe('loginGuard', () => {
7+
const executeGuard: CanActivateFn = (...guardParameters) =>
8+
TestBed.runInInjectionContext(() => loginGuard(...guardParameters));
9+
10+
beforeEach(() => {
11+
TestBed.configureTestingModule({});
12+
});
13+
14+
it('should be created', () => {
15+
expect(executeGuard).toBeTruthy();
16+
});
17+
});

web/src/app/_guards/login-guard.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { inject } from '@angular/core';
2+
import { CanActivateFn, Router } from '@angular/router';
3+
import { AuthService } from '../_services/auth-service';
4+
5+
export const loginGuard: CanActivateFn = (route, state) => {
6+
const authService = inject(AuthService);
7+
const router = inject(Router);
8+
9+
if (authService.isAuthenticated()) {
10+
// Redirect authenticated users to home or dashboard
11+
router.navigate(['/dashboard']);
12+
return false;
13+
}
14+
15+
return true;
16+
};

web/src/app/_interceptors/auth-interceptor.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ import { inject } from '@angular/core';
33
import { AuthService } from '../_services/auth-service';
44
import { catchError, switchMap, throwError } from 'rxjs';
55
import { Router } from '@angular/router';
6+
import { UserService } from '../_services/user-service';
67

78
export const authInterceptor: HttpInterceptorFn = (req, next) => {
89
const authService = inject(AuthService);
10+
const userService = inject(UserService)
911
const router = inject(Router);
1012

1113
return next(req).pipe(
@@ -16,6 +18,8 @@ export const authInterceptor: HttpInterceptorFn = (req, next) => {
1618
return authService.refreshToken().pipe(
1719
switchMap(() => {
1820
const clonedRequest = req.clone({ withCredentials: true });
21+
userService.getProfile().subscribe(); // Update user profile after token refresh
22+
// Retry the original request with the new token
1923
console.log('Token refreshed, retrying request:', clonedRequest);
2024
return next(clonedRequest);
2125
}),

web/src/app/_models/user-model.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ export interface ProfileDto {
2727
id?: number; // Added to match backend
2828
name: string;
2929
email: string;
30+
address: string;
31+
phoneNumber: string;
3032
profilePicture?: string | null; // Explicitly allow null
33+
role: string;
34+
bio: string;
3135
}
3236

3337
// UpdateProfilePictureDto is correct

web/src/app/_pages/auth/login/login.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@
4242

4343
<div class="social-login">
4444
<div id="google-button"></div>
45-
<button (click)="triggerSignIn()">Google Login</button>
45+
<!-- <button (click)="triggerSignIn()">Google Login</button> -->
4646
</div>
4747

4848
@if (errorMessage) {
4949
<mat-error class="error-message">{{ errorMessage }}</mat-error>
5050
}
5151
</mat-card-content>
5252
</mat-card>
53-
</div>
53+
</div>

web/src/app/_pages/auth/login/login.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { MatCheckboxModule } from '@angular/material/checkbox';
99
import { Router, RouterLink, RouterModule } from '@angular/router';
1010
import { LoginDto, SocialLoginDto } from '../../../_models/user-model';
1111
import { GoogleAuthService } from '../../../_services/google-auth-service';
12+
import { UserService } from '../../../_services/user-service';
1213

1314

1415
@Component({
@@ -33,6 +34,7 @@ export class Login implements OnInit {
3334
private fb = inject(FormBuilder);
3435
private authService = inject(AuthService);
3536
private router = inject(Router);
37+
private userService = inject(UserService);
3638
loginForm: FormGroup;
3739
errorMessage = '';
3840

@@ -55,7 +57,17 @@ export class Login implements OnInit {
5557

5658
const loginDto: LoginDto = this.loginForm.value;
5759
this.authService.login(loginDto).subscribe({
58-
next: () => this.router.navigate(['/']),
60+
next: () => {
61+
this.userService.getProfile().subscribe({
62+
next: (profile) => {
63+
this.userService.setProfile(profile);
64+
this.router.navigate(['/']);
65+
},
66+
error: (err: Error) => {
67+
this.errorMessage = err.message;
68+
}
69+
});
70+
},
5971
error: (err: Error) => this.errorMessage = err.message
6072
});
6173
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>not-found works!</p>

web/src/app/_pages/not-found/not-found.scss

Whitespace-only changes.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { ComponentFixture, TestBed } from '@angular/core/testing';
2+
3+
import { NotFound } from './not-found';
4+
5+
describe('NotFound', () => {
6+
let component: NotFound;
7+
let fixture: ComponentFixture<NotFound>;
8+
9+
beforeEach(async () => {
10+
await TestBed.configureTestingModule({
11+
imports: [NotFound]
12+
})
13+
.compileComponents();
14+
15+
fixture = TestBed.createComponent(NotFound);
16+
component = fixture.componentInstance;
17+
fixture.detectChanges();
18+
});
19+
20+
it('should create', () => {
21+
expect(component).toBeTruthy();
22+
});
23+
});

0 commit comments

Comments
 (0)