In a normal node rest API it will be easy for us to call the passport in the routes itself. Since I'm using serverless lambda for rest API, it doesn't have any routers. Instead from serverless.yml file I can call the handlers alone and calling passport.authenticate from that place is not returning any response properly. How to use passportjs in nodejs with serverless lambda?
I have tried both callback and return promise method.
serverless.yml
google:
handler: handlers/user.google
events:
- http:
path: /auth/google
method: get
cors: true
googlecallback:
handler: handlers/user.googlecallback
events:
- http:
path: /auth/google/callback
method: get
cors: true
handlers/user.js
cost passport = require("passport"),
GoogleStrategy = require("passport-google-oauth").OAuth2Strategy;
passport.use(
new GoogleStrategy(
{
clientID: config.google.clientId,
clientSecret: config.google.clientSecrect,
callbackURL: "http://localhost:3000/dev/auth/google/callback",
},
function (accessToken, refreshToken, profile, done) {
return done(err, profile);
}
)
);
module.exports.google = async (event, context, callback) => {
context.callbackWaitsForEmptyEventLoop = false;
return callback(null,passport.authenticate("google", {
scope: ["https://www.googleapis.com/auth/plus.login"],
}));
}
module.exports.googlecallback = async (event, context) => {
context.callbackWaitsForEmptyEventLoop = false;
return new Promise((resolve, reject) => {
return resolve(passport.authenticate("google", { successRedirect: '/', failureRedirect: "/login" }))
})
}