When implementing Google Login using NodeJS Passport

Asked 1 years ago, Updated 1 years ago, 55 views

app.get('/auth/google/callback',   passport.authenticate('google'));

If you do this, it works well.

If you try to log it, it will emit an error.

app.get('/auth/google/callback', (req, res) => {
    console.dir( 'called' );
    passport.authenticate('google')(req,res);
});

UnhandledPromiseRejectionWarning: TypeError: next is not a function

When you do this, your browser keeps spinning around the Google page.

app.get('/auth/google/callback', (req, res) => {
    console.dir( 'called' );
    passport.authenticate('google');
});

node.js passport

2022-09-22 13:38

1 Answers

I'll give you the answer to you... This is because the official document is not intended/suggested usage. First of all, if you look at Passport's official document about authenticate, you can see this.

The function of authenticate() is in the standard Connect middleware format, making it easy to write as routing middleware in the Express web app.

Then we can look at Express's app.get() method to see if we can call something back and forth that we call. Now, to answer question number 1, you can do this (according to official document .

app.get('/auth/google/callback',
    function (req, res) {
        console.dir('calling');
    },
    passport.authenticate('google'),
    function (req, res) {
        console.dir('called');
    }
);


2022-09-22 13:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.