diff --git a/src/app/app-routing.module.ts b/src/app/app-routing.module.ts index c4fde3d..2571469 100644 --- a/src/app/app-routing.module.ts +++ b/src/app/app-routing.module.ts @@ -10,6 +10,7 @@ import { SigninComponent } from './pages/signin/signin.component'; import { SignupComponent } from './pages/signup/signup.component'; import { AdminPageComponent } from './pages/admin-page/admin-page.component'; import { AuthGuard } from './services/auth.guard'; +import { PageAccountComponent } from './pages/page-account/page-account.component'; const routes: Routes = [ { path: '', redirectTo: 'signin', pathMatch: 'full' }, @@ -19,6 +20,7 @@ const routes: Routes = [ { path: 'categories', component: ListCategoriesComponent }, { path: 'favoris', component: FavorisUserComponent }, { path: 'filtres', component: FiltersPageComponent }, + { path: 'profil', component: PageAccountComponent }, { path: 'Deconnexion', redirectTo: 'home'}, {path: 'restaurants', canActivate: [AuthGuard], component: RestoPageComponent}, {path: 'page-not-found',component: PageNotFoundComponent}, diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 895593a..de864a8 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -25,6 +25,7 @@ import { HeaderLogoComponent } from './header/components/header-logo/header-logo import { AuthInterceptor } from './services/auth.interceptor'; import { FavorisUserComponent } from './pages/favoris-user/favoris-user.component'; import { SignupComponent } from './pages/signup/signup.component'; +import { PageAccountComponent } from './pages/page-account/page-account.component'; @NgModule({ declarations: [ @@ -48,7 +49,8 @@ import { SignupComponent } from './pages/signup/signup.component'; UpdateDelRestauComponent, HeaderLogoComponent, FavorisUserComponent, - SignupComponent + SignupComponent, + PageAccountComponent ], imports: [ BrowserModule, diff --git a/src/app/header/components/nav-bar/nav-bar.component.html b/src/app/header/components/nav-bar/nav-bar.component.html index 8fadc20..6a99db4 100644 --- a/src/app/header/components/nav-bar/nav-bar.component.html +++ b/src/app/header/components/nav-bar/nav-bar.component.html @@ -16,6 +16,7 @@ Restaurants Filtres Mes favoris + Mon profil Deconnexion diff --git a/src/app/pages/models/user.ts b/src/app/pages/models/user.ts index 9e2e971..2eba1ca 100644 --- a/src/app/pages/models/user.ts +++ b/src/app/pages/models/user.ts @@ -1,5 +1,3 @@ -import { RoleList } from "./roleList"; - export interface User { id?:number; prenom: string; diff --git a/src/app/pages/page-account/page-account.component.html b/src/app/pages/page-account/page-account.component.html new file mode 100644 index 0000000..52fc6ea --- /dev/null +++ b/src/app/pages/page-account/page-account.component.html @@ -0,0 +1,16 @@ +
+
+
+ + +
+
+ + +
+
+ + +
+
+
\ No newline at end of file diff --git a/src/app/pages/page-account/page-account.component.scss b/src/app/pages/page-account/page-account.component.scss new file mode 100644 index 0000000..e69de29 diff --git a/src/app/pages/page-account/page-account.component.spec.ts b/src/app/pages/page-account/page-account.component.spec.ts new file mode 100644 index 0000000..8bbe80f --- /dev/null +++ b/src/app/pages/page-account/page-account.component.spec.ts @@ -0,0 +1,25 @@ +import { ComponentFixture, TestBed } from '@angular/core/testing'; + +import { PageAccountComponent } from './page-account.component'; + +describe('PageAccountComponent', () => { + let component: PageAccountComponent; + let fixture: ComponentFixture; + + beforeEach(async () => { + await TestBed.configureTestingModule({ + declarations: [ PageAccountComponent ] + }) + .compileComponents(); + }); + + beforeEach(() => { + fixture = TestBed.createComponent(PageAccountComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/src/app/pages/page-account/page-account.component.ts b/src/app/pages/page-account/page-account.component.ts new file mode 100644 index 0000000..41068c2 --- /dev/null +++ b/src/app/pages/page-account/page-account.component.ts @@ -0,0 +1,36 @@ +import { Component, OnInit } from '@angular/core'; +import { FormBuilder, FormGroup } from '@angular/forms'; +import { AuthService } from 'src/app/services/auth.service'; +import { User } from '../models/user'; + +@Component({ + selector: 'app-page-account', + templateUrl: './page-account.component.html', + styleUrls: ['./page-account.component.scss'] +}) +export class PageAccountComponent implements OnInit { + + public userInfo: FormGroup; + + constructor(private fb: FormBuilder, private authService: AuthService) { + this.userInfo = this.initForm(); + + this.authService.getConnectedUserInfo()?.subscribe( + (user: User) => { + this.userInfo = this.initForm(user); + } + ) + + } + + ngOnInit(): void { + } + + private initForm(user?: User): FormGroup { + return this.fb.group({ + firstName: [user ? user.prenom : ''], + lastName: [user ? user.nom : ''], + email: [user ? user.email : ''] + }) + } +} diff --git a/src/app/services/auth.service.ts b/src/app/services/auth.service.ts index 4fe0010..9cf8b88 100644 --- a/src/app/services/auth.service.ts +++ b/src/app/services/auth.service.ts @@ -3,6 +3,9 @@ import { Injectable } from '@angular/core'; import { map, Observable } from 'rxjs'; import { environment } from 'src/environments/environment'; import { User } from 'src/app/pages/models/user'; +import jwt_decode from 'jwt-decode'; +import { Router } from '@angular/router'; + @Injectable({ @@ -12,7 +15,7 @@ export class AuthService { private apiUrl: string; private tokenKey: string; - constructor(private http: HttpClient) { + constructor(private http: HttpClient, private router: Router) { // On se sert des variables d'environnement de notre application this.apiUrl = environment.apiUrl; this.tokenKey = environment.tokenKey; @@ -47,4 +50,15 @@ export class AuthService { ); } + getConnectedUserInfo(): Observable | void { + const token = localStorage.getItem(this.tokenKey); + if(token) { + const decodedToken = jwt_decode(token); + const userId = decodedToken.userId; + return this.http.get(`${this.apiUrl}/user/${userId}`); + } else { + this.router.navigate(['signin']); + } + } + }