DEV Community

Cover image for How to implement Facebook Login with NestJS

How to implement Facebook Login with NestJS

King Elisha on August 20, 2020

NestJS is a relatively new framework that has been growing in popularity for many good reasons. It has full support for TypeScript and it combines ...
Collapse
 
eduwr profile image
Eduardo Wronscki • • Edited

you are my hero! Thank you for this article, it was very very useful...

Collapse
 
noorul_amin_d76066b12282 profile image
Noor Ul Amin •

when i git the localhost:3000/auth/facebook in the browser i do get a login screen by facebook. After login i do get an error from facebook:
Image description

Collapse
 
hoangdaochuz profile image
hoangdaochuz •

I have a similar issue, but I solve it by adding an email scope on facebook app

Image description
I hope this one can help you guys

Collapse
 
tahirmahmudzade profile image
Tahir Mahmudzade •

hey did you find the solution for this? I've been getting the exact same error, even though I have tried everything, I'm still stuck with this

Collapse
 
charly_machine profile image
Charles Maldon •

Excellent article, thank you very much for sharing it; it’s been a great help.
As you explain, I can create a user via Facebook login or retrieve a user previously created in this way. But when I try to use a domain with https instead of localhost:3000, I always get these errors:
[Nest] 96 - 29/07/2026, 14:43:02 ERROR [ExceptionsHandler] FacebookTokenError: This authorisation code has already been used.
in Strategy.parseErrorResponse (/opt/render/project/src/node_modules/passport-facebook/lib/strategy.js:198:12)
in OAuth2Strategy._createOAuthError (/opt/render/project/src/node_modules/passport-oauth2/lib/strategy.js:420:16)
in /opt/render/project/src/node_modules/passport-oauth2/lib/strategy.js:177:45
in /opt/render/project/src/node_modules/oauth/lib/oauth2.js:196:18
in passBackControl (/opt/render/project/src/node_modules/oauth/lib/oauth2.js:132:9)
in IncomingMessage.<anonymous> (/opt/render/project/src/node_modules/oauth/lib/oauth2.js:157:7)
in IncomingMessage.emit (node:events:520:35)
in endReadableNT (node:internal/streams/readable:1729:12)
in process.processTicksAndRejections (node:internal/process/task_queues:90:21) {
type: “OAuthException”,
code: 100,
subcode: 36009,
traceID: “Af10pfooI0nWFe3jGSfUFBg”,
status: 500
}

The domain is included in Meta Developer App, just as I had to do with localhost:3000.

Could you please provide more information on what needs to be done?

Collapse
 
yueyuuta profile image
YueYuuta • • Edited

I got an error, apparently it does not return the name and email, some help

dev-to-uploads.s3.amazonaws.com/i/...

Collapse
 
usmanashraf678 profile image
Usman Ashraf •

I think this shows up if the email of user is not verified.

Collapse
 
andyogaga1 profile image
Andy Ogaga •

Wonderful article Sir....

Collapse
 
slavik_semen_be52899bfbcc profile image
Slavik Semen •

Thank you.
How protect other routes ?
@Get("/products")
@UseGuards(AuthGuard("facebook")) ?

if i have also Google guard it will be like:
@Get("/products")
@UseGuards(AuthGuard("facebook"))
@UseGuards(AuthGuard("google"))

Collapse
 
embpdaniel profile image
Daniel Montano •

Amazing article! One thing to note is if your local front-end runs on a separate port you will have CORS errors with Facebook. To resolve, the front-end MUST use the same port as your server (localhost:3000 in this case) so you'll need to proxy your request made on front-end to get rid of the errors.

Collapse
 
budazimbud profile image
BudAzimbud •

then how to implement logout ??

Collapse
 
linhtran28 profile image
linhtran28 •

Thank for your sharing, may i ask how can i implement facebook login with graphql project? is it possible without @Req annotation?

Collapse
 
devlugo profile image
Rodolfo Lugo •

Hello @linhtran28 , did you found a way to implemenent this?

Collapse
 
bogdy234 profile image
Filimon Bogdan •

Any news on this?

Collapse
 
quangkhaidev profile image
Đỗ Quang Khải •

Thank King Elisha, hope you can create a lot of post like this <3

Collapse
 
gopalesagar profile image
gopalesagar •

Good article. I have a question regarding the facebookLoginRedirect function. How are you able to access req.user as req is an Express object?

Collapse
 
podrimczech profile image
Faton •

To anyone getting the "Sorry, something went wrong". Go to facebook app and allow "email" as a scope.

Collapse
 
hassannazir profile image
hassannazir •

I am getting this error:
InternalOAuthError: Failed to obtain access token