How to establish Multi Domain Google SSO between NodeJS app and WordPress
How to establish Multi Domain Google SSO between NodeJS app and WordPress
I have Google SSO implemented in ReactJS/ExpressJS App and I want to make the user when logs in this Node App to be registered as well in the WordPress website
I have here in this section the data that comes from React to ExpressJS and that works fine, so I want to when the user register/login to be directed to another WP website and find him self registered there also, I added another rule in route.post but it doesn't work
routesGoogle.post("/login", googleLogin, secondGoogle);
export async function googleLogin(req, res) {
try {
const { body } = req;
// await verifyGoogle();
// After successfully creating tshe user, send the data to Zapier
const zapierResponse = await axios.post('https://hooks.zapier.com/hooks/catch/39045/3annwb9/', {
name: body.user.name,
email: body.user.email,
date: new Date().toISOString() // Current date in ISO format
});
// Check if the Zapier webhook call was successful
if (zapierResponse.status !== 200) {
console.error('Failed to send data to Zapier', zapierResponse.data);
}
const { user: googleUser } = body;
let user = await userModel.findOne({ email: googleUser.email });
if (!user) {
user = await createUser(googleUser);
}
const token = await createToken(user, process.env.JWT_SECRET, '7d');
res.redirect("https://XXXXXXX.shop"); // I added this but doesn't work
return res.json({ token });
} catch (error) {
console.error(error);
return res.status(500).json({ message: "Internal Server Error" }); // Improved error message
}
}
WordPress
// In your WordPress plugin or theme's functions.php
// Add an endpoint to handle token-based authentication
add_action('rest_api_init', function () {
register_rest_route('custom-plugin', '/authenticate', array(
'methods' => 'POST',
'callback' => 'authenticate_user',
));
});
// Callback function for handling authentication
function authenticate_user($request) {
$data = $request->get_json_params();
// Implement your WordPress authentication logic here
// Check if the provided token is valid
if ($token_is_valid) {
return rest_ensure_response(array('message' => 'Authentication successful'));
} else {
return rest_ensure_response(array('error' => 'Invalid token'), 401);
}
}
Turbo Multi-language Translator
Make the better internet purchasing globaly
Turbosify SEO Speed Booster
5.0 (7)•Free plan available
Get better conversions by improving store loading speed
Installed
Turbo Multi-language Chat - AI Customer service in one hand