token service
This commit is contained in:
parent
a2fd96e6a4
commit
6e68de9f3a
@ -32,7 +32,7 @@ import { FicheContactComponent } from './components/fiche-contact/fiche-contact.
|
|||||||
import { PaginationComponent } from './components/pagination/pagination.component';
|
import { PaginationComponent } from './components/pagination/pagination.component';
|
||||||
import { CreneauComponent } from './components/creneau/creneau.component';
|
import { CreneauComponent } from './components/creneau/creneau.component';
|
||||||
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
|
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
|
||||||
import { HttpClientModule } from '@angular/common/http';
|
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
|
||||||
import { PageAjoutContactComponent } from './pages/page-ajout-contact/page-ajout-contact.component';
|
import { PageAjoutContactComponent } from './pages/page-ajout-contact/page-ajout-contact.component';
|
||||||
import { PageModifierContactComponent } from './pages/page-modifier-contact/page-modifier-contact.component';
|
import { PageModifierContactComponent } from './pages/page-modifier-contact/page-modifier-contact.component';
|
||||||
import { PageCreationTeamComponent } from './pages/page-creation-team/page-creation-team.component';
|
import { PageCreationTeamComponent } from './pages/page-creation-team/page-creation-team.component';
|
||||||
@ -89,7 +89,9 @@ import { EmoticonComponent } from './emoticon/emoticon.component';
|
|||||||
FormsModule,
|
FormsModule,
|
||||||
AutofocusFixModule.forRoot(),
|
AutofocusFixModule.forRoot(),
|
||||||
],
|
],
|
||||||
providers: [],
|
providers: [
|
||||||
|
|
||||||
|
],
|
||||||
bootstrap: [AppComponent],
|
bootstrap: [AppComponent],
|
||||||
})
|
})
|
||||||
export class AppModule {}
|
export class AppModule {}
|
||||||
|
@ -3,8 +3,8 @@ import { HttpClient } from '@angular/common/http';
|
|||||||
import { map, Observable } from 'rxjs';
|
import { map, Observable } from 'rxjs';
|
||||||
import { environment } from 'src/environments/environment';
|
import { environment } from 'src/environments/environment';
|
||||||
import { Menu } from '../models/menu';
|
import { Menu } from '../models/menu';
|
||||||
import jwt_decode from 'jwt-decode';
|
|
||||||
import { Router } from '@angular/router';
|
import { Router } from '@angular/router';
|
||||||
|
import { TokenService } from './token.service';
|
||||||
|
|
||||||
@Injectable({
|
@Injectable({
|
||||||
providedIn: 'root'
|
providedIn: 'root'
|
||||||
@ -12,25 +12,18 @@ import { Router } from '@angular/router';
|
|||||||
export class MenusService {
|
export class MenusService {
|
||||||
|
|
||||||
apiUrl: string;
|
apiUrl: string;
|
||||||
tokenKey: string;
|
|
||||||
|
|
||||||
|
|
||||||
constructor(private http: HttpClient, private router: Router) {
|
constructor(private http: HttpClient, private router: Router, private tokenService: TokenService) {
|
||||||
// On se sert des variables d'environnement de notre application
|
// On se sert des variables d'environnement de notre application
|
||||||
this.apiUrl = environment.apiUrl;
|
this.apiUrl = environment.apiUrl;
|
||||||
this.tokenKey = environment.tokenKey;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//on affiche les menus d'une team
|
//on affiche les menus d'une team
|
||||||
getMenu(): Observable<any> | void {
|
getMenu(): Observable<any> | void {
|
||||||
//on récupère l'id de la team grâce au token du membre :
|
const teamId = this.tokenService.getCurrentTeamId();
|
||||||
const token = localStorage.getItem(this.tokenKey);
|
if (teamId){
|
||||||
if(token){ //s'il y a un token tu le decode
|
|
||||||
const decodedToken = jwt_decode<any>(token);
|
|
||||||
const teamId = decodedToken.teamId;//tu vas chercher le teamId
|
|
||||||
console.log(teamId); //dans le json du token
|
|
||||||
return this.http.get(`${this.apiUrl}/menus/team/${teamId}`);
|
return this.http.get(`${this.apiUrl}/menus/team/${teamId}`);
|
||||||
//on reconstruit alors l'url pour afficher les menus via le team_id
|
|
||||||
}else {
|
}else {
|
||||||
this.router.navigate(['accueil']);
|
this.router.navigate(['accueil']);
|
||||||
}
|
}
|
||||||
@ -46,12 +39,12 @@ tokenKey: string;
|
|||||||
return this.http.post(`${this.apiUrl}/menus/add`, menu);
|
return this.http.post(`${this.apiUrl}/menus/add`, menu);
|
||||||
}
|
}
|
||||||
|
|
||||||
deleteMenu(menu: Menu): Observable<any> {
|
deleteMenu(id: any): Observable<any> {
|
||||||
return this.http.delete(`${this.apiUrl}/menus/delete/1`);
|
return this.http.delete(`${this.apiUrl}/menus/delete/`+ id);
|
||||||
}
|
}
|
||||||
|
|
||||||
updateMenu(menu: Menu): Observable<any> {
|
updateMenu(menu: Menu): Observable<any> {
|
||||||
return this.http.put(`${this.apiUrl}/contacts/update/1`, menu);
|
return this.http.put(`${this.apiUrl}/contacts/update/`, menu);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
16
src/app/services/token.service.spec.ts
Normal file
16
src/app/services/token.service.spec.ts
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
import { TestBed } from '@angular/core/testing';
|
||||||
|
|
||||||
|
import { TokenService } from './token.service';
|
||||||
|
|
||||||
|
describe('TokenService', () => {
|
||||||
|
let service: TokenService;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
TestBed.configureTestingModule({});
|
||||||
|
service = TestBed.inject(TokenService);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should be created', () => {
|
||||||
|
expect(service).toBeTruthy();
|
||||||
|
});
|
||||||
|
});
|
45
src/app/services/token.service.ts
Normal file
45
src/app/services/token.service.ts
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
import { Injectable } from '@angular/core';
|
||||||
|
import { environment } from 'src/environments/environment';
|
||||||
|
import jwt_decode from 'jwt-decode';
|
||||||
|
|
||||||
|
@Injectable({
|
||||||
|
providedIn: 'root'
|
||||||
|
})
|
||||||
|
export class TokenService {
|
||||||
|
tokenKey = environment.tokenKey;
|
||||||
|
|
||||||
|
constructor() { }
|
||||||
|
|
||||||
|
public getToken(): string | null {
|
||||||
|
const token = localStorage.getItem(this.tokenKey);
|
||||||
|
if(token) {
|
||||||
|
return token;
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public getCurrentMembreId(): number | null {
|
||||||
|
const token = this.getToken();
|
||||||
|
if(token) {
|
||||||
|
const decodedToken = jwt_decode<any>(token);
|
||||||
|
const userId = decodedToken.sub;
|
||||||
|
return userId;
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public getCurrentTeamId(): number | null {
|
||||||
|
const token = this.getToken();
|
||||||
|
if(token){
|
||||||
|
const decodedToken = jwt_decode<any>(token);
|
||||||
|
const teamId = decodedToken.teamId;
|
||||||
|
return teamId ;
|
||||||
|
|
||||||
|
}else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user