Simple Auth Part II (React)
Auth React-Side
Our job in React is simple enough:
send requests for login
if the login is successful, get the token and store it
from there on, send the token with each request
also, do something with the interface so the user knows they're logged in (show their avatar, etc)
Some Utilities to Help Us
We have a little problem to solve first though. Needing to send the token sometimes means the url would be, at times http://localhost:8080/contacts/list?order=email
and other times http://localhost:8080/contacts/list?order=email&token=xxxxx
.
As we add parameters, this may becomes more and more cumbersome. Let's create a little function to handle that for us:
{ name:'john', surname:'silver', guineas:400}
```
and transforms it into a string like
```
name=john&surname=silver&guineas=400
```
This is a very simplistic function that will break on nested objects or arrays
@param {Object} params An Object containing all the keys
@returns {string} the query parameters string
*/
export const objectToQuery = params => {
return Object.keys(params)
.filter( key => params[key] !== undefined)
.map( key => encodeURIComponent(key) + '=' + encodeURIComponent(params[key]))
.join('&')
.trim();
}
/**
creates a url string with a query object
For example:
```
const url = makeRequestUrl('/stardust',{a:1,username:'ziggy'})
```
url in this case will be:
/stardust?a=1&username=ziggy
@param {String} path the path that you want to request
@param {Object} params an object of parameters
@returns {string} the url
*/
export const makeRequestUrl = (path, params) => {
if(!params){ return path } // if no parameters were provided, return early
const query = objectToQuery(params)
if(query.length){
const has_interrogation_mark = path.indexOf('?') >= 0
const url = path + (has_interrogation_mark ? '&' : '?') + query;
return url
}
return path
}
```
While we're at it, let's create a shortcut so we avoid writing the full URL every time:
Now, instead of this:
we can write this:
This "token
" thing begins empty, but when we log in, it will be filled, and from that point on, it will be sent with each request
Login and Logout
Those are particularly simple:
One last thing to do, we want a form for logging in:
Finally, change the previous renderProfilePage
to:
Run the app!
We can verify this is working by trying a few usernames and passwords from the ones we've stored previously. For example, we can try:
We should see "hello anna!" with a logout button. Let's try the logout.
We just have to verify that we're receiving and sending the token.
Let's just add a button that accesses the user's page /mypage
:
Then try to click it; you should get an error. Log in, using, say:
You should get a message saying you accessed the page successfully.
We're done!
Almost.
If this was a real application, we'd probably want to store the users in our SQL database, and we'd probably want a way for user to sign up, reset their password, and so on. But, as we said at the beginning, all of this is too sensitive to reinvent it with every app, which is why we...
...are going to scrape most of what we've done here, and do production-grade authentication in the next chapter.
Last updated
Was this helpful?