Skip to content
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

Merged
merged 5 commits into from
Jul 1, 2024
Merged

Conversation

Pybite
Copy link
Collaborator

@Pybite Pybite commented Jul 1, 2024

resolved issue #81

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;
Copy link
Member

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.

Copy link
Collaborator Author

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

Copy link
Member

@nbkhope nbkhope Jul 1, 2024

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

image

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]);
Copy link
Member

@nbkhope nbkhope Jul 1, 2024

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)

Suggested change
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){
Copy link
Member

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.

Suggested change
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 ]);
Copy link
Member

@nbkhope nbkhope Jul 1, 2024

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.

Suggested change
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)
Copy link
Member

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' });

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Json isn't preferred?

Copy link
Member

@nbkhope nbkhope Jul 1, 2024

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.

Copy link
Member

@nbkhope nbkhope Jul 1, 2024

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:

https://github.com/expressjs/express/blob/4cf7eed927d3ccd3f1d0c9a14d562ec0a1635e86/lib/response.js#L162

Copy link
Collaborator Author

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]);
Copy link
Member

@nbkhope nbkhope Jul 1, 2024

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?

Suggested change
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

server/index.js Outdated Show resolved Hide resolved
Co-authored-by: nbkhope <[email protected]>
@nbkhope
Copy link
Member

nbkhope commented Jul 1, 2024

@Pybite let me know if you want me to review again. You can click the following UI to signal it:

image

@Pybite Pybite requested a review from nbkhope July 1, 2024 20:15
@nbkhope nbkhope merged commit 0052af3 into nbktechworld:master Jul 1, 2024
1 check passed
@Pybite Pybite deleted the backend branch July 1, 2024 22:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants