page account

This commit is contained in:
Romain Verger 2022-03-07 15:36:40 +01:00
parent b771677562
commit c80b74eb7f
9 changed files with 98 additions and 4 deletions

View File

@ -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},

View File

@ -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,

View File

@ -16,6 +16,7 @@
<a routerLink="restaurants" routerLinkActive="active-custom" class="nav-link p-4 pe-5" style="font-size: 21px;">Restaurants</a>
<a routerLink="filtres" routerLinkActive="active-custom" class="nav-link p-4 pe-5" style="font-size: 21px;">Filtres</a>
<a routerLink="favoris" routerLinkActive="active-custom" class="nav-link p-4 pe-5" style="font-size: 21px;">Mes favoris</a>
<a routerLink="profil" routerLinkActive="active-custom" class="nav-link p-4 pe-5" style="font-size: 21px;">Mon profil</a>
<a routerLink="deconnexion" routerLinkActive="active-custom" (click) = "onCloseSession()" class="nav-link p-4 pe-5" style="font-size: 21px;">Deconnexion</a>
</div>
</div>

View File

@ -1,5 +1,3 @@
import { RoleList } from "./roleList";
export interface User {
id?:number;
prenom: string;

View File

@ -0,0 +1,16 @@
<div class="container mt-5">
<form class="row g-2" [formGroup]="userInfo">
<div class="col-md-6">
<label for="inputFirstName" class="form-label">Prénom</label>
<input type="text" class="form-control" id="inputFirstName" formControlName="firstName" readonly="readonly">
</div>
<div class="col-md-6">
<label for="inputLastName" class="form-label">Nom</label>
<input type="text" class="form-control" id="inputLastName" formControlName="lastName" readonly="readonly">
</div>
<div class="col-md-12">
<label for="inputEmail" class="form-label">Email</label>
<input type="email" class="form-control" id="inputEmail" formControlName="email" readonly="readonly">
</div>
</form>
</div>

View File

@ -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<PageAccountComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ PageAccountComponent ]
})
.compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(PageAccountComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});

View File

@ -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 : '']
})
}
}

View File

@ -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<User> | void {
const token = localStorage.getItem(this.tokenKey);
if(token) {
const decodedToken = jwt_decode<any>(token);
const userId = decodedToken.userId;
return this.http.get<User>(`${this.apiUrl}/user/${userId}`);
} else {
this.router.navigate(['signin']);
}
}
}