how can I sing in a user using google oauth from sever side?
socialProviders: {
google: {
clientId: process.env.GOOGLE_CLIENT_ID as string,
clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
// prompt: "select_account", // optional: always show account picker
// accessType: "offline", // optional: to get refresh token
},
},
FYI the client side (authClient) works fine. However the server side auth instance returns this error.
1 Reply
in FE use: const response = await authClient.signIn.social({
provider: "google",
callbackURL:
/,
// errorCallbackURL: "/auth/login?error=google-auth-failed",
});
in BE:
@Post('sign-in/social')
async googleSignIn(@Req() req: Request, @Res() res: Response) {
try {
this.logger.log('Google sign-in request received');
// Check if required environment variables are set
if (!process.env.GOOGLE_CLIENT_ID || !process.env.GOOGLE_CLIENT_SECRET) {
this.logger.error('Google OAuth environment variables are not set');
return res.status(500).json({
error: 'Google OAuth configuration is missing',
message: 'GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET must be set',
});
}
// Handle Google social sign-in
const data = await auth.api.signInSocial({
body: {
provider: 'google',
},
});
this.logger.log('Google sign-in successful');
// Set cookies if provided in response
return res.status(200).json(data);
} catch (error) {
this.logger.error('Error in Google sign-in:', error);
return res.status(500).json({
error: 'Google sign-in failed',
message: error.message,
});
}
}