fac-17 / week-7-langs

Week 7 Group
https://lang-week7.herokuapp.com/
0 stars 4 forks source link

Force Unique user in SQL database #33

Open samjam48 opened 5 years ago

samjam48 commented 5 years ago

You can add a UNIQUE specifier in your SQL database to prevent multiple occurrences of the same username. This would save having so much logic in the handleRegister() function and allow you to make the code more efficient without polling the database to find all users and then see if they exist already. (although nice job on implementing all this functionality and making it work)

Your SQL would just be like this:

CREATE TABLE IF NOT EXISTS users (
    user_id SERIAL PRIMARY KEY,
    user_name VARCHAR(50) NOT NULL UNIQUE,
    user_hash VARCHAR(100) NOT NULL
);

and then why you try and add a user you can just handle the error if the user exists instead of so much code for verifying stuff.

samjam48 commented 5 years ago

You could use the functionality you've built to check in the front end to prevent the form being submitted and send them an error message if the username is not unique

gminova commented 5 years ago

thank you! :smile_cat: didn't know about that,