-
-
Save catalinpit/9e1b5a36f4d420034a20a304e008cac9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const REGISTRATION = ` | |
mutation ($name: String!, $username: String!, $email: String!, $password: String!) { | |
insert_users_one( | |
object: { | |
name: $name, | |
username: $username, | |
email: $email, | |
password: $password | |
} | |
) { id } | |
} | |
`; | |
const execute = async (variables, reqHeaders) => { | |
const fetchResponse = await fetch( | |
"<your_GraphQL_endpoint>", | |
{ | |
method: 'POST', | |
headers: { | |
...reqHeaders, | |
'x-hasura-access-key': process.env.HASURA_GRAPHQL_ADMIN_SECRET | |
} || {}, | |
body: JSON.stringify({ | |
query: REGISTRATION, | |
variables | |
}) | |
} | |
); | |
const data = await fetchResponse.json(); | |
return data; | |
}; | |
// Request Handler | |
app.post('/register', async (req, res) => { | |
// get request input | |
const { name, username, email, password } = req.body.input; | |
// encrypt the password | |
const hashedPassword = bcrypt.hashSync(password, 10); | |
const { data, errors } = await execute({ name, username, email, password: hashedPassword }); | |
if (errors) { | |
return res.status(400).json(errors[0]) | |
} | |
return res.json({ | |
...data.insert_users_one | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment