import passport from 'passport' import { Strategy as GitHubStrategy} from 'passport-github' import { Strategy as GoogleStrategy} from 'passport-google-oauth2' import { Strategy as JwtStrategy } from 'passport-jwt' export type StrategyConfig = { clientID: string clientSecret: string scope: string[] } type UserProfile = {} type AuthState = { request: any token: string refreshToken: string profile: UserProfile } type AuthSuccessFunction = (input: { request: any token: string refreshToken: string profile: UserProfile }) => Promise type AuthConfig = { onSuccess?: AuthSuccessFunction callbackURL: string strategies: { Github: StrategyConfig } } passport.serializeUser(function(user, done) { done(null, user); }) passport.deserializeUser(function(user, done) { done(null, user); }) const opts = { jwtFromRequest: (req) => { const { headers: { authorization, ['x-api-key']: apiKey } } = req return authorization ?? apiKey; }, secretOrKey:process.env.JWT_SECRET, issuer: process.env.JWT_ISSUER, audience: process.env.JWT_AUDIENCE, } passport.use(new JwtStrategy(opts, function(jwt_payload, done) { // User.findOne({id: jwt_payload.sub}, function(err, user) { // if (err) { // return done(err, false); // } // if (user) { // return done(null, user); // } else { // return done(null, false); // // or you could create a new account // } // }); done(null, jwt_payload) })); export const AUTH_CONFIG = { callbackURL: process.env.AUTH_CALLBACK_URL, strategies: { github: { clientID: process.env.GITHUB_ID, clientSecret: process.env.GITHUB_SECRET, scope: ['identity', 'profile'] }, google: { clientID: process.env.GOOGLE_ID, clientSecret: process.env.GOOGLE_SECRET, scope: [ "phone", "email", "openid", "profile", ] }, }, onSuccess: async (authState: AuthState) => { // console.log('AA', authState) } } export const StrategyMap = { github: GitHubStrategy, google: GoogleStrategy, }