1
0
mirror of https://github.com/sasjs/adapter.git synced 2025-12-11 09:24:35 +00:00
Files
adapter/src/auth/refreshTokensForSasjs.ts
2023-06-22 13:37:07 +03:00

46 lines
1.2 KiB
TypeScript

import { prefixMessage } from '@sasjs/utils/error'
import { RequestClient } from '../request/RequestClient'
import { getTokenRequestErrorPrefix } from './getTokenRequestErrorPrefix'
import { ServerType } from '@sasjs/utils/types'
/**
* Exchanges the refresh token for an access token for the given client.
* @param requestClient - the pre-configured HTTP request client
* @param refreshToken - the refresh token received from the server.
*/
export async function refreshTokensForSasjs(
requestClient: RequestClient,
refreshToken: string
) {
const url = '/SASjsApi/auth/refresh'
const headers = {
Authorization: 'Bearer ' + refreshToken
}
const authResponse = await requestClient
.post(url, undefined, undefined, undefined, headers)
.then((res) => {
const sasAuth = res.result as {
accessToken: string
refreshToken: string
}
return {
access_token: sasAuth.accessToken,
refresh_token: sasAuth.refreshToken
}
})
.catch((err) => {
throw prefixMessage(
err,
getTokenRequestErrorPrefix(
'refreshing tokens',
'refreshTokensForSasjs',
ServerType.Sasjs,
url
)
)
})
return authResponse
}