import * as passport from 'passport';
import { IUser, LinkedinAuth } from 'passport-linkedin-api-v2';
class LinkedInAuthController {
public initialize() {
passport.use('linkedin', this.getStrategy());
return passport.initialize();
}
private getStrategy = (): passport.Strategy => {
const params = {
clientID: 'CLIENT_ID',
clientSecret: 'CLIENT_SECRET',
callbackURL: 'http://127.0.0.1:3000/auth/linkedin',
scope: ['r_emailaddress', 'r_liteprofile'],
};
return new LinkedinAuth(params,
(accessToken: string, refreshToken: string, profile: any, done: any) => {
profile.accessToken = accessToken;
done(profile);
});
};
private authenticate = (strategy: string, state: string, callback: any) => passport.authenticate(
strategy,
{
state,
session: false,
failWithError: true,
passReqToCallback: true,
},
callback);
public authenticateUser = (req: any, res: any) => {
this.authenticate('linkedin', 'custom_state', async (profile: any) => {
if (!profile || profile.id == null || profile.accessToken == null) {
throw 'Authentication failed';
}
const user = await this.getUser(profile.accessToken);
})(req, res);
};
}
export const linkedInAuthController = new LinkedInAuthController();
export const authenticateUser = linkedInAuthController.authenticateUser;