-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added signup endpoint to backend #85
Conversation
server/index.js
Outdated
@@ -48,14 +48,32 @@ async function setupApp() { | |||
|
|||
}); | |||
|
|||
app.post('/users', async (req, res) => { | |||
try { | |||
const { name, email, password, birthdate, notification_preference, biography, terms_agreed, newsletter_subscribed } = req.body; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the frontend form fields have name attribute values using camelCaseLikeThis.
For example: notificationPreference
So we should extract the camelCase names.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh ok I don't know why I used the table names
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't talk about it in bootcamp, but one good program to test POST requests is Postman.
https://www.postman.com/downloads/
You can skip registration, then change it to POST, then write the URL. Then you go to
Write the Body (raw, JSON). Press Send.
server/index.js
Outdated
app.post('/users', async (req, res) => { | ||
try { | ||
const { name, email, password, birthdate, notification_preference, biography, terms_agreed, newsletter_subscribed } = req.body; | ||
const user = await pgClient.query(`SELECT * FROM messages WHERE email = $1`, [email]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Backtick is when we need to interpolate. We should not interpolate any SQL statement otherwise we'll introduce SQL injection. (You didnt substitute anything here, but just in case)
const user = await pgClient.query(`SELECT * FROM messages WHERE email = $1`, [email]); | |
const user = await pgClient.query('SELECT * FROM messages WHERE email = $1', [email]); |
server/index.js
Outdated
if(user.length !== 0){ | ||
return res.json({ error: 'email already exists' }); | ||
} | ||
if (user.password.length < 6){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's possible they didn't include any password at all in the request, in which case user.password.length
would fail because user.password is undefined. So we guard against that with first checking if it isnt defined.
if (user.password.length < 6){ | |
if (!user.password || user.password.length < 6){ |
server/index.js
Outdated
return res.status(401).json({ message: 'password has to be more than 6 characters' }); | ||
} | ||
|
||
const userData = await pgClient.query(`INSERT INTO users (name, email, birthdate, notification_preference, terms_agreed, newsletter_subscribed) VALUES ($1, $2, $3, $4, $5, $6) RETURNING *`, [name, email, password, birthdate, notification_preference, biography, terms_agreed, newsletter_subscribed ]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The frontend names are different (you have to check name attributes in signup.js). 🤷♂️ Just gotta use correct names.
const userData = await pgClient.query(`INSERT INTO users (name, email, birthdate, notification_preference, terms_agreed, newsletter_subscribed) VALUES ($1, $2, $3, $4, $5, $6) RETURNING *`, [name, email, password, birthdate, notification_preference, biography, terms_agreed, newsletter_subscribed ]); | |
const userData = await pgClient.query(`INSERT INTO users (name, email, birthdate, notification_preference, terms_agreed, newsletter_subscribed) VALUES ($1, $2, $3, $4, $5, $6) RETURNING *`, [name, email, password, dateOfBirth, notificationPreference, biography, agreeToTerms, subscribeToNewsLetter ]); |
const userData = await pgClient.query(`INSERT INTO users (name, email, birthdate, notification_preference, terms_agreed, newsletter_subscribed) VALUES ($1, $2, $3, $4, $5, $6) RETURNING *`, [name, email, password, birthdate, notification_preference, biography, terms_agreed, newsletter_subscribed ]); | ||
return res.json(userData); | ||
} catch(err){ | ||
console.error(err.message) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should make sure to also send a res in case of error.
e.g.
return res.status(500).send({ error: 'Internal Server Error' });
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Json isn't preferred?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Pybite json vs send function? Either one works same way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Pybite send() calls json() behind the scenes, reading the source code for express library:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh thank you ! I surely will read up on it right now
server/index.js
Outdated
app.post('/users', async (req, res) => { | ||
try { | ||
const { name, email, password, birthdate, notification_preference, biography, terms_agreed, newsletter_subscribed } = req.body; | ||
const user = await pgClient.query(`SELECT * FROM messages WHERE email = $1`, [email]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you mean to check the users table?
const user = await pgClient.query(`SELECT * FROM messages WHERE email = $1`, [email]); | |
const user = await pgClient.query(`SELECT * FROM users WHERE email = $1`, [email]); |
You can always check your statements in the Node REPL like this: (in Terminal)
cd server
node
pg = require('pg')
pgClient = new pg.Client({ database: 'full_stack_db', username: 'full_stack_user', password: 'changeme'})
pgClient.connect()
email = '[email protected]'
user = await pgClient.query('SELECT * FROM users WHERE email = $1', [email])
.exit
Co-authored-by: nbkhope <[email protected]>
@Pybite let me know if you want me to review again. You can click the following UI to signal it: |
resolved issue #81