Implement Google OAuth in NestJS using Passport Tosin Moronfolu on August 15, 2022 Introduction Authentication is an essential part of most applications. Implementing authentication in your application depends on requir... Read full post Collapse Expand Matias Matias Matias Follow Joined May 22, 2024 • May 22 '24 • Edited on May 22 • Edited Dropdown menu Copy link Hide @chukwutosin_ At this point how can I handle state? I need to pass a custom parameter from the frontend, and get this parameter on backend redirection. This is to save extra on my DB apart of the data google give me. Thank you so much! Collapse Expand Matias Matias Matias Follow Joined May 22, 2024 • May 23 '24 • Edited on May 24 • Edited Dropdown menu Copy link Hide I found a solution: In file: google.strategy.ts add this function. async authenticate(req: Request) { if (req.query.myData) { // /auth return super.authenticate('google', { state: req.query.myData, }); } // /auth/callback return super.authenticate(req); } Enter fullscreen mode Exit fullscreen mode In file: google.strategy.ts update constructor. constructor() { super({ clientID: '', clientSecret: '', callbackURL: '', scope: ['email', 'profile'], passReqToCallback: true, // <---- add this. }); } Enter fullscreen mode Exit fullscreen mode In file: google.strategy.ts update validate function. async validate( req: Request, // <---- add this. accessToken: string, refreshToken: string, profile: Profile, done: VerifyCallback, ): Promise<void> { ... console.log(req.query.state); // <----- Your data will be here. Enter fullscreen mode Exit fullscreen mode Collapse Expand Tosin Moronfolu Tosin Moronfolu Tosin Moronfolu Follow Code is my caffeine Email tosinmoronfolu@gmail.com Location Lagos, Nigeria Work Backend Developer Joined Jun 1, 2018 • Jul 30 '24 Dropdown menu Copy link Hide Cool Collapse Expand Huy Tran Huy Tran Huy Tran Follow Joined Mar 26, 2024 • Aug 31 '24 Dropdown menu Copy link Hide nice Collapse Expand Ganesh Patil Ganesh Patil Ganesh Patil Follow DevRel Engineer @usebruno Email ganeshpatil386386@gmail.com Location India Education Bachelor of CSE Work Computer Science Student Joined Oct 12, 2021 • Aug 15 '22 Dropdown menu Copy link Hide Well written @chukwutosin A great article consist well research and this is actually amazing! Collapse Expand Tosin Moronfolu Tosin Moronfolu Tosin Moronfolu Follow Code is my caffeine Email tosinmoronfolu@gmail.com Location Lagos, Nigeria Work Backend Developer Joined Jun 1, 2018 • Aug 15 '22 Dropdown menu Copy link Hide Thank you! I appreeciate the feedback. Collapse Expand Alex Karydis Alex Karydis Alex Karydis Follow Joined Nov 13, 2023 • Nov 25 '23 Dropdown menu Copy link Hide Thank you for this amazing tutorial! Can confirm that everything is up to date. Collapse Expand Tosin Moronfolu Tosin Moronfolu Tosin Moronfolu Follow Code is my caffeine Email tosinmoronfolu@gmail.com Location Lagos, Nigeria Work Backend Developer Joined Jun 1, 2018 • Jan 12 '24 Dropdown menu Copy link Hide There might be upgrades in the versions of the several tools used, but it should still work just fine Collapse Expand Abhik Banerjee Abhik Banerjee Abhik Banerjee Follow Web3 Dev, Foodie, Cats and Anime Lover rolled into one. Ping me up if you have any article topic recommendations ;) Email abhik@abhikbanerjee.com Location Kolkata, West Bengal, India. Joined Mar 28, 2020 • Aug 15 '23 Dropdown menu Copy link Hide This was helpful. Thanks! Collapse Expand Tosin Moronfolu Tosin Moronfolu Tosin Moronfolu Follow Code is my caffeine Email tosinmoronfolu@gmail.com Location Lagos, Nigeria Work Backend Developer Joined Jun 1, 2018 • Aug 15 '23 Dropdown menu Copy link Hide I'm glad you found it helpful. Thanks! Collapse Expand iGEORGE17 iGEORGE17 iGEORGE17 Follow Joined Jan 28, 2022 • Aug 16 '22 Dropdown menu Copy link Hide juss started learning nestJS, nice article Collapse Expand Tosin Moronfolu Tosin Moronfolu Tosin Moronfolu Follow Code is my caffeine Email tosinmoronfolu@gmail.com Location Lagos, Nigeria Work Backend Developer Joined Jun 1, 2018 • Aug 16 '22 Dropdown menu Copy link Hide Oh, that's cool! I'm glad you found it helpful Collapse Expand Dener Rocha Dener Rocha Dener Rocha Follow Joined Jun 1, 2018 • Oct 16 '22 Dropdown menu Copy link Hide How to integrate with an angular app ? Just call the route in nestjs ? Collapse Expand Tosin Moronfolu Tosin Moronfolu Tosin Moronfolu Follow Code is my caffeine Email tosinmoronfolu@gmail.com Location Lagos, Nigeria Work Backend Developer Joined Jun 1, 2018 • Oct 19 '22 Dropdown menu Copy link Hide yes. just call the endpoints like you normally would in a frontend app Collapse Expand Wakeup Flower Wakeup Flower Wakeup Flower Follow Girl dev who loves lifelong learning. Coding feels like magic Email phpgirlmsn@gmail.com Location France Education Made code work without knowing how; now I want the how Joined May 6, 2025 • Jun 18 Dropdown menu Copy link Hide Thank you Collapse Expand Jessé Azevêdo Jessé Azevêdo Jessé Azevêdo Follow My name is Jessé Azevêdo, I'm pharmaceutical and development. Joined Dec 22, 2023 • Dec 22 '23 Dropdown menu Copy link Hide Hi @chukwutosin_ , Thank you very much for your amazing How To! Collapse Expand Tosin Moronfolu Tosin Moronfolu Tosin Moronfolu Follow Code is my caffeine Email tosinmoronfolu@gmail.com Location Lagos, Nigeria Work Backend Developer Joined Jun 1, 2018 • Jan 12 '24 Dropdown menu Copy link Hide You're welcome! Collapse Expand mundzirmuin mundzirmuin mundzirmuin Follow Joined Aug 10, 2023 • Aug 10 '23 Dropdown menu Copy link Hide Thanks, I'll try implement this in my next project Collapse Expand Tosin Moronfolu Tosin Moronfolu Tosin Moronfolu Follow Code is my caffeine Email tosinmoronfolu@gmail.com Location Lagos, Nigeria Work Backend Developer Joined Jun 1, 2018 • Aug 15 '23 Dropdown menu Copy link Hide Great! Thanks Collapse Expand shifoo28 shifoo28 shifoo28 Follow Joined Nov 28, 2023 • Nov 28 '23 Dropdown menu Copy link Hide Well done, bro Collapse Expand Seghosimhe David Seghosimhe David Seghosimhe David Follow Backend Developer Education Bowen University Work Backend dev Joined Feb 16, 2022 • Aug 18 '22 Dropdown menu Copy link Hide When running locally, the first route does not take me to the google authentication page, is that how it's supposed to be on localhost? Collapse Expand Tosin Moronfolu Tosin Moronfolu Tosin Moronfolu Follow Code is my caffeine Email tosinmoronfolu@gmail.com Location Lagos, Nigeria Work Backend Developer Joined Jun 1, 2018 • Aug 18 '22 Dropdown menu Copy link Hide No, you probably missed a step. What route did you visit? Collapse Expand Seghosimhe David Seghosimhe David Seghosimhe David Follow Backend Developer Education Bowen University Work Backend dev Joined Feb 16, 2022 • Aug 18 '22 Dropdown menu Copy link Hide I created a route path in my user auth controller, localhost:3000//auth/google Does it have to be just localhost:3000/ Tosin Moronfolu Tosin Moronfolu Tosin Moronfolu Follow Code is my caffeine Email tosinmoronfolu@gmail.com Location Lagos, Nigeria Work Backend Developer Joined Jun 1, 2018 • Aug 18 '22 Dropdown menu Copy link Hide following the article. you just need to visit: localhost:3000/auth Seghosimhe David Seghosimhe David Seghosimhe David Follow Backend Developer Education Bowen University Work Backend dev Joined Feb 16, 2022 • Aug 18 '22 Dropdown menu Copy link Hide it works, It did not work previously because I tried to access it from my swagger documentation Seghosimhe David Seghosimhe David Seghosimhe David Follow Backend Developer Education Bowen University Work Backend dev Joined Feb 16, 2022 • Aug 18 '22 Dropdown menu Copy link Hide I have a question if you don't mind, In a case where you do not need a refreshToken, what other option can be passed to super() aside accessType? Tosin Moronfolu Tosin Moronfolu Tosin Moronfolu Follow Code is my caffeine Email tosinmoronfolu@gmail.com Location Lagos, Nigeria Work Backend Developer Joined Jun 1, 2018 • Aug 18 '22 Dropdown menu Copy link Hide Any other Google specific options can be passed there. Every other option can be found on Google's documentation which I linked at the end of the article Seghosimhe David Seghosimhe David Seghosimhe David Follow Backend Developer Education Bowen University Work Backend dev Joined Feb 16, 2022 • Aug 19 '22 Dropdown menu Copy link Hide Thanks Collapse Expand Seghosimhe David Seghosimhe David Seghosimhe David Follow Backend Developer Education Bowen University Work Backend dev Joined Feb 16, 2022 • Aug 18 '22 Dropdown menu Copy link Hide Thanks Collapse Expand Abdul Azeez Abdul Azeez Abdul Azeez Follow Citizen Of The World ! Location WonderLand! Joined Oct 9, 2021 • Feb 5 '23 Dropdown menu Copy link Hide Please @chukwutosin_ is this going to save to the database? Collapse Expand Tosin Moronfolu Tosin Moronfolu Tosin Moronfolu Follow Code is my caffeine Email tosinmoronfolu@gmail.com Location Lagos, Nigeria Work Backend Developer Joined Jun 1, 2018 • Mar 14 '23 Dropdown menu Copy link Hide There's no database setup here. Collapse Expand Thanh Trần Duy Thanh Trần Duy Thanh Trần Duy Follow Joined Jun 5, 2025 • Aug 31 Dropdown menu Copy link Hide amazing Code of Conduct • Report abuse For further actions, you may consider blocking this person and/or reporting abuse
@chukwutosin_ At this point how can I handle
state
?I need to pass a custom parameter from the frontend, and get this parameter on backend redirection.
This is to save extra on my DB apart of the data google give me.
Thank you so much!
I found a solution:
In file:
google.strategy.ts
add this function.In file:
google.strategy.ts
update constructor.In file:
google.strategy.ts
update validate function.Cool
nice
Well written @chukwutosin A great article consist well research and this is actually amazing!
Thank you! I appreeciate the feedback.
Thank you for this amazing tutorial! Can confirm that everything is up to date.
There might be upgrades in the versions of the several tools used, but it should still work just fine
This was helpful. Thanks!
I'm glad you found it helpful. Thanks!
juss started learning nestJS, nice article
Oh, that's cool! I'm glad you found it helpful
How to integrate with an angular app ? Just call the route in nestjs ?
yes. just call the endpoints like you normally would in a frontend app
Thank you
Hi @chukwutosin_ ,
Thank you very much for your amazing How To!
You're welcome!
Thanks, I'll try implement this in my next project
Great! Thanks
Well done, bro
When running locally, the first route does not take me to the google authentication page, is that how it's supposed to be on localhost?
No, you probably missed a step. What route did you visit?
I created a route path in my user auth controller, localhost:3000//auth/google
Does it have to be just localhost:3000/
following the article. you just need to visit: localhost:3000/auth
it works, It did not work previously because I tried to access it from my swagger documentation
I have a question if you don't mind, In a case where you do not need a refreshToken, what other option can be passed to super() aside accessType?
Any other Google specific options can be passed there. Every other option can be found on Google's documentation which I linked at the end of the article
Thanks
Thanks
Please @chukwutosin_ is this going to save to the database?
There's no database setup here.
amazing