commit
25830750bd
15 changed files with 11763 additions and 42 deletions
|
|
@ -41,9 +41,9 @@ export class AuthService {
|
|||
|
||||
return this.http.post(`${this.apiUrl}/membres/sign-in`, body).pipe(
|
||||
map((x: any) => {
|
||||
console.log('Service : ', x.accessToken);
|
||||
console.log('Service : ', x.token);
|
||||
// Modification à faire ici
|
||||
localStorage.setItem(this.tokenKey, x.accessToken);
|
||||
localStorage.setItem(this.tokenKey, x.token);
|
||||
return x; // permet de renvoyer la réponse à l'initiateur (page Signin) après le traitement du map
|
||||
})
|
||||
);
|
||||
|
|
|
|||
16
src/app/services/menus.service.spec.ts
Normal file
16
src/app/services/menus.service.spec.ts
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
import { TestBed } from '@angular/core/testing';
|
||||
|
||||
import { MenusService } from './menus.service';
|
||||
|
||||
describe('MenusService', () => {
|
||||
let service: MenusService;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({});
|
||||
service = TestBed.inject(MenusService);
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(service).toBeTruthy();
|
||||
});
|
||||
});
|
||||
51
src/app/services/menus.service.ts
Normal file
51
src/app/services/menus.service.ts
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
import { Injectable } from '@angular/core';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { map, Observable } from 'rxjs';
|
||||
import { environment } from 'src/environments/environment';
|
||||
import { Menu } from '../models/menu';
|
||||
import { Router } from '@angular/router';
|
||||
import { TokenService } from './token.service';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class MenusService {
|
||||
|
||||
apiUrl: string;
|
||||
|
||||
|
||||
constructor(private http: HttpClient, private router: Router, private tokenService: TokenService) {
|
||||
// On se sert des variables d'environnement de notre application
|
||||
this.apiUrl = environment.apiUrl;
|
||||
}
|
||||
|
||||
//on affiche les menus d'une team
|
||||
getMenu(): Observable<any> | void {
|
||||
const teamId = this.tokenService.getCurrentTeamId();
|
||||
if (teamId){
|
||||
return this.http.get(`${this.apiUrl}/menus/team/${teamId}`);
|
||||
}else {
|
||||
this.router.navigate(['accueil']);
|
||||
}
|
||||
}
|
||||
|
||||
getMenuById(id: any): Observable<any> {
|
||||
return this.http.get(`${this.apiUrl}/menus/` + id);
|
||||
}
|
||||
|
||||
addMenu(menu: Menu): Observable<any> {
|
||||
console.log(menu);
|
||||
|
||||
return this.http.post(`${this.apiUrl}/menus/add`, menu);
|
||||
}
|
||||
|
||||
deleteMenu(id: any): Observable<any> {
|
||||
return this.http.delete(`${this.apiUrl}/menus/delete/`+ id);
|
||||
}
|
||||
|
||||
updateMenu(menu: Menu): Observable<any> {
|
||||
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…
Add table
Add a link
Reference in a new issue