02 - Create User
In this module, we'll add client methods for creating and logging in as a user.
User Scripts
Please add a 02-user-scripts.js file inside of the client folder:
└── 5-Express Server
└── server
└── client
└── 01-scripts.js
└── 02-user-scripts.js
└── index.htmlWe'll add some of our auth logic in this file.
Script Tags
Follow these steps to wire up the js file:
1. Go to index.html.
2. Go to the bottom of that same file, underneath the Bootstrap scripts and the 01-scripts.js file, and add the 02-user-scripts.js tag like this:
<script src="01-scripts.js"></script>
<script src="02-user-scripts.js"></script>
</body>
</html>userSignUp
At the top of this new script file,02-user-scripts.js, let's add the code for signing up a user:
/********************
* POST - /createuser
*********************/
function userSignUp(){
let userName = document.getElementById('userSignUp').value; //1
let userPass = document.getElementById('passSignUp').value;
console.log(userName, userPass);
let newUserData = {user : { username: userName, password: userPass}}; //2
fetch('http://localhost:3000/api/user/createuser', {
method: 'post',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(newUserData) //3
})
.then(response => response.json())
.then(function (response) {
console.log(response.sessionToken);
let token = response.sessionToken; //4
localStorage.setItem('SessionToken', token); //5
});
}Analysis
Here, we grab the value of the user/password data from the
createuserinput field in theindex.htmlfile.The variables used to store the sign up info from the DOM get passed into the values of the
usernameandpasswordproperties. We package everything up in a user object.In the request object in our
fetch()call, we pass in thenewUserDatavariable to be sent off in the body of our request to the server.We get the
sessionTokenfrom the response and store it in atokenvariable.In our
localStorage, we call thesetItemfunction and store the token inlocalStorage. This will keep our token safely stored in our local window.
Test
Make sure the client and server are running.
Open up the client in the browser. Go to Step #6: POST Sign Up on the table.
Sign up with a new user and press send. You should see the following:
Notice that we have the username, password, and a token printing in the console window.
You should also crack open Postgres, refresh your database, and look at the User table. You should see the new user added there now.
userSignIn
The signin function is exactly the same as signup. We're just pulling from different fields and sending a request to a different endpoint. Put this code under userSignUp:
function userSignIn(){
let userName = document.getElementById('userSignin').value;
let userPass = document.getElementById('passSignin').value;
console.log(userName, userPass);
let userData = {user : { username: userName, password: userPass}};
fetch('http://localhost:3000/api/user/signin', { //<---signin route used
method: 'post',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(userData)
})
.then(response => response.json())
.then(function (response) {
console.log(response.sessionToken);
let token = response.sessionToken;
localStorage.setItem('SessionToken', token);
});
}Testing
Follow the signup instructions to create a couple more users. Then use Step 7: POST SignIn to sign in as each user. Notice that each one gets a different session token, which is reset in localStorage each time.
Last updated