page account
This commit is contained in:
parent
b771677562
commit
c80b74eb7f
@ -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},
|
||||
|
@ -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,
|
||||
|
@ -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>
|
||||
|
@ -1,5 +1,3 @@
|
||||
import { RoleList } from "./roleList";
|
||||
|
||||
export interface User {
|
||||
id?:number;
|
||||
prenom: string;
|
||||
|
16
src/app/pages/page-account/page-account.component.html
Normal file
16
src/app/pages/page-account/page-account.component.html
Normal 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>
|
25
src/app/pages/page-account/page-account.component.spec.ts
Normal file
25
src/app/pages/page-account/page-account.component.spec.ts
Normal 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();
|
||||
});
|
||||
});
|
36
src/app/pages/page-account/page-account.component.ts
Normal file
36
src/app/pages/page-account/page-account.component.ts
Normal 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 : '']
|
||||
})
|
||||
}
|
||||
}
|
@ -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']);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user