Laravel 11 - Auth Api - using sanctum ( Register, Login, Profile Data and Logout )

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this session we are going to see how to create a a API by using Lal 11 sanctum uh in a API we are going to create APA for register user login user getting profile details and log out okay this is the official uh official website of laal I will go to documentation first we will create one U project so creating the project okay copy this command composer create hyphen project l/ colon uh this is the version 1.0 and project name okay I will open the command PR okay I'll change the project name as o API okay in my system I install the examp uh P PHP version above 8.2 okay it's creating it will take some time to create the project okay project has been created okay I will open this project in vs code first I will uh go inside the directory okay this is the folder for LEL 11 okay we can see EnV composer Json we will open and check it LEL framework land. okay uh first uh we will run this project by using the command PHP artian sh PHP or ser we'll check the homepage now this is the Lael 11 homepage it's working fine okay before that uh I want to show you some folders database okay not database okay first I'll will show that here the sanctum was not installed in lar 11 we supposed to install that here after installation you will get the file here inside the config s. PHP okay I will open the boostrap app. PHP see here here it's only web there is no AP option here now next I will go to Roots folder here also web is only available there is no ap. PHP okay first I will go to the official website l s okay first step we have to install this PHP auti install AP just copy this one and I'll go to V code I will open the terminal again I will open one more terminal by using plus icon okay I will install this so I didn't create the database so we need to do this step now I'm going to give no okay uh now we will check the changes again I will open the composer. Json see here s sanctum 4.0 has been installed uh boostrap app.php see here uh the setup was created uh finally um okay see here sanctum PHP file has been created inside the config here we have to do one more correction that is we have to open the model user model here um app models user model uh here we have to include has API tokens from the LEL Santa okay I'll will show you AP token authentication okay so here uh they clearly mentioned your user uh model should use this okay I will copy this line I will paste it here next copy this one has Factory notifiable it's already there in the code so add this one comma okay save it okay everything s s installation has been over now we will create a database and do the migration here I will create one new database LEL APA okay copy this name open. EnV file here default is U SQL I'm going to use MySQL so I will change this to my SQL okay remove this one okay I will paste my DB name root I don't have a password so empty I'll save it so currently I am having empty database now I'm going to do the migration I'll clear this everything command is PHP RTI migrate I'll put enter see I'm facing some issue uh that is uh unknown coll uh this one it says I'll go and check it because uh it is utf8 mb4 General C okay it's it's not issue we will add some variable in the uh EnV file automatically this error will disappear so add this keyword dbor collash and yours collash name so now PHP artian migration will work okay I will clear the screen PHP partition M so it worked fine okay let's go on okay it is empty database I will reload this database page see all the table has been created uh this one personal access token will be create will be created after sanctum installation only if you didn't install now you will get a remaining nine tables only okay uh next uh we will check the roots see AP root has been automatically created I clear everything okay so this is not record I will commment this one I'm going to create one uh API controller so PHP ran make controller API folder slash API control okay we'll go and check it HTTP controls AP AP controller okay uh so first we are uh uh actually uh in this example we are going to create a for register login then profile finally log out so we have to add four function inside the AP controller and we will test it okay I'll remove this line we have to call this path so copy this one I will add it in the API uh app HTP controllers API slash AP controller we have to include the path we complet it first we will do the register AP actually in this login and in uh in this API example login uh only login and register both doesn't need any middleware for authentication we need middleware for only profile and log out okay it's a p method okay my function name is register so I will add the save it I will add here reest already included the so we can use this first we will check AP is working or not then we will add the functionality save this one so I will open Postman this is a fresh collection I will add one request this is a register API okay this is a post method copy this one slash register so our path slap SL register okay register uh we are going to enter name email and password we will uh first of all we will check this is working or not okay first I will save this one okay I'll so it's working fine oh sorry not this one okay it's working fine this is the previous request that's why it appeared I closed the old connection so AP is working fine we will add the functionality okay first we have to include the user model use apps SL model models slash user next we have to add Au hash and validator we will add that use Eliminator support foret out we need a hash next validator we will do the validation okay save this okay once again we will check it is there any error while including so I can find no error first we have to validate user okay Lau provides a default validation so we can use that validated make reest all oh sorry it must be so our input will be name so this is reir next to email this is record only and validation it must be email then uh uh we don't need duplicate user so we have to check with uni unique table name is users we have to check the column email okay I will open the browser here I will click the users tables this is a user table here we have to check the email it must be unique uh if same uh email it comes means it's a duplicate error has to come so I will open the code so unic we are checking finally password so password and just setting as record first condition if validation fails if validate user fails then we have to return the error response so for APA we are going to use return uh response written type must be Json so we will include that inside AR symbol we will add the necessary uh respons status must uh if it is error means status must be false status false I will add the message validation eror next error message what are the errors available errors sorry this is already validate user turns to errors so L will return all the errors okay save this one first of all we will check with the empty response okay open the postman this is previous message see we got all the errors so now we have to include everything name email password okay I add name as with email Rya do sunflower atgmailcom okay I will check so only password field is reced is coming so everything is working fine so we will add the function first we will store the user value in the t uh database table model is a user so add user I think okay user only I add okay create okay we are going to enter only three parameters name email and password okay uh after the creating user in the table we have to return success response uh I think uh here we can add the Response Code also as of now for error message I will add 41 success Mees I will add 200 okay let me check this here I got 200 I will set it again yeah I got it 41 this is validation here if you want you can use it otherwise leave it so same thing response so straight away I am giving return so I'll copy this one I'll paste it here success response is 200 status true message we can give user created successfully okay uh uh for login and the register we have to return the token of the user by using the token only you can get the profile details so I'm going to share the token same thing we will do it in the login also the login functionality also we will share the token of the user user create token uh it will come from the sanum so now just call the function user tends to create token in caps it's a API token I'm going to share plain text token okay status true message user created SU Y and a to okay we will check now how it works okay once again I will click it pass field is required uh my table is empty I will add 1 2 3 4 5 6 okay uh status true uh message user created successfully then I I received one token okay uh let us check the table okay I refresh the table so uh we successfully created register AP my name uh email ID and password has been registered here and uh personal access token you can see the token here okay uh next we will uh create a login um API before that we we need to handle 500 error also that we will do that for that uh we will create if uh some table column missing this kind of we can handle it so I will give a try catching if you want you can add it otherwise it is not record try catch throwable copy this one p it here here mention 500 status is false pH tends to get message see this one okay we included the trach also okay I'll check the API once again it's working fine uh I already registered this email ID so it gives the email has already oh sorry the email has already been taken okay validation also working fine okay I will go to AP next I'm going to create a login okay save it okay I didn't save this one sorry okay public function l in um I'm going to use try and catchy for this one so 500 errk already for login you will need only email and password okay we'll check now we will copy this one also uh remove the name so for Ling we are going to use email and password so email and password validation we are checking then error message it will give save it we will create and check this validation now I will duplicate this one L it's login okay it I'll check now okay it's validation is working fine uh after that we have after Val validating everything we have to check this record is present in our table or not okay we will use l for checking this one if not or attempt request only we are going to check email and password it means there is no record in the table or mismatch in the password also okay we will check the column names email password copy this error paste it here email and password start match with our record save it we will check this condition now email password I add withu gmail.com password 1 2 3 4 5 6 okay status false we can go check it in the pretty view status false then uh because uh there is no emailed email ID rub. here it's available is only rub. sunflower so I will check the okay correct one we now we will add the functionality for the current one if it is available you have to take the record dollar user is equal to user model so user there email password is already validated there so no need to put any V condition this is more than enough so we have to check the email and uh if it is available take the record so where email equal to requested email first then it's a 200 response okay so copy this one paste it here user logged in successfully so if it is successful user we have to send them token so it will uh generate token and provide to the user okay we will check now okay I will click Send sorry this this oh it's a post meod only noes r withl at gmail.com password 1 2 3 4 5 6 if it is successful it has to return the token we will check now it's working fine username and pass password is correct so it Returns the token we will check the table now personal access token so it created one uh Row for my login okay so keep as it is so our login AP has been completed next we will uh get the profile informations by using profile URL for this URL we have to uh include the uh middle work uh you middle work because uh login user data only we supposed to take so uh the user must be already logged in I will so the token is enough for us to take the record of the logged in user okay I am going to use root group this is for a Prof okay first I will put the use group okay call back function I will add it oh here I'm going to call two apis one is profile okay we'll create the function another one is uh log out okay we need to include the middle ver [Music] art santum okay we will check uh this URL first it's working or not something okay I'm going to get the user information only so I will use get method this profile profile okay I will click uh send login not defined okay okay I will check [Music] now okay first we will add the functionality and check it if it is profile now we have to pass the in token or in the header I will show you first we will get the user data user data is use o function user based on the token it will provide you all the information then it will we have to return 200 response status true this is profile information profile information and we have to return the data data is user data next we have to return the ID if you want you can return the ID okay save this one okay sorry it's okay uh I will go to the so profile functionalities has been over API also we done it group middle sanum okay okay next uh in headers we have to add two things one is accept I will hide it accept application. Json next authorization authorization we you supposed to add the BR which is already available in the space be token in the L so I already logged in so I copy this be token be space uh paste the token I'll send now so it's it's working fine this AP uh I didn't give you any authorization uh that's why error came so status true profile information ID one Ru my email ID and all my details and including that ID so what I will do I will here change one more ID something I will change in the PA token if it is not right token you will get the message unauthenticated I'll change to right now I'll give send so I got my profile information so the final one is log out we'll do that just uh remove the token that's more than enough okay public function log out the token is the input for this API hot user tokens deleted then we have to return the response user loged out so no need this data I'm going to return just an M status is more uh status and message is more than enough save it so AP log out same thing the only change is uh you have to change the log out instead of profile then we have to pass the uh user token here so I will duplicate this one I'll copy this PA token accept application Json authorization I'll P the beer token save this one I click Send so logged out okay uh now I log out from the system so again uh by using the same token I I will get the I will try to get the profile information now so un un authenticate error came so it's everything is working fine
Info
Channel: Rubyvidhya Ammu
Views: 1,911
Rating: undefined out of 5
Keywords:
Id: st7DtZA85fw
Channel Id: undefined
Length: 44min 2sec (2642 seconds)
Published: Sat Apr 06 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.