I've been grappling with this problem for quite some time now, but I just can't seem to wrap my head around how it functions.
There's a file named passport.ts
in my project which looks something like this:
passport.serializeUser<any, any>((user, done) => {
done(undefined, 1);
});
passport.deserializeUser((id, done) => {
done(null, 1);
});
// Strategy config
const GoogleStrategy = passportGoogle.Strategy
passport.use(new GoogleStrategy({
clientID: config.get('googleAuth.clientID'),
clientSecret: config.get('googleAuth.clientSecret'),
callbackURL: config.get('googleAuth.callbackURL'),
passReqToCallback: true
}, async (accessToken, refreshToken, profile, done)=> {
return done
}))
In the root directory of my project, there's an index.ts
file that initializes passport in the standard way:
app.use(passport.initialize());
app.use(passport.session());
I'm setting up controllers using express's Router function in different files, one of them being authenticate.ts
, which looks something like this:
import {Router} from 'express';
import passport from 'passport';
import config from 'config';
import * as passportConfig from '../utils/passport' //this is passport.ts mentioned above
const router = Router();
router.get('/google', passport.authenticate('google', {scope: config.get('googleAuth.scope')}));
I realize that I haven't included the callback route, and I've encountered difficulties while trying to make everything work because I'm not sure how to use the passport object defined in passport.ts with the strategy in different files.
Any assistance would be highly appreciated!