mirror of
https://github.com/MAGICGrants/campaign-site.git
synced 2026-01-09 12:27:59 -05:00
fix(auth): use fetch instead of axios when fetching refresh token due to edge runtime compatibility
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user