passport-facebook icon indicating copy to clipboard operation
passport-facebook copied to clipboard

Pop up window to do authentication in?

Open hyperh opened this issue 8 years ago • 11 comments

Is it possible to have a new window open when you do the /auth/facebook call, and then do the authentication in that window, have it close, then redirect your original window?

hyperh avatar Dec 08 '16 04:12 hyperh

Figured it out. Here's my code in case anyone wants to do the same.

// server/routes
app.get('/auth/facebook', passport.authenticate('facebook', { display: 'popup' }));
app.get('/auth/facebook/callback',
  passport.authenticate('facebook', { successRedirect: '/auth/facebook/success', failureRedirect: '/login' }),
);
// client/routes
<Route path="auth/facebook/success" component={AuthSuccess} />
// AuthSuccess.js
import React, { Component } from 'react';

export default class AuthSuccess extends Component {
  componentDidMount() {
    const url = '/private';
    window.opener.open(url, '_self');
    window.opener.focus();
    window.close();
  }

  render() {
    return (
      <div>
        AUTH SUCCESS!
      </div>
    );
  }
}

const url = '/auth/facebook';
const name = 'facebook_login';
const specs = 'width=500,height=500';
window.open(url, name, specs);

hyperh avatar Dec 14 '16 00:12 hyperh

hello! in the last chunk of code... where do you define the url, name, specs...?

johnandblue avatar May 03 '17 17:05 johnandblue

Nice solution @hyperh!

@johnandblue, the last four lines refer to the original "connect to facebook" button you display in your UI to kick off the auth flow. Mine looks the same, the only difference being const name = '_blank' so it opens in a new window (along with the display: 'popup' in the FacebookStrategy config)

pfarnach avatar May 18 '17 23:05 pfarnach

Hi @hyperh @pfarnach

I am trying to do window.open with the login url and doing the authentication in the new window in React app. I dint understand as to how I could close the window or show some message to the user when the login request in the new window is successful, as I don't have any handle to it

Basically I have the reference to the new window inside my React app, but it will be helpful if you could tell me how I could use it to listen to the events or the login request success and use it to show some message to the user or close that window?

audupa avatar Oct 30 '17 12:10 audupa

Glad I found this, it helped a lot with my OAuth integrations for Github/GHE! Thank you!

ccampanale avatar Dec 21 '17 16:12 ccampanale

Thank you

otaree avatar May 19 '18 09:05 otaree

What about the authorization token? where do you store it after a successful redirect?

numediaweb avatar Feb 09 '19 11:02 numediaweb

Thank you. Awesome solution.

baranimurthy avatar Feb 28 '19 10:02 baranimurthy

This is a a wonderful solution, Thank you so much for this.

DayanaLorza avatar Apr 25 '20 00:04 DayanaLorza

Note that if you want the popup window to be centered on the screen, you can replace the specs variable with this:

var width = 470, height = 580;
var w = window.outerWidth - width, h = window.outerHeight - height;
var left = Math.round(window.screenX + (w / 2));
var top = Math.round(window.screenY + (h / 2.5));
const specs = `width=${width},height=${height},left=${left},top=${top},toolbar=0,scrollbars=0,status=0,resizable=0,location=0,menuBar=0`;

(not sure if the other specs are needed, but I left them in, as they were part of the gist here)

Also note: If you want to use a new-tab rather than a popup, I think you just replace name = 'facebook_login' with name = '_blank', and omit the specs argument. (and maybe remove the display: 'popup' line in server code)

Venryx avatar Dec 06 '21 13:12 Venryx

As of 2023 it doesn't work. No popup or anything

Dagge11 avatar Aug 07 '23 08:08 Dagge11