github
245 of 402 branches covered (60.95%)
Branch coverage included in aggregate %.
765 of 1002 new or added lines in 14 files covered. (76.35%)
14 existing lines in 3 files now uncovered.3556 of 4115 relevant lines covered (86.42%)
5.37 hits per line
|
import { jwtDecode } from 'jwt-decode'; |
1✔ |
|
|
1✔ |
|
// Define interface for the decoded token
|
1✔ |
|
interface DecodedToken { |
1✔ |
|
id: string; |
1✔ |
|
email: string; |
1✔ |
|
userType: string; |
1✔ |
|
iat: number;
|
1✔ |
|
exp: number;
|
1✔ |
|
} |
1✔ |
|
|
1✔ |
|
export const decodedToken = (): DecodedToken | null => { |
|
|
// Fetch token from localStorage
|
9✔ |
|
const userToken: string | null = localStorage.getItem('userToken'); |
9✔ |
|
|
9✔ |
|
if (!userToken) {
|
9✔ |
|
console.error('No user token found in localStorage'); |
9✔ |
|
return null;
|
9✔ |
|
} |
|
NEW
|
|
× |
NEW
|
try {
|
× |
NEW
|
const parsedToken = JSON.parse(userToken);
|
× |
|
if (!parsedToken || !parsedToken.token) {
|
|
NEW
|
console.error('Invalid token structure'); |
× |
NEW
|
return null;
|
× |
NEW
|
} |
× |
NEW
|
const decoded: DecodedToken = jwtDecode(parsedToken.token as string); |
× |
NEW
|
return decoded;
|
× |
NEW
|
} catch (error) {
|
× |
NEW
|
console.error('Error decoding token', error); |
× |
NEW
|
return null;
|
× |
NEW
|
} |
× |
|
}; |
9✔ |