You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Popup stays open after authentication succeeds #21
I got this problem trying to implement Twitter login button in reactjs.
After clicking on the login button and logging in successfully the popup window was not closed. I took a close look at react-twitter-auth-component.js and found out popup.location was blank (line 413). Is it just me or anyone else has encountered the same error?
Reading around it looks like you can't access the popup the way this component was built. Only way I could think of to get around that was to use a socket to kick back the info to the client once you have authenticated successfully:
// Serverconstexpress=require('express')constpassport=require('passport')const{Strategy: TwitterStrategy}=require('passport-twitter')constcors=require('cors')constsession=require('express-session')constapp=express()constserver=require('http').createServer(app)constio=require('socket.io')(server)passport.use(newTwitterStrategy({consumerKey: 'your key',consumerSecret: 'your secret',// have to set this callback url on apps.twitter.comcallbackURL: 'http://127.0.0.1:8080/twitter/callback',},(req,token,tokenSecret,profile,cb)=>cb(null,profile)))passport.serializeUser((user,cb)=>cb(null,user))passport.deserializeUser((obj,cb)=>cb(null,obj))app.use(cors({origin: 'http://localhost:3000'}))app.use(session({secret: 'KeyboardKitty',resave: true,saveUninitialized: true}))app.use(express.json())app.use(passport.initialize())app.use(passport.session())app.set('socketio',io)io.sockets.on('connection',socket=>{socket.on('room',room=>{socket.join(room)})})constaddSocketToSession=(req,res,next)=>{req.session.socket=req.query.socketnext()}consttwitterAuth=passport.authenticate('twitter')app.get('/twitter',addSocketToSession,twitterAuth)app.get('/twitter/callback',twitterAuth,(req,res)=>{constio=req.app.get('socketio')io.sockets.in(req.session.socket).emit('user',req.user.username)res.send('all done!')})server.listen(8080)
Well in this example you are using wrong Node.js library. In tutorial and in example I have used passport-twitter-token and in this example you are using passport-twitter. That is big difference. If you are using that library, then yes, you would need web sockets or something similar.
Its also possible to use what I wrote with passport-twitter-token and without the sockets. You just send the callback to localhost:3000 like you did in your example. It feels convoluted bouncing back and forth to the server like that and writing a bunch of requests on the server that passport is set up to do in the first place. Sockets is just a different approach, I guess.
Hi from the future. I just had the same issue. Not sure if this helps but make sure your callback URL has the same origin as your app that opens the popup window, otherwise it won't be able to access the popup's properties and the popup will stay open. https://developer.mozilla.org/en-US/docs/Web/API/Window/open#Return_value
Activity
funador commentedon May 21, 2018
Reading around it looks like you can't access the popup the way this component was built. Only way I could think of to get around that was to use a socket to kick back the info to the client once you have authenticated successfully:
ivanvs commentedon May 21, 2018
Well in this example you are using wrong Node.js library. In tutorial and in example I have used
passport-twitter-tokenand in this example you are usingpassport-twitter. That is big difference. If you are using that library, then yes, you would need web sockets or something similar.funador commentedon May 21, 2018
Its also possible to use what I wrote with passport-twitter-token and without the sockets. You just send the callback to localhost:3000 like you did in your example. It feels convoluted bouncing back and forth to the server like that and writing a bunch of requests on the server that passport is set up to do in the first place. Sockets is just a different approach, I guess.
jonesty commentedon May 21, 2019
Hi from the future. I just had the same issue. Not sure if this helps but make sure your callback URL has the same origin as your app that opens the popup window, otherwise it won't be able to access the popup's properties and the popup will stay open. https://developer.mozilla.org/en-US/docs/Web/API/Window/open#Return_value