Reisepass: andere Weiterleitung für Login und Account-Registrierung
Ich verwende das Passport-Modul (Github-Authentifizierung) in meiner App und möchte je nach Aktion umleiten ... Ich überprüfe, ob es sich nur um eine normale Anmeldung handelt oder ob sich der Benutzer zum ersten Mal anmeldet.
<code>passport.use(new GitHubStrategy({ clientID: conf.github.app_id, clientSecret: conf.github.app_secret, callbackURL: conf.github.callback_url }, function(accessToken, refreshToken, profile, done) { // asynchronous verification, for effect... process.nextTick(function () { // To keep the example simple, the user's GitHub profile is returned to // represent the logged-in user. In a typical application, you would want // to associate the GitHub account with a user record in your database, // and return that user instead. Models_User.findOrCreateUser(profile, function(msg){ console.log("auth type:" + msg); }); return done(null, profile); }); } )); </code>
In meiner Funktion findOrCreateUser überprüfe ich, ob es sich um einen neuen Benutzer handelt, und führe die gesamte DB-Aktion aus. Zum Testen lasse ich die Funktion eine msg-Variable zurückgeben, die nur eine Zeichenfolge ist, die "login" oder "new_registration" enthält.
Meine Frage ist also, wie ich die Variable "transportieren" soll, die ich von findOrCreateUser erhalte, damit ich sie nach Abschluss der Passauthentifizierung entsprechend umleiten kann ("/ welcome" oder "/ back_again").
den anderen Passcode in meiner App:
<code>// GET /auth/github // Use passport.authenticate() as route middleware to authenticate the // request. The first step in GitHub authentication will involve redirecting // the user to github.com. After authorization, GitHubwill redirect the user // back to this application at /auth/github/callback app.get('/auth/github', passport.authenticate('github'), //passport.authenticate('github', { scope: ['user', 'public_repo', 'gist'] }), function(req, res){ // The request will be redirected to GitHub for authentication, so this // function will not be called. }); // GET /auth/github/callback // Use passport.authenticate() as route middleware to authenticate the // request. If authentication fails, the user will be redirected back to the // login page. Otherwise, the primary route function function will be called, // which, in this example, will redirect the user to the home page. app.get('/auth/github/callback', passport.authenticate('github', { successRedirect: '/', failureRedirect: '/login' }), function(req, res) { res.redirect('/'); }); </code>