Get google oauth access token and implement google refresh token

Hey everyone! šŸ‘‹ I'm working on implementing authentication in my Next.js app using BetterAuth with Google OAuth, and I could use some help with two things: How can I retrieve the user's access_token after login? What's the best way to implement token refresh with BetterAuth in a Next.js setup? If anyone has experience with this or can point me to some solid resources or examples, I’d really appreciate it! šŸ™ Thanks in advance! šŸ’™
6 Replies
Ping
Ping•2d ago
How can I retrieve the user's access_token after login?
You can query your DB
What's the best way to implement token refresh with BetterAuth in a Next.js setup?
I could be wrong but I think we're in the makings for a better api regarding this. @bekacru ?
cupskrrtt
cupskrrttOP•2d ago
You can query your DB
Is this the only way, if it is alright i can use somekind of caching to reduce db stress
I could be wrong but I think we're in the makings for a better api regarding this.
Well currently for this i create a temporary way to refresh the oauth2 access token, let me know if you guys already implement the better way to do this thanks for the response
KiNFiSH
KiNFiSH•20h ago
for the accessToken , it is stored on account Table along with refreshToken. you can use the internalAdapter for findAccount and get the access token for there . if you could get an access to ctx you can do this way
// this is by userID
const result = await ctx.context.internalAdapter.findAccounts(userId)

// this is by accountID
const result = await ctx.context.internalAdapter.findAccount(accountId)
// this is by userID
const result = await ctx.context.internalAdapter.findAccounts(userId)

// this is by accountID
const result = await ctx.context.internalAdapter.findAccount(accountId)
yeah for the refresh token , you can lets say i used twitter, i can do sth like this .. the url might not correct but you can pass your way of refreshing access token.
twitter: {
clientId: process.env.TWITTER_CLIENT_ID || "",
clientSecret: process.env.TWITTER_CLIENT_SECRET || "",
async refreshAccessToken(refreshToken) {
const response = await fetch("https://api.twitter.com/2/oauth2/token", {
method: "POST",
body: new URLSearchParams({
grant_type: "refresh_token",
refresh_token: refreshToken,
}),
});
const data = await response.json();
return {
accessToken: data.access_token,
refreshToken: data.refresh_token,
};
},
},
twitter: {
clientId: process.env.TWITTER_CLIENT_ID || "",
clientSecret: process.env.TWITTER_CLIENT_SECRET || "",
async refreshAccessToken(refreshToken) {
const response = await fetch("https://api.twitter.com/2/oauth2/token", {
method: "POST",
body: new URLSearchParams({
grant_type: "refresh_token",
refresh_token: refreshToken,
}),
});
const data = await response.json();
return {
accessToken: data.access_token,
refreshToken: data.refresh_token,
};
},
},
cupskrrtt
cupskrrttOP•20h ago
for the refresh access token when is the function gonna run,
KiNFiSH
KiNFiSH•20h ago
if you use your authClient for calling to authClient.refreshToken()
cupskrrtt
cupskrrttOP•19h ago
aight thanks man

Did you find this page helpful?