javascript - What is the cause of the InternalOAuthError in my sails.js passport.js implementation? -
i'm trying implement passport.js authentication in sails.js
app, using google oauth2.0 strategy. have considered using sails-generate-auth
, sails-auth
no longer supported. have considered waterlock
works local, twitter , facebook strategies.
the google()
function below called when user presses 'login google+' button. expected behaviour user gets redirected google page, prompted authenticate themselves. in actuality, following error logged @ marked line. user object undefined @ point.
internaloautherror: failed obtain request token (status: 307 data: <html> <head> <title>temporary redirect</title> </head> <body bgcolor="#ffffff" text="#000000"> <h1>temporary redirect</h1> document has moved <a href="https://accounts.google.com/oauthgetrequesttoken">here</a>. </body> </html> )
the function below located in authcontroller
, gets called upon clicking 'login google+' button.
google(request, response) { const google = sails.config.oauth.google; passport.use(new googlestrategy({ consumerkey: my_key, consumersecret: my_secret, callbackurl: 'http://localhost:1337/auth/callback/google', }, (token, tokensecret, profile, done) => { //verify callback: never appears called. user.findorcreate({ providerid: profile.id, provider: 'google' }).then((user) => { request.session.me = user; return done(user); }).catch(error => { sails.log('failed log in using google: ' + error); }); } )); passport.authenticate('google', { failureredirect: '/login', scope: [ 'profile', ], }, function(error, user) { //gets called, user undefined. if (error) { sails.log('err: ' + error); //<== error gets logged here. } request.login(user, function(error) { if (error) { sails.log('err: ' + error); response.view('500'); return; } res.redirect('/'); }); })(request, response); },
misc information: passport initialized in config/policies.js
:
'*': [ passport.initialize(), passport.session(), ],
my question: cause of error i'm getting?
i ran same issue using express, passport, , passport-google-oath. solved switching topassport-google-oauth20.
i not 100% sure if why, google seems have dropped oauth support quite while back.
important: oauth 1.0 officially deprecated on april 20, 2012, , no longer supported. encourage migrate oauth 2.0 possible.
you'll have change strategy to:
var googlestrategy = require('passport-google-oauth20').strategy;
passport.use(new googlestrategy({ clientid: google_client_id, clientsecret: google_client_secret, callbackurl: "http://www.example.com/auth/google/callback" }, function(accesstoken, refreshtoken, profile, cb) { user.findorcreate({ googleid: profile.id }, function (err, user) { return cb(err, user); }); } ));
Comments
Post a Comment