xenial-black
xenial-black15mo ago

How do I pass invitation token with google auth to my backend?

import { CredentialResponse } from "../interfaces/google"; export const Login: React.FC = () => { const { mutate: login } = useLogin<CredentialResponse>(); //http://localhost:3000/login?to=%2Flogin%2F754c8673492ef41ba151b4ed526e74dab4cf6151c5126d89c43c0bee8f765aab const location = useLocation(); const queryParams = new URLSearchParams(location.search); const token = queryParams.get("to")?.split("/login/")[1]; console.log(token); const { loginWithRedirect } = useAuth0(); const GoogleButton = (): JSX.Element => { const divRef = useRef<HTMLDivElement>(null); useEffect(() => { if (typeof window === "undefined" !window.google !divRef.current) { return; } try { window.google.accounts.id.initialize({ ux_mode: "popup", client_id: "*-.apps.googleusercontent.com", callback: async (res: CredentialResponse) => { if (res.credential) { res.token = token; login(res); } }, }); window.google.accounts.id.renderButton(divRef.current, { theme: "filled_blue", size: "medium", type: "standard", }); } catch (error) { console.log(error); } }, []); // you can also add your client id as dependency here return <div ref={divRef} />; }; return ( ..... <Box mt={4}> <GoogleButton /> </Box> ); }; The token what it should be passed to my backend. and call this function: router.route('/login/:token').post(createUser); I tested with postman and from backend part it was working well and created the user I expected
4 Replies
kapa.ai
kapa.ai15mo ago
Hi @acidias. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
kapa.ai
kapa.ai15mo ago
---------------------- Hmm, I don't know enough to give you a confident answer yet. The provided code and information are not related to refine. However, if you have any questions about refine, I'd be happy to help. You can also check out the refine documentation here (https://refine.dev/docs) and blogs here (https://refine.dev/blog) for more information. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
Overview | refine
What is refine?
Blog | refine
A Docusaurus powered blog!
kapa.ai
kapa.ai15mo ago
<@134090892167479296> kapa.ai is still learning and improving, please let us how it did by reacting below
xenial-black
xenial-black15mo ago
I have an invitations table in db, which being checked in my createUser function and if the token is valid it's takes the invitators ID and pass to the new user as it parentUserId. So it's kinda become a subuser I tried out my createUser function with postman and its worked correctly, however I don't know how to pass the token from frontend with refine.
const createUser = async (req, res) => {
try {
const { name, email, avatar, token } = req.body; // Get the token from the request body

const userExist = await User.findOne({ email });

if (userExist) return res.status(200).json(userExist);

// Check if token exists in the invitations table
console.log("token: ", token)
const invitation = await Invitation.findOne({ token: req.params.token });

if (!invitation) throw new Error('Invalid token');

const newUser = await User.create({
name,
email,
avatar,
parentUserID: invitation.parentUserID, // Use the invitor's ID from the invitation
});

res.status(200).json(newUser);
} catch (error) {
console.log(error);
res.status(500).json({ message: error.message });
}
}
const createUser = async (req, res) => {
try {
const { name, email, avatar, token } = req.body; // Get the token from the request body

const userExist = await User.findOne({ email });

if (userExist) return res.status(200).json(userExist);

// Check if token exists in the invitations table
console.log("token: ", token)
const invitation = await Invitation.findOne({ token: req.params.token });

if (!invitation) throw new Error('Invalid token');

const newUser = await User.create({
name,
email,
avatar,
parentUserID: invitation.parentUserID, // Use the invitor's ID from the invitation
});

res.status(200).json(newUser);
} catch (error) {
console.log(error);
res.status(500).json({ message: error.message });
}
}