Last active
June 5, 2024 11:29
-
-
Save PotatoPope/8763ee032673c020670fa821ad1a2583 to your computer and use it in GitHub Desktop.
Authsession
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function authorize( | |
): Promise<Token> { | |
const authReqConfig: AuthSession.AuthRequestConfig = { | |
clientId: CONST.AUT.CLIENT_ID, | |
codeChallengeMethod: CodeChallengeMethod.S256, | |
extraParams: { | |
realm: CONST.AUT.REALM, | |
acr_values: CONST.AUT.ACR, | |
prompt: "Login", | |
}, | |
redirectUri: CONST.AUT.REDIRECT_URI, | |
scopes: ["openid", "scopes"], | |
usePKCE: true, | |
}; | |
const discoveryConfig: AuthSession.DiscoveryDocument = { | |
authorizationEndpoint: CONST.AUT.AUTENDPOINT, | |
tokenEndpoint: CONST.AUT.TOKENENDPOINT, | |
}; | |
const request = new AuthSession.AuthRequest(authReqConfig); | |
return request | |
.promptAsync(discoveryConfig, { | |
// @ts-expect-error | |
preferEphemeralSession: true, | |
createTask: false, | |
}) | |
.then(async (response) => { | |
switch (response.type) { | |
case "success": { | |
const { code } = response.params; | |
const accessTokenReq: AuthSession.AccessTokenRequest = | |
new AuthSession.AccessTokenRequest({ | |
clientId: CONST.AUT.CLIENT_ID, | |
code: code, | |
redirectUri: CONST.AUT.REDIRECT_URI, | |
extraParams: { | |
code_verifier: request?.codeVerifier | |
? request.codeVerifier | |
: "", | |
}, | |
}); | |
const responseToken: AuthSession.TokenResponse = | |
await AuthSession.exchangeCodeAsync(accessTokenReq, { | |
tokenEndpoint: discoveryConfig.tokenEndpoint, | |
}); | |
return responseToken; | |
} | |
case "dismiss": { | |
throw new Error("User dismissed the login prompt") | |
} | |
case "error": { | |
throw new Error("Error during login"); | |
} | |
} | |
}) | |
.catch((error) => { | |
throw new Error("Error during login", error); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment