Build a Nest.JS, Fastify, GraphQL, and Mongo Auth API

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this speed demo i'm going to quickly create an sjs server on top of fastifier we're going to be exporting a graphql authenticated api and we're going to be hosting everything on our local db i'm going to be using brew install for that so i'm going to use an sds framework everything comes included it's going to be including the graphql decorators that we need as well as the mongoose so we're going to just create a single base this is going to be code first for the schemas so it's going to be running on top of the fastify i'm again i'm going to be using local mongodb brew we're going to be using password for authentication we're going to be using this strategy the jwt and quick note for fastify we're gonna have to be updating our apollo server festify to version alpha 3.0 as of today today is march 25th so be on the heads out for that and at the end we're gonna have this uh so this is just a quick uh playground so i could just show you guys we're gonna have we open up our schemas we have different types of we have an input type for create user we have mutations we have our create user login update user and update pass we're going to be using sjs partial and omit types for those uh remove users and then we have our basic queries such as find all find one in our current user we're going to be creating a decorator that fetches our current user as the person's logged in so just if we call me over here and i click and log in under steve and i click on login i'm going to get this token so if we open up here let's just paste in and now with that code if we were gonna use anything to let's say current user we can fetch our current user all right let's get started so let's just run a fresh nest new project and after it finishes installing let's get started let's just go inside it and let's just use e so we can start going any direction but one thing i like to do is start going down the package json so from here i can see so we come in here one thing we're gonna start doing is we don't need express so i'm going to delete the typings of the dependencies and that itself okay and so right here is where i can start installing all our additional modules let me see everything is installed perfect i'm going to use this prettier package i'm going to use uh-huh now this will save on command okay and so what i want to add is some dependencies that we're going to use we're going to use graphql uh mongoose passport and then some passport wait json web tokens and bcrypt and our festify engine okay so right here um so these are we're going to need we're going to need nest and graphql our nas mongoose and mongoose standalone library so the nes jw jwt token module and then all right i forgot to add here but make sure you add passport um so we have nasty as passport and passport jwt but also add passport and then our festify engine and bcryp so we can just you use a yarn add after that we're gonna have to install the dependency of apollo server and because we're using fastify we're gonna have to use the apollo server alpha version three that's actually uh well documentated on their own okay and so while this is going i'm just gonna start renaming this okay i'm just gonna rename this to resolver okay and then i can remove from my source my app.controller dot spec because we don't want that because that's a resolver okay so this is good now let me hide my node modules okay we need our graphql gwt mongoose passport our fasta fire bcrip our standalones and then let's just add our yarn dependency and this is gonna be apollo server fastify and this is gonna be at three point zero point zero dash alpha three and that's for the playground uh so we wanna use alpha version three let this load okay now one let's go into our modules and let's just do some refactoring okay and then our app resolver since this was previously a controller we can remove these and this is gonna be a resolver and we're gonna bring this from the graphql quarry oh and this is gonna return of type string and then we have our modules imports so from imports is where we're going to need our mongoose module and our graphql module that we're bringing in from nasjs library for root this is going to take an object and this is just going to take a strength o17 okay i'm going to close off nest and then here is going to take some properties so we're going to put the auto scheme of the true because it's gonna be auto generated we're gonna set our playground to true and we wanna set our debugging to false okay and let me see what else let's go to our main right here and we're gonna have to import so we're gonna import our at nest fastify inside here it has two exporting modules we're gonna use both of them and then if we're right here create is we're going to have to specify and then we're going to pass in our new nestify fastified adapter inside here inside our create so let's first specify that this is going to be a nest fast application and then this is going to be level up module and then this is going to be a new fastify adapter and if we do a save it it will auto compress it okay and grab ql now we should just have one query that we can run which is get hello okay so one core coming back okay so now this is working so now one thing we want next thing we want to do is create the authentication and our connection to database so for this guy this is pretty much done let's go over here our at resolver we're going to create a user resolver and then our main engine is up and running okay now one thing i want to do is create the rest of our directories and so i'm going to create a module for user okay so from here i'm just going to go nest generate module i'm going to call this user and then i'm going to do nas generate service user nest generate r of user and then i'm also going to need a guard so from here i want to go to my cd user directory and here's where i need to basically inside here doing this generate guard i'm going to put this as a user guard okay after that uh we're going to create our user dot entity dot ts and then our also jwt dot strategy lts and then our user inputs our ddo dot ts and um i think that is it and we also need a decorator yeah so i'm going to say user decorator ts now those should all create all these files inside here perfect let's get started working on our entity so our user model so like i mentioned the great thing about nsjs is that we can bring in our it has a mongoose npm package and also has a grad coat package and that you can use its own decorators to just create a single class that you specify your scheme and then your graphql scheme and then you create your dtos aside from that which is really useful because then we can create partial types and mix it with omit type so we can just have a update user minus the password prop property okay so let's get started so we're going to need from mongoose we're gonna need nest mongoose we're gonna need prop and then we're gonna need schema to create our schema and then our schema factory and then after that we're going to need our nest graphql packages and that's going to be as before we're going to need our field and then our object type and then a register enum type and that's going to be for the roles because we're going to choose between basic and advanced roles okay and then we're going to need from mongoose itself what we're going to need is the document and types okay so let's start building our object type and schema so we're going to say app object type and at schema type so now these are two graphs these are two decorators coming from each respective library and we're going to say export class we're going to call this user and while i'm here next thing i want to do is export type and we're going to call this user document and this is going to be of type user and document okay and we need to do one more and this is going to be the export of user scheme that we're going to bring into our user module so export const user schema and this is going to come from the schema factory that we're importing which has a method called create for class and all we have to pass in is our user user class okay and now from that we can start defending our properties so like every good one we're always going to have an id and that id is going to be an of type object id but we're going to return it as a string and then we're going to just call it underscore id and this is going to be of types and again this is going to be a specific object id since it's going to be uh unique okay and then i'm just going to copy some of this and then we'll create a prop and then this is going to i could leave this here this is going to be first name and this is just going to be a string so we can just uh duplicate some of this okay so from here i want to change this to last name this i want to change it to password and this i want to change it to email and what else do we have what else do we need oh and then our roles okay and then we also want to do a created ad created at and this is going to be a type string and so for our roles that we created like said we're going to need an enum type so let's just do a base of field and this type that we're going to return back is going to be of roles and then we're going to set the default value to roles dot admin and uh let's leave this to a nullable so because this could be true this could be empty on and um okay so let me put this to rolls and there's gonna be also a type of it's going to be an optional okay so right now we're going to create our enum type so we're going to use this function that we're importing register enum type and we're also going to create so we're going to say enum and we're going to call this roles and so this is going to be a string enough so we're going to call admin is equal to admin and then our basic is equal to basic cool and then from here is we're going to say we want to get our rolls and then we're just going to call it we're going to say specify so our name is going to be um yeah roles and then we can add a description rules for admin creating projects and users okay so now all this should be connecting and we have our we have our data set so perfect so we're using from the mongoose and sjs library and from the nest js graphql we're using a single type to work on this all right and the next thing we're going to move on is our user module okay and just like the app module we're going to import graphql and the mongoose module but we're going to use change it up because here's where we have to specify to mongoose hey creator class and our object type of this specific scheme of this data type and then the graphql this was going to start creating our mutations and queries so let's go create an imports array and again we're going to use our graphql module and this is going to be uh i think for no actually what am i saying we're not going to use a graphql module we are going to need here is our uh mongoose module and our jwt strategy that we still need to create okay so let's just go into mongoose module.4 feature and let's add our array and then inside this array it's going to be a name and this is going to be of our user that we created so we're just going to bring in this entity and we're going to bring in the name and the schema that we want to supply it with is going to be with the user schema that we also created that we're also going to import and let's add our trading comma and then that's all we can do for now all right so let's go to our jwt strategy and for our strategy what we're gonna need is our passport jwt okay and then from here we're gonna need extract jwt and strategy okay and then from our nest passport we're going to need passport strategy and then finally from the nest common we're going to need injectable just come in and we're going to injectable perfect so let's create our decorator of injectable and we're gonna call this call class export class this is gonna be our jwt strategy that's going to extend our passport strategy and we're going to place in our strategy from our passport jwt so let's create our class and so this is going to have a super constructor and we're going to pass in these objects is it's going to be these properties it's going to start creating this on every quest and every response so we're going to use jwt from request request and then from here is we're going to use that extract uh jwt and we're going to use our from off um off header as bearer token so that's going to get our bearer token and then one is ignore expiration and then we're going to say false and then our final one is going to be our secret or key and here i'm just going to say you know um really difficult okay and so we have that for our k2 object and then what we're going to need is we're going to validate it because it's going to look for this and we're going to create a payload which we're going to define um later but for now let me just create this we're going to deconstruct from our payload we're going to grab our id and our email because that's what we're going to store in every payload from rj json web token so we're going to get this from our payload and then we'll remember a new term is a just rd and email and an object i think this is telling me that nope i want to really save this little dictionary okay and i have our strategy and then we're going to create our current user decorator so this is going to be used to fetch the current user's id for validation and we're going to get this from our nest js common and we're going to use create param decorator and then execution context is what's going to carry that and then from our graphql we're going to need our nas graphql library and then we're going to use our gql execution context great so let's create our class sport export const current user is going to be a create prime decorator and so this is going to be of type data and that data is going to be currently unknown and then our contacts is going to be of execution contacts okay and that is it so then we can create our logic and so from here we want to deconstruct uh we're going to deconstruct our id and our email from um our gql graphql execution contacts so let's grab our underscore id and our email and that's going to come from our gko execution because we're going to from there create contacts and we're going to create a context and then from there we're going to use the get contacts method and we're just going to get our request user okay and then from there is where can we turn again our underscore id and our email and an object okay so once that's running let me see we have our user decorator or entity and then we could just start working on our guard um our guard we're not gonna really use any of this it's just gonna be a a a another gql off guard okay so from here i'm gonna import again from nest common i'm gonna bring in my injectable that we need in our execution context and then again from passport and sjs we're gonna nest the passport and we're gonna bring in our off card and then from graphql nest graphql we're going to bring in our gqo execution context again so we're going to create an injectable and then from there we're going to again export class and we're going to call this gql off guard camel case and then we're going to extend this one to be the off guard of type and then we're going to pass in the strategy that we're going to use which is jwt okay and so from here we're going to use a get request function and this is going to be of execution context and then from this method is where we're going to get our contacts so we're going to create a constable constant called ttx abbreviation and this is going to be our same dql con execution context that we're going to create our contacts from and then from here is where we're just gonna return our ctx get contacts and we're gonna we're gonna return our user okay and then we have our user module that i think we already changed and then our user resolver so this two part actually let me work on the user inputs dto before i move on the resolver and the um service because the all the resolver and service they're going to be really hand in hand so let's just create our user inputs for our create and update user input so i'm going to need some things that i'm just going to copy from our graphql here close our module user inputs dto okay so again let me close my resolver and my service for now because we'll come back to that and i just copied my nest imports don't really need this don't really need this don't really need this okay so let's bring in our imports that we're going to need and from here we're going to create an input type input type and then this is going to be our export class and we're going to call this create user input now just a fun fact nest already does a lot of this heavy lifting for us if you use the nest generate resource but it also creates a lot more stuff that we need to do but for this example i just want to creep it create a bit very short so again we're going to create a field and so we're always going to need our first name of string and let me just last name and then our email or password and then uh let me put email right after password doesn't really matter because in our graphql query is all we're gonna choose what to replace back and then okay so we have string email and then let's do our created ad so this is gonna be our created at and there's gonna be a string but i wanna initialize it here now so i wanna call it new date and we're just gonna use two iso string okay so this is our input for um so this is going to create an input type so all these by the far are going to be required and then we're going to do is create a partial and omit type that so when we update the user the only thing that the user can update would be first name last name email and that's it password and created that we're gonna omit it from there so we're gonna choose omit type and then partial type partial type okay and also from here we could say input type and then we're going to put export class and this is update user uh input extends and so here this is going to be a first partial type because this is going to take be able to take in on the base and then we're going to use an omit type to remove axes from our create user input we want to remove access to our password created at and then we want to pass these as a const so that makes it unchangeable and yeah okay so right there we have our update and create user inputs what else are we missing now for the user resolver and service so i'm just going to put the side by side because i'm going to use intellij to fill out some of this so i'm going to go to my resolver and so yeah let's get started with the resolver and then resolver will just auto create my user methods from here because intellij will handle that part okay um perfect okay so i'm gonna need from resolver this is going to bring back a type user so i need my user entity it's going to say user entity and then we're going to need a constructor for this so we're going to export class user resolver and so this is going to use a constructor and there's going to be a private read only because we only want to use our user service so this is going to be a user service and then let me close this constructor so let's create our first mutation for create login and then we'll create a query for find off so we can make sure that it's all working okay so we're going to need a mutation and this is going to bring back a user okay and we're going to call this create user okay and then we're going to be using arguments and so this is going to be our create user input type and so then we want to place it right here and then we here's one of the specified create user input and i think that is the only that we need from them so after that is here we place our logic and since it's a a synchronous function we're going to do a try catch and then what we want to do is return await this dot us and then this is going to say our create user well this is going to be just use uh this dot create user and then here's we're going to pass in our create user input now this function does not exist but if we go here uh let me see okay perfect so all i want to do is add import statement and i want to use my service now i should have the option to create this create method perfect so this is what i want to do i just want to create my resolvers and then just auto fill out to my user service and then my services will handle my mongoose connections okay so we have our create user let's do one more for login so login is pretty much gonna have some of the same stuff mutation and this is gonna be of type string because first login we wanna bring back our token okay and then for our arguments okay okay for our arguments what we want is of uh let me see if we can call this login okay and we're just gonna need login email password email this is a type string and then we're just going to duplicate that one more and let's just password okay and then again we're gonna do a try catch and then this is gonna return uh await this.us.login and then we're gonna pass in the email and the password so let's come over here and let's create a create method login so create a password with parameters of password and email great great great now let's move on to our quarry for our find all so this is going to be of type query uh-huh and then from here we want to return is an array of users so this is going to return user array because that's how we define it here and now here's where we want to create uh before we can we find all actually i think yeah whereas we're gonna use our uh gql off guard okay so from here what we're gonna do is we're gonna use a guard from sgs and then we're going to specify the gtql off guard so use guard as a used card okay use guards and then we want to specify the gql and then we're going to do a import it and so we're going to do in a sync find all users and this is going to be a function no parameters and then we're just going to return await this.userservice.find all this doesn't exist let's put this in a try catch okay and then lastly we're just gonna create that okay okay okay and i think that is good for now everything is running great okay so for our service what we're gonna have to first do is create a connection to our like i mentioned earlier and some data from our user entity so let's get started with our mongoose so we're going to import from nest mongoose we're going to use our inject model this is what we're going to use to create to get access to the user model so the user repo you can think of this and then we're also going to need is from mongoose again the types and the model and types that we used earlier and then we're going to need our user document and our user so import from user entity and we're going to bring our user and our user document and we also need to bring then our imports our inputs dto so we have create input and we have update inputs and then we also need our jwt service so from nest jwt we're going to bring in our jwt extra service and then we're going to need import everything as bcrib so import uh just bcrypt i think i have recruit and then from here we want to import everything as we crypt and um from here i also want to import just a graphql error just because i like it a little bit more it's a little bit more um informative than the default so i want graph graph ql error okay that is everything that we need to bring into here okay so let's get started with our create user so since it's going to be asynchronous let's uh actually let me just do all of these because these are going to be try catch uh-huh okay great and we also need a con constructor and for our constructor here is where we're gonna create a private uh jwt service and this is going to be a jwt service and then we're also going to need our inject model that i said we're going to create and so this is going to be of type user.name that's how we're specifying it it's going to be a private variable that we're going to call it user model uppercase and then this is going to be of type model and this is where we specify our user document okay okay so right now we created our injection to our database which is right here or one of the connections because we originally did it in the user module and prior to that in the i module so as you can see how data i guess transcends down so now let's work on our create user so our create user is going to be taking in this input and what we can do is just um let's do a const let's first validate to make sure that this user does not exist because if it does exist we're just going to throw a new graphql error that says like you know knob or you already exist else we're going to just hash the new password with the bcrypt and then we're just going to save that user into our user model so let's say uh is user and this is going to be in a weight and there's going to be this dot user model dot find one uh find one by id find this should be fine by id okay and so from here we want to pass in oh you know find one sorry guys and so from here we're going to pass in the parameter of email we want to look for one of this email and so from here we're just gonna see if this person's email that they already created is if it's true then we're just gonna uh throw an error so then we say if is user is true because if it was false then it was empty then we could just go ahead and just create a new user so if this is true then we just want to throw a new graphql error and we're just going to say nah bro you already exist oh spell that wrong throw new graphql error okay and then else is we're just going to use uh so we're going to do our create user input this has a password and we're going to use weight bcrypt and then the bcrypt has a hash method that we're going to use to uh just hash our create user input dash pass dot password the salt uh length that we're going to use is 10 so this is a synchronous because right after that we could just do a dot then and then we would just want to return our updated password and then finally we can just return our await new this dot user model and then from here we're just gonna uh push in our create user input that has now our hash password and then just our saving method okay let me see what errors do we have here okay okay so i should have no any errors here no errors perfect and okay so let's move into our login and our login is just gonna validate it so it's just gonna check if user email and password is the same one that we have on database then pass in this query then you can give them a token i'll say you know nah bro you don't exist okay so let's just uh remove this unnecessary stuff that we need uh we don't need specifications for now okay and then from here what i want to do is i'm going to try finding a new user like this so i'm going to basically go to is user find one by email that we're using and then if this is true it's also where i want to compare with the bcrip function okay so let's do this so create a const and we're going to call this user again and this is going to be an await and this dot user model dot find one and then we're going to pass in the email so this is going to bring in a user or not so then we're going to return if res and await decrypt so let me wrap these and then we're going to compare bcrypt.com here compare and then from here is we're going to pass in our password that the user is uh trying to log in and then we're going to compare that with the actual user.password okay and so that's going to say yes or no and so okay so this is going to bring a boolean and if that boolean is true what we want to do is return a token else is where we can return a new graphql error and we say oh no homie wrong password email okay and let me add to my dictionary because homie triggered okay and then our res cannot find oh create i'm sorry this has to be user so if our user is true and then we want to compare if this is also true then to bring back our token else we're going to throw in a new graphql error and let's create our helper function for our we don't need to so let's just actually just throw our token okay and we're going to return this.jwt service this is going to have a sign in a sync and then what we want to pass in is our email that we're using right there and our user for underscore id is we're going to pass in our user dot underscore id okay all right so let's uh now go into our find query off so this is going to try away and for find out it's just going to return this our user model and it's going to say find and then we want to execute execute it okay so those are so simple that we have needed okay because yeah so we're not using this yet so let's check out our graphql server so we update this now now we go to queries we'll let's go to a schema we'll have an input of create user we have a mutation for create user login and then we have a find all and these are our types okay so let's create a query let's calculate create for login called find all so for our create we're going to create a user which takes in this import and let's just start i'm going to call this uh f name okay and then we want to bring back from there we want to get our id our first name we also want to get our email and then we want to make sure our password is hashed and then we also want to get our created ad and our roles to make sure that all this is being filled out on instance so let's go to our [Music] okay so now we have our id our f name email password and if i have this tool here called robo3d 3t i think robo3t and this is basically a gui for my mongodb is where we specify the name so here is called off nest so we have this and we go to collections collections we have a db that we just created so this is our instance of our use that we just created so we have an email our password is hash and we have our created iso type string all right now let's work on our login let's check our login because now our login should return a token okay and let me uh see so this is going to be a login input and there's going to be an email so email is of gonna be what did i put right here to test that test password should be password and if uh everything serves me right here now if we play our login we should grab our token key and let's just add one more ask to make sure it's working yep now homie wrong password okay uh let me put uh s here try that again now hold me wrong password cool and now for our find out quarry so again we have a quarter here called find all users and from all our users we want to bring our id and email and password now one thing's going to happen is um we're going to get a unauthorized so we go here and we go our find all unauthorized what we need to do is from our login we need to grab this key this token and then if you're using postman or xyz you always need to insert headers and graphql playground in the apollo playground it's like this you go to sp headers you create a property you know key value pair of authorization and then we're just going to paste this here so this token right oh no god damn it so i think let me just paste this again alright so this token right here holds our data of our id and email so we try to find oh it's gonna validate everything and then we see all the users that we want here great so let's create another one and you know just call this tony stank and let's say at iron man avenge the io okay same password and let's just create this user and now let's call steve rogers and let's say captain america saying password okay and now we say find all we have our users of captain america and the test that we created so this is coming out great now let's finish our other queries that we need so right here what we're missing is our update password and update user because update password is going to just be an omit type of password and this is gonna be a variable there's gonna we're not gonna need an input uh class for a password just for a update user so okay so let's do update user and update password and i'm just gonna pretty much copy this uh for user okay so i'm gonna come here and let's just uh duplicate this twice so now this is going to be called um update user input and then there's going to be update user password update user user password one final thing before we go on is for update user password and input we need to add the current user decorator i forgot about it i'll add it later okay so for update user input the only input that we can change is our current so what we're going to need is our current user and then the input that we have so uh let me just so this is going to be update uh this is going to be user where's update user input okay user input okay and i'm going to pass this lowercase so i can grab here so there's going to be of type update user input and we're going to pass this and there's not going to be a create let me bring this in what do you mean update user input here's where you want to get it from okay and then for arguments what you want to do is for our update user input we're going to need an id and so that id is going to come from our current user so current user decorator yup and greater and we're going to return a variable of user and then for the arguments is where we're going to use our update user input and then for our trial weight we want to use say this dot user service and here's where i say update update user okay now let me create this because it's going to tell me create method not the user great and then this is update user password and for this it's just going to say um what we first want to do is confirm the current password and so this is going to be of type string and then we want to update the new password so new pass okay and then again we're going to get the current user id from here um okay and then this is going to say update update password and so from here is we're going to pass in our current password and then our new pass and our id our user dot underscore id and so from here we're just going to create a new method which is going to take our current password and our user id should have gone in there okay and actually i don't need these because i'm not going to be deconstructing an object so yeah let me just put here the user the underscore id and then we're going to pass in the current user current password and then the new password perfect no errors let me check my code this is running perfect all right so we have our update user password and now list what else are we missing so we have update user and then let's do our find one by id and then remove user and get the current user okay so let's go over here and let's do one that's called uh at quarry and this is going to be returning back a string [Music] and this is going to be called uh with a used guard and then we want to use the gql off guard that we created sync find one and this is going to take an argument of uh id underscore id which is going to be underscore id and then we uh type um this type of underscore id okay so we have arguments and then this is where you have to specify the type and it's going to be a function so this is going to return a string and then from here we can specify our id variable and it's going to be of types that object that object id this would be an object id here perfect and this should be coming from mongoose add import statement from our mongoose great great great this should have no problem at all and okay now let's add our logic and so for our find one it's gonna be our this dot user service that find one so return this will return a weight this dot user service dot find one and we're gonna pass in the id here so then we're gonna create our method of find one okay and then we need our remem remove uh user mutation so mutation and this is gonna return let's say let's gonna say this is gonna return the user that we deleted you can bring a confirmation uh up to your you know entire it's entirely up to your project and so this is going to be on synchronous function remove user and it's gonna basically be the same arguments of id okay and so we're gonna say remove user at args and we're going to say uh under main strings under underscore id and so from what this return id is going to be return id of a string and then we can work on our logic which is going to be a try catch return await this user service and this is remove and then we're gonna pass in the underscore id let me create my remove method now this should be perfect all right and for fun when then our i guess bonus is going to be our current user so we're going to say our query and then from that we're going to bring in our current user that is signed in and we're also going to use our use guard and this is going to be a sync current user and so this is going to take a current user decorator that we used which brings a user type okay and what we want to do is just return that really and so just try another try catch and we'll return await this that user service dot fine and we're gonna pass in our user dot underscore id okay and so we have our resolver done let me just use one of my tools here structure okay and so if you look at our resolver we created our con a resolver we created our constructor whereas there's where we inject our um our depend our dependency injection for our services and then we create a method create user a login user a find one find all input user input update user input update user password and then our find one our remove user and our current user and now we're gonna move on to our user service so to finish out our remaining methods okay so let's come over here okay so for our update i think we have an update password that i do and update anything okay so for update user we're gonna be using a couple of same logic just to verify okay so for our let's get started on update user okay and let me go to my update user so if i just click on this it will take me to my resolver in line 51 5153 okay and where am i using this i'm not yeah update user okay so from here i want to pass in our user dot underscore id so that's how we're going to specify um the the user that we want to update and so now this is going to be taking an id with the user of the user input and so let's do again this we're just going to do a try catch and we can pass whichever ones we have here um so our update user what we want to do is a weight return a weight and we want to say this dot user model sorry about that and there's a function called find id and update so we're going to say find by d and update so the pro the arguments that this is going to accept the first one that we're gonna pass in is our id and then followed by our user input and finally we're just gonna have to execute it so let's do our it's gonna be passing in our underscore id and then it's going to be our update user input that we're bringing in have to create a new object and we're going to say a new property new we're going to say this to true so once we have that we can just go over here and then we're just going to execute it okay so that's our update user so it's going to be finding our pat our user by id and then it's gonna update with just first name or yeah i think you can only change email first name or last name no password or created that and then our password is gonna be pretty much the update password so we're gonna confirm our id is by type id and then we're gonna confirm the password okay so let's do a const of user and it's gonna say uh await this dot user model that find one by id and then we're just going to pass in id underscore id so that's going to return our user and then we just want to do is compare so if our user will await because this is going to be a decrypt asynchronous function so bcrypt.compair then again we want to compare our current password with our user the password and so if this is true what we do want to do then is our user.password we want to now hash it so wait be crypt dot hash and there's we're going to pass in our new password and again we're going to give it a salt length of 10 so now our user is updated with the new password and then we just got to return it so return await new this dot user model and we're gonna pass in our user that we just created and finally we're just gonna save okay we're almost done so we have update user update password and let's finish our find one and remove so if remove and find one are pretty much identical all we have to do is go here and this is gonna be await this dot user model and then this is gonna say um find one by d and then it's gonna say uh just find one and then find one by d and remove so really it's just respect to one so let's just find one and we're just gonna say find by this should be find by a d and then we're just gonna pass in our underscore id and that is it for our fine one and for our remove we're going to say find by id and remove find by id and remove and i think that is it for our authenticated graphql server throw caption called locally oh yeah so let's come back over here and if we refresh our page so we still have our current login so we have let's see all our users so we run this we have our test at task uh ironman avengers captain america and so let's see what new queries and mutations we added so we have now our create we have our create user input and we also have our update user input and then we have our queries now we have find one in our current user and we have our mutations we have the user input of the user password and remove user okay so let's get started uh testing them out so i'm gonna do first i'm gonna delete my user just to test the delete user so remove user and then here is a remove user it's going to take an id and this id is going to be this guy right here and this should be returning um my id and i just want first name so if i run this now it's deleted and if i do my find all i'm only going to get two now one thing i want to do is just change my uh bcrypt my off token right here as you can see because this was originally created for our login that we created here so i'm just gonna say ca and then i'm gonna say uh avg dot io and log in under as captain america so let's grab our token and just update it okay and now there's one mutation i want to do now i'm just gonna comment some of these out i don't need my crate mutation and so let's do a mutation of um let's see what we have here let's get current user okay so let's test that out let's do query current user and this is gonna be current user and what we're gonna return is the id the first name and the email okay so now if we do that we're gonna see that we come back as steve because steve is currently logged in last name and we have all this information so that queryworks find all users and let's get find one okay and let's test our query find one this should be fine one that's going to take an id all right before we move on let's fix this where it's instead of a string for find one we need to use a user my service you want to be careful here because now this is going to be referring to our js which is in our distributed folder we don't want that you want to get is our ts folders or us or typescript file so just you know i'm going to look out for that so i'm going to go to find one it's going to be taking in this type let me turn this this is all right so if we just refresh this and if we go here now we're going to have our type of id and then we want to get our first names this should be tony stark and email should be i am at avg oh i got the wrong password i got the wrong id okay so let me get tony's id which is right here so let me go over here and we're okay and so this is gonna get tony's information perfect and lastly we just gotta test our updates okay so we check all our docs we have our so we already checked current user find one find all we tested our create user login and we create and we tested our remove user list update our user input and user password okay so let's do a two mutations so there's gonna be mutation update input and there's gonna be a update user input and this is gonna be of user type input so the other thing we can change is first name so uh i think the only thing i wanna change for now for this example is here i'm just gonna to say t-o-n-y at avg.io and then let's just i guess first name change this to anthony as i mentioned earlier in the video we forgot to add the current user decorator so we try to run this right now we'll get an error because we're not passing our execution contact so let's add it right now for update password and our update user input that was it we just need because that's the only way to capture from the header the id and the email that we're placing via json web tokens so now we change this we're gonna see updated information and if we go to our find all we'll see uh now the first name for steve is captain and the email was changed from ca at abg to steve at abg okay and i think all it is working let me see i went over my update oh and my update password mutation is the last thing we're missing let's create a new mutation and let's say update password and this is going to be of the type update user pass and this is just going to say the current pass so let's just say password and then the new pass and let's just say password one now just to test it i'm going to put another two here and i'm just going to run what this works this is going to return a first name and let's just say password so this is going to say cannot return a null for non-nullable object meaning well we can throw in a better uh graphql error because what this means is that this is not working so we remove this we're going to get our right uh response because we're using the right credentials so for the update password we should throw in a if user is if user is false then throw the new graphql error of sorry homie uh passwords don't match okay and so we see our now we go to our login let's go to our login and if we try to log in say nah homie so let's just add our one to it with our new password and and now let's check out at steve at avengers.io and password1 and we're going to get our right token so we click on here and then if we pass this little guy over here and get our current user we're gonna see updated email updated first name all that good stuff and we also have an updated password so that was it um i'm going to place this up on github use this if you need to you know have a quick targeter plate a quick starter template and if you have any questions let me know have fun with it folks
Info
Channel: Fernando Boza
Views: 4,953
Rating: 4.9407406 out of 5
Keywords:
Id: XbG1B5vjo7M
Channel Id: undefined
Length: 71min 29sec (4289 seconds)
Published: Thu Feb 25 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.