Closed taki-tiler-server[bot] closed 5 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 a user, you should implement your mutation as follows:
On each of these steps, some error may occur. It's important in these cases to provide a good description to the client of what went wrong. For example, if the password is wrong, you could return an error message like Invalid credentials, please check your e-mail and password
.
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 absed authentication, jwt tokens and an example of how to imnplement 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
This is another super-important concept on the process of building your own server. Have you ever stopped to think about how you would test if your code works? 🤔 If you thought about performing the query/mutation on one of those applications (browser, graphiql, graphql playground) and checking if it works as expected, that's one way of doing it. But we have a more solid proposal: writing more code for tests! The principle is simple: you give an entrance and check if the response is the expected. Why is it better? Well, some advantages:
If you search on the Internet, you'll find out there are many ways to test a code. Depending on the project, people decide to use one, or several of them. Here, we're going to write Integration tests
. For each query/mutation of your server, you're going to setup the database for them, provide some input scenarios, and then check if the response is given as expected.
Let's talk about another libraries (we use a lot of them!) that helps us to test our code: mocha, for test setup; chai, for the assetions; and supertest, to simulate a server running.
Your task now is to install these libraries and write one test for your recently implemented Login mutation. Some important things to have on mind:
supertest
, or just create an agent and send requests to your localhost:3000/
. If the second option is chosen, you should run your server along with the tests.package.json
, so you can just run npm run test
on terminal to perform them.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:
remeberMe
. 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 the context.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 - 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 return the following structure on body: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 populated with some mock data.