Alwaysblue
Alwaysblue

Reputation: 11830

Using Google function with Node-express-passport

I have used passport-twitter strategy and it so happens that I am unable to see passport-login window on my client side when I execute my stratergy

So this is what I am doing, initially

class Auth extends ParentClass {
  constructor(context, options) {
    super() 
    this.app.get('/callback/:service', (req, res) => 
    this.callbackEndpoint(req,res))
    this.app.get('/', (req, res, next) => this.loginEndpoint(req, res, next))
}

  async loginEndpoint (req, res, next) {
      if (req.query.Twitter) {
        console.log(`Inside Twitter Authentication`)
        passport.authenticate('twitter', { scope : ['email'] })(req,res,next);
      }

}

where in my ParentClass I am more or less initialising stuff

class ParentClass {
   this.use(corsMiddleware(options.CORS_URLS))
    this.use(bodyParser.json())
    this.use(session({
      secret: 'keyboard cat',
      resave: false,
      saveUninitialized: true,
      cookie: { secure: true }
    }))
    this.use(passport.initialize())
    this.use((passport.session()))
}

use (middleware) {
    this.app.use(middleware)
  }
}

And finally, this is my passport stratergy

passport.use(new TwitterStrategy({
    consumerKey: functions.config().twitterCredentials.apikey,
    consumerSecret: functions.config().twitterCredentials.apiSecretKey,
    callbackURL: redirect_uri,
    passReqToCallback: true
  }, async (req, accessToken, refreshToken, params, profile, done) => {
      console.log(`logging from Passport Twitter ${req.workspace, req.accessToken}`)
        done()
}))

From the first code, snippet I see the log Inside Twitter Authentication but thereafter nothing happens.

What I was expecting? A window to show up for me to log into twitter but nothing shows up and after sometime I get this in my console info: Execution took 569847 ms, finished with status: 'timeout'

So here are my Questions: 1. Can we use passport and normal oauth flow with google-functions? (just comment if not answer) 2. If yes, can we use spot the error in my above code.

Update note: While Googling for my problem, I stumbled upon this old question and realised that while it was somewhat similar but the description for the previous one was very vague. I asked a similar question later today but decided to merge it with this one.

Upvotes: 1

Views: 562

Answers (1)

Amol J
Amol J

Reputation: 172

In the first part of your code, you need to pass passport.authenticate directly to the get handler. Like this:

class Auth extends ParentClass {
  constructor(context, options) {
    super() 
    this.app.get('/callback/:service', (req, res) => 
    this.callbackEndpoint(req,res))
    this.app.get('/', passport.authenticate('twitter', { scope : ['email'] })(req,res,next))
  }
}

I assume this "App" is mounted at some path like /auth/twitter

In your front end, you open a popup window with URL pointing to /auth/twitter. Something like this:

<button onclick="oauthPrompt('twitter')">Login with Twitter</button>
<script>
    function oauthPrompt(service) {
      const url = '/auth/' + service
      var newWindow = window.open(url, 'name', 'height=600,width=450')
      if (window.focus) {
        newWindow.focus();
      }
    }

</script>

Upvotes: 1

Related Questions