So first, This is my Twitter Developer User authentication settings:
I am trying to use the Twitter passport OAuth2 package provided by @superfaceai in my NestJS backend, I don’t mind to change the package as long as it works;
This is my Twitter Strategy that I use in my authentication module:
@Injectable()
export class XTwitterStrategy extends PassportStrategy(Strategy, 'twitter') {
constructor() {
console.log(process.env.X_CLIENT_ID);
console.log(process.env.X_CLIENT_SECRET);
console.log(encodeURIComponent(process.env.X_CALLBACK_URL));
super({
clientID: process.env.X_CLIENT_ID,
clientSecret: process.env.X_CLIENT_SECRET,
callbackURL: encodeURIComponent(process.env.X_CALLBACK_URL),
clientType: 'confidential',
});
}
async validate(
_accessToken: string,
_refreshToken: string,
profile: any,
done: any,
): Promise<void> {
const { emails, displayName, photos } = profile;
console.log(profile);
const user: OAuthUser = {
provider: 'xtwitter',
email: emails[0].value,
name: displayName,
avatar: photos[0].value,
};
done(null, user);
}
}
This is my .env
:
X_CLIENT_ID=...
X_CLIENT_SECRET=...
X_CALLBACK_URL=http://127.0.0.1:3000/auth/twitter/callback
And this is my authentication controller:
@Public()
@Get('twitter')
@UseGuards(XTwitterOAuthGuard)
public async twitterAuth(): Promise<void> {}
@Public()
@Get('twitter/callback')
@UseGuards(XTwitterOAuthGuard)
public async twitterAuthCallback(
@Req() req: Request,
@Res() res: Response,
): Promise<void> {
// the register/login handling goes here, i removed the function code to make things shorter, it doesn't get here anyways.
}
}
I also added the express-session middleware to my main file:
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
}),
);
Everything looks good to me, so I am not sure where things go wrong, they don’t show any relevant error messages/codes, all I get is the ‘Something went wrong’ screen;
I would appreciate if anyone point me to what I’m doing wrong here, I did the Google OAuth in the same way and it worked perfectly. Thank you in advance !