mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-05 11:40:06 +00:00
feat: get access token & refresh tokens for server type SASJS
This commit is contained in:
36
src/auth/getAccessTokenForSasjs.ts
Normal file
36
src/auth/getAccessTokenForSasjs.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { prefixMessage } from '@sasjs/utils/error'
|
||||
import { RequestClient } from '../request/RequestClient'
|
||||
|
||||
/**
|
||||
* Exchanges the auth code for an access token for the given client.
|
||||
* @param requestClient - the pre-configured HTTP request client
|
||||
* @param clientId - the client ID to authenticate with.
|
||||
* @param authCode - the auth code received from the server.
|
||||
*/
|
||||
export async function getAccessTokenForSasjs(
|
||||
requestClient: RequestClient,
|
||||
clientId: string,
|
||||
authCode: string
|
||||
) {
|
||||
const url = '/SASjsApi/auth/token'
|
||||
const data = {
|
||||
clientId,
|
||||
code: authCode
|
||||
}
|
||||
|
||||
return await requestClient
|
||||
.post(url, data, undefined)
|
||||
.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, 'Error while getting access token. ')
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user