Skip to content

Commit 5e7c316

Browse files
committed
2 parents 2ff264e + e16ad57 commit 5e7c316

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

src/callback-endpoint.ts

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,32 @@ export const createCallbackEndpoint = (
4242
// obtain access token
4343
// /////////////////////////////////////
4444

45-
const tokenResponse = await fetch(pluginOptions.tokenEndpoint, {
46-
method: "POST",
47-
headers: {
48-
"Content-Type": "application/x-www-form-urlencoded",
49-
Accept: "application/json",
50-
},
51-
body: new URLSearchParams({
52-
code,
53-
client_id: pluginOptions.clientId,
54-
client_secret: pluginOptions.clientSecret,
55-
redirect_uri: redirectUri,
56-
grant_type: "authorization_code",
57-
}).toString(),
58-
});
59-
const tokenData = await tokenResponse.json();
60-
const access_token = tokenData?.access_token;
45+
let access_token: string;
46+
47+
if (pluginOptions.getToken) {
48+
access_token = pluginOptions.getToken(code);
49+
} else {
50+
const tokenResponse = await fetch(pluginOptions.tokenEndpoint, {
51+
method: "POST",
52+
headers: {
53+
"Content-Type": "application/x-www-form-urlencoded",
54+
Accept: "application/json",
55+
},
56+
body: new URLSearchParams({
57+
code,
58+
client_id: pluginOptions.clientId,
59+
client_secret: pluginOptions.clientSecret,
60+
redirect_uri: redirectUri,
61+
grant_type: "authorization_code",
62+
}).toString(),
63+
});
64+
const tokenData = await tokenResponse.json();
65+
66+
access_token = tokenData?.access_token;
67+
}
68+
6169
if (typeof access_token !== "string")
62-
throw new Error(`No access token: ${JSON.stringify(tokenData)}`);
70+
throw new Error(`No access token: ${access_token}`);
6371

6472
// /////////////////////////////////////
6573
// get user info

src/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ export interface PluginTypes {
111111
*/
112112
callbackPath?: string;
113113

114+
/**
115+
* Function to get token from the OAuth providers.
116+
* If its not provided default will be used.
117+
*/
118+
getToken?: (code: string) => string;
119+
114120
/**
115121
* Redirect users after successful login.
116122
*/

0 commit comments

Comments
 (0)