How to get OAuth2 Access Token in API request using custom provider configuration #871
Replies: 1 comment
-
This is how you can do token rotation if you don't use a database. UPDATE: This has been updated, assuming that you use Hi. You should probably check out the Our identity provider sent an access token in the Here is how I do it //...
callbacks: {
async jwt(prevToken, account, profile) {
// Signing in
if (account && profile) {
return {
accessToken: account.access_token,
accessTokenExpires: Date.now() + account.expires_in * 1000,
refreshToken: account.refresh_token,
user: profile,
}
}
// Subsequent use of JWT, the user has been logged in before
// access token has not expired yet
if (Date.now() < prevToken.accessTokenExpires) {
return prevToken
}
// access token has expired, try to update it
return refreshAccessToken(prevToken)
},
async session(session, token) {
if (token) {
session.user = token.user
session.accessToken = token.accessToken
}
return session
}
}
//...
/**
* Takes a token, and returns a new token with updated
* `accessToken` and `accessTokenExpires`. If an error occurs,
* returns the old token and an error property
*/
async function refreshAccessToken(token) {
try {
const url = `https://${process.env.IDS_DOMAIN}/connect/token`
const response = await fetch(url, {
body: new URLSearchParams({
client_id: process.env.IDS_CLIENT_ID,
client_secret: process.env.IDS_CLIENT_SECRET,
grant_type: "refresh_token",
refresh_token: token.refreshToken,
}),
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
method: "POST",
})
const tokens = await response.json()
if (!response.ok) {
throw tokens
}
return {
...token,
accessToken: tokens.access_token,
accessTokenExpires: Date.now() + refreshToken.expires_in * 1000,
refreshToken: tokens.refresh_token,
}
} catch (error) {
console.error(error)
return {
...token,
error: "RefreshAccessTokenError",
}
}
} #951 is the most up-to-date PR that should solve this issue without needing a user-land solution, but we would like to give a feature that solves token rotation to everyone, (both db and non-db, single and multiple provider users), which needs some thinking first. |
Beta Was this translation helpful? Give feedback.
-
Your question
How do I get the OAuth2
access_token
in API request for custom provider configuration?What are you trying to do
I have an app that is authenticating users with an oAuth2 service (Salesforce) and then will make requests on there behave.
I have configured next-auth so that I am successfully able to login using the "custom provider"
Now I need to pull data from the salesforce REST API using the access token which is returned by the oAuth request. However, it's unclear how I can get the access token in my
API
service.Seems like maybe JWT needs to be setup so the token can securely be stored on the client side? Unfortunately I'm not finding any clear documentation or examples of this.
Feedback
Documentation refers to searching through online documentation, code comments and issue history. The example project refers to next-auth-example.
Beta Was this translation helpful? Give feedback.
All reactions