如果用户使用 Passport OAuth2 (Swoop) 和 Node/Express 输入无效电子邮件,我该如何重定向用户?

How do I redirect a user if they enter an invalid email using Passport OAuth2 (Swoop) and Node/Express?

我正在我的项目中实施 Swoop (https://swoopnow.com/) 身份验证并检查用户输入的电子邮件类型。

举个例子,我只想让用户输入一个 gmail 帐户,而不是其他任何东西。

当用户输入允许的电子邮件类型 (gmail) 时,我在 Passport 的 done 功能中成功验证了他们和 return user

但是,当用户输入禁止使用的电子邮件类型(yahoo、outlook 等)时,我在 Passport 的 done 功能中 return false,它工作正常,但是我想将用户重定向到错误页面并让他们知道他们的电子邮件被禁止。

在 Swoop 中,屏幕上只显示一条 unauthorized 消息。

更新:这是我的工作代码(谢谢 Aviv Lo!)

passport.use('swoop', new OAuth2Strategy({
    authorizationURL: 'https://auth.swoop.email/oauth2/authorize',
    tokenURL: 'https://auth.swoop.email/oauth2/token',
    clientID: /* 'CLIENT_ID', */ 'swoop_fc8iff4kfmwffaz',
    clientSecret: /* 'CLIENT_SECRET', */ 'c2092c662280555d00ef4ca70e009bf88d8c3dcc86b70ca9bb17d6e9fc646dc9',
    callbackURL: 'http://localhost:3000/auth/swoop/callback'
  }, function(accessToken, refreshToken, params, profile, done) {
    let user = jwtDecode(params.id_token);

    let allowedEmailDomain = 'gmail.com';
    let userEmailDomain = user.email.split('@');

    if (userEmailDomain[1] === allowedEmailDomain) {
        done(null, user);
    } else {
        done(null, false);
    }
  }));

/*****************
* Auth Routes
*****************/
// Swoop Login Route
app.get('/auth/swoop', passport.authenticate('swoop', { scope: ['email'] }));

// Callback function after authentication occurs
app.get('/auth/swoop/callback', (req, res, next) => {
  passport.authenticate('swoop', { session: true }, (err, user) => {
    if (err) {
        return res.redirect('/error')
    }

    if (!user) { return res.redirect('/bad'); }

    req.logIn(user, (err) => {
        if (err) {
            return res.redirect('/error')
        }

        return res.redirect('/good');
    });
  })(req, res, next);
});

app.get('/logout', (req, res) => {
  req.logout();
  res.redirect('/');
});

/*****************
* Routes
*****************/
app.get('/', (req, res) => {
  res.send('Hello Swoop! \
  <div><a href="/auth/swoop">Login</a></div> \
  <div><a href="/logout">Logout</a></div>');
});

app.get('/good', (req, res, next) => {
  if(req.user) {
    res.send('This contains a secret. Welcome ' + req.user.email);
  } else {
    res.redirect('/');
  }
});

app.get('/bad', (req, res) => {
  res.send('Email Forbidden.');
});

app.get('/error', (req, res) => {
  res.send('Error.');
});

app.listen(port, () => console.log(`Swoop Demo listening on port ${port}!`));

可能正在考虑稍微更改您的代码。

app.get('/auth/swoop',(req,res,next)=>{

  //Using passport-local
    passport.authenticate('swoop', {scope: ['email']},(err, user) => {

        // If errors
        if (err) {
            return res.redirect('/bad')
        }

        //If user object does not exist => login failed
        if (!user) { return res.redirect('/bad'); }
        
        console.log((user));


        //If all good, log the dude in
        req.logIn(user, (err) => {
            
            // If errors
            if (err) {
                return res.redirect('/bad')
            }

            //Redirect the user to the correct page
            return res.redirect('/good')

        });

     

    })(req, res, next);


  

});
  passport.authenticate('swoop',(err, user) => {
     
    // Do some logic here to see if the user is logged in
    // otherwise just res.redirect the user here to somewhere you want
    // You will have access to res here because you wrap it in the route
    })(req, res, next)