简体   繁体   English

如何在登录和注册社交之间做出不同的誓言流程? 像谷歌 oauth2.0 使用护照 js

[英]How can i make a different oath flow between login & signup for social? like google oauth2.0 using passport js

I want the Auth0 to distinguish between social signup and social login.我希望 Auth0 区分社交注册和社交登录。 If a new user tries to log in with social before signing up, AUTH0 should response with Error and will ask the user to signup first.如果新用户在注册前尝试使用社交登录,AUTH0 应响应错误并要求用户先注册。 Today, when a user logs in with Social without signing up, AUTH0 treats it as signup.今天,当用户在没有注册的情况下使用 Social 登录时,AUTH0 会将其视为注册。

exports.google_LogIn = function(req, res, next) {
  passport.authenticate("google", {
    scope: ["profile", "email"]
  })(req, res, next);
}

Your Passport strategy allows you to return with a user or an error.您的 Passport 策略允许您返回用户或错误。

For example this Google strategy will return an error if your database hasn't already seen the user before (using Mongoose ).例如,如果您的数据库之前没有见过该用户(使用Mongoose ),则此 Google 策略将返回错误。

passport.use(new GoogleStrategy({
    clientID: GOOGLE_CLIENT_ID,
    clientSecret: GOOGLE_CLIENT_SECRET,
    callbackURL: "http://www.example.com/auth/google/callback"
  },
  function(accessToken, refreshToken, profile, done) {
       User.findOne({ googleId: profile.id }, function (err, user) {
         if (!user) return done(Error("needs signup"), null)
         return done(err, user)
       });
  }
));

In the callback route you can then give Passport a failureRedirect to send the user to your signup page when there is an error.在回调路由中,您可以给 Passport 一个failureRedirect以便在出现错误时将用户发送到您的注册页面。

app.get('/auth/google/callback', 
  passport.authenticate('google', { failureRedirect: '/signUpPage' }),
  function(req, res) {
    res.redirect('/');
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM