Skip to content

Instantly share code, notes, and snippets.

@PotatoPope
Last active June 5, 2024 11:29
Show Gist options
  • Save PotatoPope/8763ee032673c020670fa821ad1a2583 to your computer and use it in GitHub Desktop.
Save PotatoPope/8763ee032673c020670fa821ad1a2583 to your computer and use it in GitHub Desktop.
Authsession
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