W
Waspβ€’3mo ago
Andrea

Refresh token is null

Hey! I am using the SaaS template with Google login. I would like to retrieve the refresh token in the onAfterLogin hook so that I can later authenticate to other Google libraries. Unfortunately, the refresh token seems to always be null, what am I doing wrong? Am I missing a configuration?
14 Replies
kapa.ai
kapa.aiβ€’3mo ago
Hi @Andrea. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Hmm, I don't know enough to give you a confident answer yet. The knowledge sources provided do not contain information about retrieving the refresh token in the onAfterLogin hook with the SaaS template and Google login. However, it's important to ensure that your Google related configurations are correctly set up. You can check your userSignupFields and configFn in your main.wasp file and make sure they match the correct implementation as shown in this source (https://discord.com/channels/686873244791210014/1222916615151161374). Also, ensure that your environment variables for Google Client ID and Google Client Secret are correctly set in your .env.server file as shown in this documentation (https://wasp-lang.dev/docs/auth/social-auth/google#4-adding-environment-variables). If you're still having issues, I recommend reaching out to the Wasp community or checking the Wasp documentation for more information. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @Andrea, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
Andrea
AndreaOPβ€’3mo ago
For more context, the login process works fine, it's juste the refresh token that is missing
miho
mihoβ€’3mo ago
I think that Google only gives you the refresh token on first login/signup. You add this extra param to the URL via the onBeforeOAuthRedirect to make Google give you the refresh token on subsequent logins as well: https://stackoverflow.com/questions/66536611/get-google-oauth-refresh-token-after-first-login I've gotten it to work successfully, but I don't have the code at hand, sorry πŸ˜„
Stack Overflow
get google oauth refresh token after first login
I have implemented a google oauth signup. I can request the tokens with the code provided when the user clicks login via google. However, the tokens only include the refresh token the very first time
Ramon Schmidt
Ramon Schmidtβ€’3mo ago
Hi @Andrea did you fix it? I'm trying to get the access token also, to save it and use it in my server app. Could you please have some tips to share? @miho I'll be glad if you have something too πŸ™‚
Andrea
AndreaOPβ€’3mo ago
Ah I see, thanks! Let me try if I'm able to get it on first login/signup, I'll let you know!
Ramon Schmidt
Ramon Schmidtβ€’3mo ago
I already have created an onAfterLogin hook
export const onAfterLogin: OnAfterLoginHook = async ({ providerId, user, oauth, req}) => {

console.log('xxxx', providerId, oauth);

export const onAfterLogin: OnAfterLoginHook = async ({ providerId, user, oauth, req}) => {

console.log('xxxx', providerId, oauth);

and added it to auth:{methods like that:
onAfterLogin: import { onAfterLogin } from "@src/auth/hooks",
onAfterLogin: import { onAfterLogin } from "@src/auth/hooks",
then inside oauth param i can access tokens: { accessToken: 'asduahsdiuhasdaisdasd' refreshToken: null, accessTokenExpiresAt: 2024-10-07T20:36:52.622Z, tokenId: asduhasdhuasd now i need to know how to refresh it, when it's necessary
Andrea
AndreaOPβ€’3mo ago
I had to do this: export const onBeforeOAuthRedirect: OnBeforeOAuthRedirectHook = async ({ url, }) => { return { url: new URL(${url}&prompt=consent&access_type=offline) } }
Ramon Schmidt
Ramon Schmidtβ€’3mo ago
I'll try it! Thank you @Andrea
MEE6
MEE6β€’3mo ago
Wohooo @Ramon Schmidt, you just became a Waspeteer level 1!
martinsos
martinsosβ€’3mo ago
Nice! @miho is this something that might make sense for us to document in our docs or is it too specific?
miho
mihoβ€’3mo ago
This is not Wasp specific but this example might be useful for people to get the general concept πŸ™‚
martinsos
martinsosβ€’3mo ago
I will let you decide then if we should be adding this to our docs or learning materials
miho
mihoβ€’3mo ago
Could you pls create an issue so I don't remember to deal with this since I'm technically vacationing πŸ˜‚
Want results from more Discord servers?
Add your server