Open taki-tiler-server[bot] opened 3 years ago
finish
Now you're going to fully integrate the Login mutation with the database. Since we don't have a mutation to create users (yet), we should create one directly on the database, so we have the conditions to test it properly.
NOTE: One of the required data to create a user is the password. You should have read or heard already that storing users password as plain text in the database is a bad idea. If not, you're reading now: it is a bad idea 🤦. The minimum security we should have on this case is to use a hash algorithm to store the password. This is not a 100% secure method, but it helps already. Interested in this security matter? Check this post for additional details. Since this is only an onboard server, you don't need to have a ultra-master-blaster security method. Our main goal here is to show you some levels of security and prevent you from beginning your projects with an ultra-master-blaster insecure method.
After creating an user, you should implement your mutation as follows:
finish
Now you're going to finish your Login
mutation returning a proper token instead of an empty string. Take a look at an example of the token you're going to implement:
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
This may look like a random string, but this token has a lot of power, because there are some information encoded on it. The server can know only with a token, for example:
This is an example of the famous JWT token. You're going to build these tokens and return on your mutation. This link has some information about token based authentication, jwt tokens and an example of how to implement it on code.
Your task now is to implement a model of JWT tokens for your server and return them on your Login
mutation.
finish
NOTE: some steps on this onboard are classified as "challenges". They are meant to be some additional tasks that add some bonus features, but are not necessarily core of the server. Try not to loose too much time on them 🙃
After implementing the login, you must have noticed that one of the possible parameters for creating a JWT token is the expiration
. This parameter allows us set an expiration timestamp coded on the token. The challenge consists of:
rememberMe
. It's a boolean.rememberMe
was sent and its value is true
, you should increase the expiration of the returned token. 1 week is good for our context.Now your clients will have the oportunity to use that famous "Remember me" checkbox!
finish
Click here for your next track
In this track, you will implement a login mutation fully integrated with the database. There are many ways of implementing an authentication. The way we're doing it is: the user sends a password to prove that he is who he claims, and then the server provides a token that allows him to access some data on future requests. This token generally have an expiration, after which the user has to login again to get a new one.
Step 1/4 - The mutation setup
Estimated time: 1 hour
Let's start with baby steps, by creating the mutation prototype, with no integration with the dabatase (yet).
Let's call the mutation
login
. It should receive an e-mail and a password as parameters and, in case of success, return the following structure on body:Your
User
can have other information if you want, but these fields above should be enough.Note: Did you notice that there is a
data
object wrapping theLogin
response? Don't worry, it's the GraphQL response format. It wraps all the successful response data inside thedata
object and all the errors in anerrors
array of objects. You can read more about it here.For now, you can return the above structure directly on your code populated with some mock data.
Note 2: don't forget to open Pull Requests at the end of every step that has some code to be written!