How to Add Authentication to React Applications | Hygraph
Tue Apr 18 2023 19:51:23 GMT+0000 (Coordinated Universal Time)
Saved by
@preston
#javascript
//SIGN UP API
router.post('/auth/signup', async (req, res) => {
try {
const {
email, password, firstname, lastname,
} = req.body;
if (!email || !password || !firstname || !lastname) {
res.status(400).end();
}
const hashedPassword = await bcrypt.hash(password, 8);
const userData = {
email,
password: hashedPassword,
firstname,
lastname,
};
const response = await gqlClient.request(CreateNextUserMutation, { userData });
if (!response?.createNextUser) {
console.log('CreateUser Failed, Response: ', response);
res.status(400).end()
}
const token = jwt.sign({ user: response.createNextUser }, JWT_SECRET, { expiresIn: JWT_EXPIRES_IN });
res.send({ user: response.createNextUser, token });
} catch (err) {
console.log('POST auth/signup, Something Went Wrong: ', err);
res.status(400).send({ error: true, message: err.message });
}
});
content_copyCOPY
https://hygraph.com/blog/react-authentication
Comments