Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(auth): jwt-decode from v2 to v4 #1527

Merged
merged 13 commits into from
Dec 6, 2023
13 changes: 8 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
"jspdf": "^2.5.1",
"jspdf-autotable": "^3.5.29",
"jszip": "^3.10.1",
"jwt-decode": "^2.2.0",
"jwt-decode": "^4.0.0",
"moment": "^2.29.4",
"ngx-color": "^9.0.0",
"ngx-indexed-db": "^11.0.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
}
},
"dependencies": {
"jwt-decode": "^2.2.0",
"jwt-decode": "^4.0.0",
"ts-cacheable": "^1.0.6",
"ts-md5": "^1.3.0",
"tslib": "^2.6.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/auth/src/lib/shared/auth.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class AuthInterceptor implements HttpInterceptor {
headers: req.headers.set('Authorization', authHeader)
});

const tokenDecoded: any = this.tokenService.decode();
const tokenDecoded = this.tokenService.decode();
if (
authReq.params.get('_i') === 'true' &&
tokenDecoded &&
Expand Down
5 changes: 3 additions & 2 deletions packages/auth/src/lib/shared/auth.interface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { MsalGuardConfiguration } from '@azure/msal-angular';
import { BaseUser } from '@igo2/core';

import { MsalGuardConfiguration } from '@azure/msal-angular';
import { BrowserAuthOptions } from '@azure/msal-browser';

export interface AuthInternOptions {
Expand Down Expand Up @@ -102,7 +103,7 @@ export interface User extends BaseUser {
sourceId?: string;
locale?: string;
isExpired?: boolean;
admin?: boolean;
isAdmin?: boolean;
defaultContextId?: string;
pelord marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down
5 changes: 3 additions & 2 deletions packages/auth/src/lib/shared/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { catchError, tap } from 'rxjs/operators';
import { globalCacheBusterNotifier } from 'ts-cacheable';

import { AuthOptions, IInfosUser, User } from './auth.interface';
import { IgoJwtPayload } from './token.interface';
import { TokenService } from './token.service';

@Injectable({
Expand Down Expand Up @@ -108,11 +109,11 @@ export class AuthService {
return this.tokenService.get();
}

decodeToken() {
decodeToken(): IgoJwtPayload | undefined {
if (this.isAuthenticated()) {
return this.tokenService.decode();
}
return false;
return;
}

goToRedirectUrl() {
Expand Down
1 change: 1 addition & 0 deletions packages/auth/src/lib/shared/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './token.service';
export * from './token.interface';
export * from './auth.service';
export * from './auth.interface';
export * from './auth.interceptor';
Expand Down
7 changes: 7 additions & 0 deletions packages/auth/src/lib/shared/token.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { JwtPayload } from 'jwt-decode';

import { User } from './auth.interface';

export interface IgoJwtPayload extends JwtPayload {
user: User;
}
7 changes: 4 additions & 3 deletions packages/auth/src/lib/shared/token.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { Injectable, Injector } from '@angular/core';

import { ConfigService } from '@igo2/core';

import jwtDecode from 'jwt-decode';
import { jwtDecode } from 'jwt-decode';

import { AuthOptions } from './auth.interface';
import { IgoJwtPayload } from './token.interface';

@Injectable({
providedIn: 'root'
Expand All @@ -31,12 +32,12 @@ export class TokenService {
return localStorage.getItem(this.tokenKey);
}

decode() {
decode(): IgoJwtPayload {
const token = this.get();
if (!token) {
return;
}
return jwtDecode(token);
return jwtDecode(token) as IgoJwtPayload;
alecarn marked this conversation as resolved.
Show resolved Hide resolved
}

isExpired() {
Expand Down
1 change: 1 addition & 0 deletions packages/auth/src/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export * from './lib/shared/auth.interceptor';
export * from './lib/shared/auth.interface';
export * from './lib/shared/auth-microsoft.provider';
export * from './lib/shared/protected.directive';
export * from './lib/shared/token.interface';
export * from './lib/shared/token.service';
export * from './lib/shared/auth-storage.interface';
export * from './lib/shared/auth-storage.service';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ export class ShareMapApiComponent implements OnInit {
ngOnInit(): void {
this.auth.authenticate$.subscribe((auth) => {
const decodeToken = this.auth.decodeToken();
this.userId = decodeToken.user ? decodeToken.user.id : undefined;
this.userId = decodeToken?.user
? decodeToken.user.id.toString()
: undefined;
alecarn marked this conversation as resolved.
Show resolved Hide resolved
this.buildForm();
});
}
Expand Down
12 changes: 3 additions & 9 deletions packages/core/src/lib/analytics/shared/analytics.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Injectable } from '@angular/core';

import { User } from '@igo2/auth';
alecarn marked this conversation as resolved.
Show resolved Hide resolved

import { ConfigService } from '../../config/config.service';
import { AnalyticsOptions } from './analytics.interface';

Expand Down Expand Up @@ -45,15 +47,7 @@ export class AnalyticsService {
})();
}

public setUser(
user?: {
id: number;
sourceId?: string;
firstName?: string;
lastName?: string;
},
profils?: string[]
) {
public setUser(user?: User, profils?: string[]) {
alecarn marked this conversation as resolved.
Show resolved Hide resolved
if (this.options.provider === 'matomo') {
if (!user) {
this.paq.push(['resetUserId']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ export class AnalyticsListenerService {

listenUser() {
this.authService.authenticate$.subscribe(() => {
const tokenDecoded = this.authService.decodeToken() || {};
if (tokenDecoded.user) {
const tokenDecoded = this.authService.decodeToken();
if (tokenDecoded?.user) {
this.authService
.getProfils()
.subscribe((profils) =>
Expand Down
Loading