01 - User Create
In this module, we'll start to set up the necessary items for creating a new user.
So far, we've just been putting data into a table in our database. However, if someone were to look at that data, there's no way to tell who actually put the data there. Additionally, there's no security protocols in place, so anyone can connect to the database and modify its contents in whatever way they choose. We can use JWT to fix both of these issues. We have a little bit of prep to do first, though: We'll use Sequelize and create a User model to create a new user in the database.
User Model
Let's set up a new user model. Create a file user.js
in the models
folder.
Analysis
This should look familiar:
1. A function with a Sequelize object that calls the define
method.
2. A first parameter that will create a users
table in Postgres.
3. An object with username
and passwordhash
that will be the columns in the table. We'll talk more about a passwordhash
later.
app.js
We'll need to set up a route to the user controller methods in app.js
. We have added the entire file for orientation:
Analysis
We import the
usercontroller.js
file.We set up a route to the endpoints for the
api/user
route.Just another way to write out your routes. Just be consistent.
usercontroller.js
If you haven't already, create a new file inside the controllers
folder and call it usercontroler.js
. We'll need to add some code to that file. Note: if Lebowski isn't your style, enter in your own flavor for the string values:
Analysis
This should look familiar again:
1. We bring in our necessary imports. Same as the testcontroller, just with a User model now.
2. We start out our POST
method for a createuser
endpoint.
3. Inside the method, we have the basics for creating a new user and returning a message in the response.
Postman
Let's quickly test this with Postman.
1. Start your server then open Postman.
2. Figure out the endpoint to send a POST
request to.
3. Press send.
4. You should see the response string:
Postgres
You should also go check Postgres to see that the data showed up:
Last updated