1 00:00:00,940 --> 00:00:10,539 well in this video we are going to start to develop the login function that is very similar to the one 2 00:00:10,539 --> 00:00:16,620 we have been doing so far of register ok in the login one we are going to think a little 3 00:00:16,620 --> 00:00:25,660 about how it is going to work the front is going to send us a json in that json what is usually 4 00:00:25,660 --> 00:00:32,679 always send is the email and the password is worth the email and the password we are going to say 5 00:00:33,520 --> 00:00:41,679 then we are going to receive instead of these three fields because the username we would not need it 6 00:00:41,679 --> 00:00:47,859 is worth once we have the email and the password what is done is first we check that the email 7 00:00:47,859 --> 00:00:54,640 exists in our database so we are going to launch a selection consultation of a single 8 00:00:54,640 --> 00:01:02,259 field that this email is looking for if this email does not find it then it will give us an error if it 9 00:01:02,259 --> 00:01:09,540 finds it the second step would be to check that the password that they send us from the front is correct 10 00:01:10,939 --> 00:01:19,219 ok if it is not correct it is assumed that the email yes but the password not so there we would send another 11 00:01:19,219 --> 00:01:30,560 another error code is valid if the email and the password are fine then what do we have to 12 00:01:30,560 --> 00:01:37,879 do and create our touch is worth the same as we have done when we have registered it then we are going 13 00:01:37,879 --> 00:01:46,540 to pass the value load that is the idea will create the access to that in with the function that we have done 14 00:01:46,540 --> 00:02:00,540 in the previous video, and once the token has been created, we are going to create the cookie and 15 00:02:00,540 --> 00:02:10,639 we send the json to the from, if any type of error occurs, then we put the 500 or the state 16 00:02:10,639 --> 00:02:17,419 o el código de estado que nosotros queramos entonces un poco los pasos a seguir son esos 17 00:02:17,419 --> 00:02:25,520 pero me voy a basar en la función que hemos hecho del register así que esto voy a ir colocándolo 18 00:02:25,520 --> 00:02:42,460 todo un poco dejándolo ya todo bien vale este aquí este bien esto no lo queremos fuera 19 00:02:42,460 --> 00:02:55,020 then we are going to start with all this I am going to upload it to have it all that we have 20 00:02:55,020 --> 00:03:05,180 everything visible or we are going to download this so it is worth then the function is good this I am going to 21 00:03:05,180 --> 00:03:11,680 erase it entirely, it is not worth it, I write it again, there is no problem, it is because with 22 00:03:14,740 --> 00:03:20,560 login the same as synchronous, it is all the same, 23 00:03:22,960 --> 00:03:30,099 we are going to receive the parameters, the network and the network is worth the request and the response 24 00:03:30,099 --> 00:03:48,330 and we do our arrow function and we start the first thing we want to receive is the e-mail field 25 00:03:48,330 --> 00:04:08,050 ok the field email and the field password that we are going to pick up from rec dot 26 00:04:10,530 --> 00:04:19,050 ok then here we are already taking the password and the email that the from the client is sending us 27 00:04:19,050 --> 00:04:27,689 once we have done that we follow the same steps ok then as we are going to do an operation 28 00:04:27,689 --> 00:04:35,970 on the mongo database we are going to use the track at to control those errors ok so 29 00:04:35,970 --> 00:04:46,449 we put the track at ok and here we have said that the first thing we want to do is to check that 30 00:04:46,449 --> 00:04:56,170 that email that they are passing us from the front exists then to see this 31 00:04:58,110 --> 00:05:04,290 because we are going to launch a consultation ok then this consultation I am going to put it in two 32 00:05:04,290 --> 00:05:11,410 ways one which is the way it gets older not for a little longer 33 00:05:11,410 --> 00:05:17,170 time that also works but you are going to see that if you go to the technical documentation and that is why 34 00:05:17,170 --> 00:05:24,490 I insist and I am very heavy that you always work with the technical documentation, it is worth because 35 00:05:24,490 --> 00:05:32,670 if you do not get used to doing things but you do not update them at the same time that they 36 00:05:32,670 --> 00:05:38,170 are updated by the development teams of the different technologies that you are using, 37 00:05:38,170 --> 00:05:43,810 ok then this before we could put something like cons 38 00:05:45,329 --> 00:05:53,750 well I am going to explain it to you ok it is a when we have made the connection with our database 39 00:05:53,750 --> 00:06:00,009 we have used mongoose that you already have to have it clear ok with the mongoose connect we made the 40 00:06:00,009 --> 00:06:06,610 connection then we have made the mongoose point is scheme that is the scheme that we have made and 41 00:06:06,610 --> 00:06:13,050 and from there we have taken out a model that is user and user we have imported it up here because 42 00:06:13,050 --> 00:06:18,750 we have worked with it when registering a user, it is worth then we have that user 43 00:06:18,750 --> 00:06:28,290 if I put here user point that model is the one that allows me as it belongs as it is created 44 00:06:28,290 --> 00:06:35,949 from the model of mongoose it allows us to launch the queries it is worth then if I put the user here 45 00:06:35,949 --> 00:06:44,889 and I put the fine one here we have types of queries is worth in this case of search selection 46 00:06:44,889 --> 00:06:51,269 search that will return me all the records search for there and search for update delete 47 00:06:51,269 --> 00:07:00,410 search and delete good for there it is worth all these to me what interests me now is to look for one we know 48 00:07:00,410 --> 00:07:08,949 que el esquema que hemos hecho de los datos cuando registramos a un usuario pusimos que 49 00:07:08,949 --> 00:07:18,670 el email tiene que ser único vale si me vengo a modelos user models tenemos que el email es 50 00:07:18,670 --> 00:07:25,790 requerido que sí y que es único lo cual le hacía la clave vale que es único vale entonces 51 00:07:25,790 --> 00:07:37,850 here what I am going to put is user find one and that he looks for us is that email then what 52 00:07:37,850 --> 00:07:50,079 we pass is that json that is the email two points I do not need here with passing the email it is already 53 00:07:50,079 --> 00:08:00,170 me va a servir entonces yo ser fan one este email vale que me busque este email vale ese 54 00:08:00,170 --> 00:08:09,209 email o esa búsqueda tiene que ser asíncrona así que el agua vale por eso le hemos metido 55 00:08:09,209 --> 00:08:25,949 up here also the async is worth this error is worth it and we are going to collect the result of this operation 56 00:08:27,610 --> 00:08:38,210 in a variable because we are going to call it user ok then here we have in 57 00:08:38,210 --> 00:08:46,269 user found we are going to launch the consultation so that it looks for the first document that it finds with 58 00:08:47,929 --> 00:08:56,070 an email value that accompanies the email key and that it is the one that we are passing between keys 59 00:08:56,070 --> 00:09:03,649 that is the one that we have passed from the front ok well this would be a way to put it and it is 60 00:09:03,649 --> 00:09:11,269 how it has been put for a long time, it is worth and it is this line and that's it now if we come to the 61 00:09:11,269 --> 00:09:20,639 technical documentation it is worth here we have within mongoose of the documentation of the mongoose document 62 00:09:20,639 --> 00:09:32,159 it is worth of the doc in queries I have the one of query prototype the fine one is worth then the parameters 63 00:09:32,159 --> 00:09:41,779 que retorna que es la query bueno tenemos aquí un ejemplo qué es el que me interesa 64 00:09:43,299 --> 00:09:49,460 vale entonces aquí te declara una variable que es query en la que te dice el modelo que usa en este 65 00:09:49,460 --> 00:09:56,779 ejemplo que es kit en punto web donde vale el color sea igual a blanco vale es como escribir 66 00:09:56,779 --> 00:10:05,539 the criterion the condition now I create another variable that would be here the call remove also 67 00:10:06,980 --> 00:10:09,899 we are going to make a pause forgive 68 00:10:12,120 --> 00:10:20,460 ok we continue we are seeing ok the example ok then we launched the consultation that we have 69 00:10:20,460 --> 00:10:29,679 created in the web and this will return a json with all the information that we have in that 70 00:10:29,679 --> 00:10:37,740 document saved in the mongo ok if it finds it then we are going to write it as it is 71 00:10:37,740 --> 00:10:49,899 then we return to our code and I write it as it is in the documentation and that is constant 72 00:10:49,899 --> 00:11:03,480 query this is equal to user that in our model where it is worth where the field and mail 73 00:11:11,679 --> 00:11:23,039 is equal and between and the string well here there is no string here it is and mail also 74 00:11:23,039 --> 00:11:31,379 ok it seems that I am putting the same thing but it is not the same this email is the key of the jason 75 00:11:31,379 --> 00:11:37,559 that I am sending within the web ok and this other email is the one I am collecting from the 76 00:11:37,559 --> 00:11:43,500 reg body ok I do not know if in the video you can see that here is a lighter blue and here a 77 00:11:43,500 --> 00:11:54,100 darker ok they are not the same ok this point and comma ok and what I am going to have is the second line 78 00:11:54,100 --> 00:12:03,600 that it was with use is the same as a white is worth because 79 00:12:03,600 --> 00:12:07,440 white 80 00:12:07,440 --> 00:12:17,509 query point fine parenthesis parenthesis point 81 00:12:17,509 --> 00:12:24,029 ok well this according to the technical documentation is the way it is 82 00:12:24,029 --> 00:12:31,870 put today is from here is the way it has been put for a long time, okay, then 83 00:12:31,870 --> 00:12:40,350 nothing, we are going to comment this way and we are left with the new one, okay, what is the next thing I am going to 84 00:12:40,350 --> 00:12:47,850 do once I have launched the consultation and I am going to have a result in users found that or it may 85 00:12:47,850 --> 00:12:55,570 be that there is nothing or it may be that there is a json with the information of its user ok if there is 86 00:12:55,570 --> 00:13:03,230 nothing it means that that email is wrong so here we are going to put a yes 87 00:13:07,129 --> 00:13:16,769 we are going to put it if there is nothing in you are found it is null that you are going to do a return and we are going to 88 00:13:16,769 --> 00:13:27,779 send a network is in status and we can send a code because it can be 89 00:13:31,120 --> 00:13:39,299 204, it is worth that there is no point and comma content, well if we go back to 90 00:13:42,220 --> 00:13:47,980 our web here I have the state codes that are divided into 91 00:13:47,980 --> 00:13:52,059 informativa respuesta satisfactoria de redirecciones errores del cliente este 92 00:13:52,059 --> 00:13:59,600 es el mdn web docs vale entonces aquí tenéis todos los códigos de error y así 93 00:13:59,600 --> 00:14:05,679 los podéis usar de una forma coherente vale vale 94 00:14:05,679 --> 00:14:11,899 volvemos a nuestro código y seguimos vale qué pasa 95 00:14:11,899 --> 00:14:14,500 sí 96 00:14:14,500 --> 00:14:29,500 the email is correct, it is fine, then we continue working, it is worth what we do if the email is correct, it is to compare, 97 00:14:29,500 --> 00:14:34,500 if the email is correct, we have said that in UserForms it returns a JSON with all the information of its user, 98 00:14:34,500 --> 00:14:41,940 user so one of that part of that information is a key value field that is the password and the 99 00:14:41,940 --> 00:14:46,980 password that we have registered in the database then we can make that comparison 100 00:14:46,980 --> 00:14:52,820 is worth that comparison we are going to use it we are going to create it with or we are going to do it with by 101 00:14:52,820 --> 00:15:03,960 click and it is worth what is the one we have used up here where here the bike is worth 102 00:15:06,240 --> 00:15:17,399 to do the hash to do to encrypt that password is worth then here we are importing it 103 00:15:17,399 --> 00:15:24,480 by Crip, the by Crip.js to by Crip and here I am going to use it again, okay, then we are going to put 104 00:15:24,480 --> 00:15:38,889 this would be something like by Crip, this is, I am writing more like this, .compare and in compare to that function 105 00:15:38,889 --> 00:15:44,190 what we are going to pass is on the one hand the password that we have, okay, the one that has passed us 106 00:15:44,190 --> 00:15:52,289 the from and the one that we are collecting in this variable from up here is worth 107 00:15:53,750 --> 00:16:04,279 and for another the one that I am going to pass on is the one that we have found when we have compared when we have 108 00:16:04,279 --> 00:16:12,700 launched the consultation on the email then I am going to put it here the user found point password is worth this 109 00:16:12,700 --> 00:16:21,080 is this instruction this bike.com for what is going to return us is a true or a false 110 00:16:21,080 --> 00:16:33,519 ok and it is an operation on the database so we do it asynchronous and with we are going to say and smart 111 00:16:33,519 --> 00:16:48,519 for example, it is equal to a variable that is coincident, because yes or no, false or no, this is the same 112 00:16:50,019 --> 00:16:57,620 if we go to the help we go to the help to the technical documentation because we also have 113 00:16:57,620 --> 00:17:04,099 here the different examples ok you see the sea the residence such good because a little you read it 114 00:17:04,099 --> 00:17:10,099 you see you see that it is going to compare a little the same thing that I am doing ok although here it is 115 00:17:10,099 --> 00:17:15,200 comparing the hash this we have to prove now because when we saved in the 116 00:17:15,200 --> 00:17:23,720 database we are saving it already configured encrypted ok then that has to be checked ok I think 117 00:17:23,720 --> 00:17:29,619 that it compares directly, it decrypts it and it does it for us well, but let's see it, 118 00:17:30,660 --> 00:17:37,960 we are going to continue in our code in our visual studio code, 119 00:17:39,759 --> 00:17:44,380 okay, so what am I going to do? ask if 120 00:17:44,380 --> 00:18:00,299 is not worth if there is no coincidence that is, the passwords are not the same, so what am I going to 121 00:18:00,299 --> 00:18:08,279 tell you, because you send a retun again, we send it back now or we send it a res in status 122 00:18:09,539 --> 00:18:10,839 and in this case 123 00:18:10,839 --> 00:18:33,660 because I am going to send a I do not know the error 401 the error 401 is worth the error 401 is the one of 124 00:18:33,660 --> 00:18:47,839 that you are not authorized, okay, okay, then if the email is fine, we have found it 125 00:18:47,839 --> 00:18:56,259 in our database and the passwords are the same, so what do we have to do 126 00:18:56,259 --> 00:19:09,039 all the part of creating the touch this one of the 6 is not valid but this one is valid then here we are going 127 00:19:09,039 --> 00:19:17,299 to put a little bit because the same constant touch is equal to weight is valid to create 128 00:19:17,299 --> 00:19:27,680 access to touch with and here I do not put the user 6 because it is within the login function I do not have it 129 00:19:27,680 --> 00:19:36,920 ok then there what we are going to put is that you believe me the touch passing the value load that is the 130 00:19:36,920 --> 00:19:47,299 the id and this is going to be you have to put in that key the user found ok dot under there 131 00:19:47,299 --> 00:19:55,599 ok now we are working with the user found ok I have already created that touch in principle 132 00:19:58,559 --> 00:20:06,470 we are going to create the cookie ok and in the cookie we first pass the name of the cookie which is 133 00:20:06,470 --> 00:20:16,710 touch and then the value that is going to be all worth this already because it does not change anything that more we pass the 134 00:20:16,710 --> 00:20:19,950 3.j are 135 00:20:21,769 --> 00:20:28,609 parentheses and keys ok we are passing the id 136 00:20:28,609 --> 00:20:37,829 is equal to the user fan point there the username 137 00:20:37,829 --> 00:20:50,670 two points and use a point and use name and we are missing the email 138 00:20:50,670 --> 00:20:58,049 two points and use 139 00:20:59,450 --> 00:21:05,960 point and 140 00:21:07,319 --> 00:21:11,529 and here 141 00:21:11,529 --> 00:21:28,650 ok, here it would be to close the keys, close the parenthesis, ok, and if there is no error, then what 142 00:21:28,650 --> 00:21:34,529 , that is, sorry, sorry, if any error has occurred in all these operations, we can 143 00:21:34,529 --> 00:21:49,259 pass the error 500 the error 500 then we are going to pass the res status 144 00:21:51,500 --> 00:21:55,960 500 ok and if you want here we can pass the json 145 00:22:05,579 --> 00:22:13,480 an error has occurred in the server 146 00:22:14,859 --> 00:22:34,859 or we can pass the same json error, we could also do that, well, in this case I'm going to leave it like that and that's it, okay, let's see how it works for us, 147 00:22:34,859 --> 00:22:48,099 the point works well then we are going to see the records that 148 00:22:48,099 --> 00:22:55,769 we have this 149 00:22:55,769 --> 00:22:59,509 the one of carmen is worth the one of carmen we are going to delete it and we are going to work 150 00:22:59,509 --> 00:23:01,890 on it 151 00:23:02,509 --> 00:23:07,170 we are going to come and this out 152 00:23:07,170 --> 00:23:15,049 and out and we are going to be in berkley and we are going to delete these and then we are going to do 153 00:23:15,049 --> 00:23:24,890 everything so that we do not forget how it is done then new request ok we remember that it is the post 154 00:23:24,890 --> 00:23:37,009 we remember that to register it was the local host register that I go to the body ok and here we are going to 155 00:23:37,009 --> 00:23:46,980 put the values ​​then that the username is the same 156 00:23:49,519 --> 00:23:59,960 that the email the points is the same as 157 00:23:59,960 --> 00:24:14,019 carmen arroba dan 2.com and that the password this is equal to 158 00:24:15,920 --> 00:24:23,599 1 2 3 4 because you want this I give it to send this creates it for us we have not done this 159 00:24:23,599 --> 00:24:31,059 register we have not touched it ok we are going to check it here I have it Carmen with her password 160 00:24:31,059 --> 00:24:41,099 encrypted everything, everything is fine, now let's go to create another consultation, then we are going to close this 161 00:24:41,099 --> 00:24:50,019 and this and this and this I leave another consultation ok then we are going to put the post we are going to put 162 00:24:50,019 --> 00:25:00,700 localhost 2 points 4000 barra dan 2 in this case it is worth it I go to the body and now we are going to 163 00:25:00,700 --> 00:25:06,099 start checking the different errors that we have been controlling the first was that the email 164 00:25:06,099 --> 00:25:11,259 did not exist ok then we are going to put it well and we know that we only pass two fields and 165 00:25:11,259 --> 00:25:20,759 email and the password is worth then email that will be the one that is worth comma and the password 166 00:25:20,759 --> 00:25:31,940 ok then we are going to put the password now now well but the email I am going to put it wrong ok 167 00:25:31,940 --> 00:25:37,539 then the email I am going to put the carmen 2 carmen 2 168 00:25:39,920 --> 00:25:59,170 gmail eradan 2.com ok then I have this this in quotes doubles is his thing ok I give it to sense 169 00:25:59,170 --> 00:26:16,170 and it does not return me anything or if it returns me the error the set is the same status 204 it is worth that it is the one 170 00:26:16,170 --> 00:26:22,730 that we have told it to happen to us if there is a password problem it is worth that it is not authentic 171 00:26:22,730 --> 00:26:27,910 that the authentication is not correct ok that is not working well it takes us ok it has 172 00:26:27,910 --> 00:26:34,150 checked that the email does not exist, it has launched the firewall, okay, that has done it for us well, 173 00:26:34,150 --> 00:26:40,990 so now we are going to take it, we are going to put it well and clean it, okay, but I am going to dance these two numbers 174 00:26:40,990 --> 00:26:48,670 of the password, then we are going to put instead of 1 2 3 4 1 3 2 4 ok and I give it to be ok and here 175 00:26:48,670 --> 00:26:55,910 gives me the error 401 which is the one that we have told him here 176 00:26:55,910 --> 00:27:01,650 ok then it is before the error 204 was that 177 00:27:01,650 --> 00:27:06,869 I did not find the data of 401 is the one of the authentication is worth a 178 00:27:06,869 --> 00:27:14,269 authorized ok then he tells you here ok 179 00:27:14,269 --> 00:27:20,650 it has also worked for us now we are going to see if I put it all right 1 2 3 4 carmen 180 00:27:22,109 --> 00:27:25,250 if it works for us and if you have found 181 00:27:28,329 --> 00:27:35,210 this json it is worth if you have found this user yes that he has compared the two passwords although 182 00:27:35,210 --> 00:27:40,509 he does not pass it is encrypted now we decrypt it already decrypt it alone and then as he has done everything 183 00:27:40,509 --> 00:27:51,119 well, it has created us, it returns the data that we have told it, the user found, sorry, the ID, the username 184 00:27:51,119 --> 00:28:00,019 and the email, okay, and also the header has been made for us, okay, and the token has been returned to us, which is what 185 00:28:00,019 --> 00:28:08,400 we need if here we are giving as you see that it always gives us 200 that is fine that there is no 186 00:28:08,400 --> 00:28:14,680 problem that the operation has done it well and the touch is changing ok every time we give it to 187 00:28:14,680 --> 00:28:23,660 send it always updates it so that login function would already be ok and it is 188 00:28:23,660 --> 00:28:32,480 working correctly for us now we are going to do since we are in the out function 189 00:28:32,480 --> 00:28:43,920 ok then for the out function we are going to lower this ok we are going to create sport 190 00:28:45,359 --> 00:28:56,740 with the out this is going to be the same an entry of a network and a network we already use them or not it gives us the 191 00:28:56,740 --> 00:29:05,440 same is worth exactly the same as so far an arrow function is worth the first thing we have to 192 00:29:05,440 --> 00:29:10,799 do is create this route because we did not have it then let's go to our routes 193 00:29:13,240 --> 00:29:17,619 routes here you are to routes then here we go to 194 00:29:19,559 --> 00:29:21,559 router post 195 00:29:21,559 --> 00:29:42,700 logout and we want the logout to be executed and here the functions that we want to take is register 196 00:29:42,700 --> 00:29:52,119 login and the logout one cannot be forgotten, it is worth if we cannot do things well, we already 197 00:29:52,119 --> 00:29:59,380 we already have it in route we already export route we have here it is worth this I am going to 198 00:30:02,880 --> 00:30:09,119 and we already have that route so that it does not recognize it, it is worth what I have to do here 199 00:30:09,119 --> 00:30:16,819 when we execute this function in the out, because it is that this touch disappears, it is worth 200 00:30:16,819 --> 00:30:24,000 then for the token to disappear we can this is easy this is the network is worth what 201 00:30:24,000 --> 00:30:31,579 we are going to send it is a cookie but it is empty we are going to send it with the same name then it is going to 202 00:30:31,579 --> 00:30:42,359 crush the previous one but in this case it is sent empty in quotes and I am going to pass a 203 00:30:42,359 --> 00:30:49,740 third option to the res cookie, which are the options, then here what I am going to 204 00:30:49,740 --> 00:30:55,740 happen is when I want you to expire, so if you look here it says options 205 00:30:57,720 --> 00:31:07,480 convert them to write when you want it to expire basically a signature of the cookie 206 00:31:07,480 --> 00:31:13,960 a good pad, what I want to happen to him is this the expiration time of that copy that I want it to be 207 00:31:13,960 --> 00:31:23,160 right now, then we are going to put the spa and we are going to tell him 208 00:31:25,119 --> 00:31:36,240 and my date to do it is worth with this I am telling him to expire at this very moment 209 00:31:37,480 --> 00:31:43,980 so I'm going to put this here and what I'm going to do is to see that everything is fine, 210 00:31:43,980 --> 00:32:00,220 return resens status 200 is worth 200 and I would already have my function that the only thing I do, the only thing 211 00:32:00,220 --> 00:32:10,839 que necesito es ponerla la la cookie toque en hacer entonces si ahora me vengo creamos una 212 00:32:10,839 --> 00:32:22,019 nueva vamos a ponerle post vamos a decirle aquí le hemos puesto si he puesto pues vamos a decirle 213 00:32:22,019 --> 00:32:36,359 localhost 4000 dan 2 y ahora le metemos el logout vale si enviamos vemos que el toque 214 00:32:36,359 --> 00:32:45,400 ya se nos ha quedado en blanco vemos que el response ser cookie esto ya no hay ya no hay 215 00:32:45,400 --> 00:32:55,180 cookie ok and it responds to me one that is worth everything is ok ok well these would be the functions to 216 00:32:57,779 --> 00:33:07,900 register log in and leave the lobby ok well a greeting see you later