fix(auth): use fetch instead of axios when fetching refresh token due to edge runtime compatibility

This commit is contained in:
Artur
2024-10-17 11:55:14 -03:00
parent 8a36fa6da8
commit 0a94df9e50

View File

@@ -1,4 +1,3 @@
import axios from 'axios'
import { JWT } from 'next-auth/jwt'
import { jwtDecode } from 'jwt-decode'
@@ -7,17 +6,28 @@ import { KeycloakJwtPayload } from '../types'
export async function refreshToken(token: JWT): Promise<JWT> {
try {
const { data: newToken } = await axios.post(
const response = await fetch(
`${env.KEYCLOAK_URL}/realms/${env.KEYCLOAK_REALM_NAME}/protocol/openid-connect/token`,
new URLSearchParams({
client_id: env.KEYCLOAK_CLIENT_ID,
client_secret: env.KEYCLOAK_CLIENT_SECRET,
grant_type: 'refresh_token',
refresh_token: token.refreshToken as string,
}),
{ headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }
{
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: new URLSearchParams({
client_id: env.KEYCLOAK_CLIENT_ID,
client_secret: env.KEYCLOAK_CLIENT_SECRET,
grant_type: 'refresh_token',
refresh_token: token.refreshToken as string,
}).toString(),
}
)
if (!response.ok) {
throw new Error(`Error: ${response.statusText}`)
}
const newToken = await response.json()
const jwtPayload: KeycloakJwtPayload = jwtDecode(newToken.access_token)
return {