Fix login token guard
This commit is contained in:
parent
8b4304b910
commit
f0b9d27cee
2 changed files with 5 additions and 1 deletions
|
|
@ -30,6 +30,9 @@ export function LoginPage() {
|
|||
|
||||
try {
|
||||
const { token } = await authService.login({ username, password })
|
||||
if (!token) {
|
||||
throw new Error('Resposta de login inválida. Verifique o usuário e a senha.')
|
||||
}
|
||||
const payload = decodeJwtPayload<{ role?: string }>(token)
|
||||
const role = resolveRole(payload?.role)
|
||||
login(token, role, username)
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
const toBase64 = (value: string) =>
|
||||
value.replace(/-/g, '+').replace(/_/g, '/').padEnd(Math.ceil(value.length / 4) * 4, '=')
|
||||
|
||||
export const decodeJwtPayload = <T extends Record<string, unknown>>(token: string): T | null => {
|
||||
export const decodeJwtPayload = <T extends Record<string, unknown>>(token?: string): T | null => {
|
||||
if (!token) return null
|
||||
const [, payload] = token.split('.')
|
||||
if (!payload) return null
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue