User Auth In Unity | Register & Login Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
by the end of this video you will have a login and register system that works like this you'll be able to click register and the user will automatically log into the account that's just been created for them on your playfab dashboard so if I go and search you can see there's been a new player created with a display name of serger and if I go to login and I enter the email you can see you can log into that account as well by providing the email and password and then it Returns the username of the account provided but before we jump into the code make sure you watch my previous video where we set up an imported our playfab SDK into our Unity project So currently I have empty Unity project with some UI setup that just allows us to switch between login and register as well as some a few input Fields where users can enter their email password and username or they can log in with the email and password the login buttons and register buttons don't do anything yet so the first thing we have to do is in script create a folder called playfab and then we'll create a c-sharp script called playfab login manager so if you open this up in Visual Studio the first thing I'm going to do is create a constant string called Lost email key and mouse password key and what these are going to do is these are just going to store the values in player prefs of the email and password that the user last logged in with this will allow us to automatically log in the next time the player plays the game so the next thing you're going to want to do is create some references to the register input field so that would mean the email username and password input fields to the register UI and then you'll also want to create some references to the login input fields we don't need a username for login okay then what I'm going to do is I'm going to split them up into separate regions so you can do that by saying hashtag region and we'll call this region register and then underneath the register functions we'll say hashtag in region and we'll just do the same thing for login so login hashtag region login and then hashtag and region we'll start off with the register function then we'll move on to the login okay so for the register we're going to have a public void unregister press that's going to be called by the register button and then we'll have a private void called register and our register function is going to take email username and password as a parameter then in our on register pressed function we'll call the register function with the parameters of register email.txt register username.txt and register password.txt now next up at the top of our script we're going to have to be using playfab and using playfab.client models this will give us access to the playfab SDK then next up we'll create a private void called playfab failure and as a parameter we'll use a playfab error and we'll name it error and then inside this function we will just debug.log the error.generate error report this function is going to be called every time playfab encounters an error so now in our register function we can call playfab client API dot register playfab user and the parameter for this will be new register playfab user request and then the fields will have to set within the playfab user request will be the email is equal to email username is equal to username password is equal to password so this is just going to tell playfab what the details of the account are that you want to create so as the second parameter we want to tell playfab what we want to do if we successfully register the user and we will do that by saying success result and then we'll use a Lambda function to call our login function and we'll pass in our email and password that the user use to register their account to the login function and as the last parameter we are going to say play verb failure so basically what this means is we are going to tell the playfab client API to register a new playfab user with the email username and password that we've been given as input from the UI and if registering the user was successful then we're going to call the login function which we are so yet to create and if it fails then we are going to call the playfab failure function which takes in an argument of playfab error so you can do any kind of error handling or output that you'd like inside of this function you'll just want to change line 29 from username to display name is equal to username then here you want to say required by its username and email is equal to false because now we're using the display name instead of the username alright so now that the register function is done we can move on to the login and we'll just do the same for the login by creating a public void on login press which will be called by the login button then we'll have another private void called login which is going to take two parameters which will be the email and the password and now you can see we don't have an error here anymore then inside the unlock and pressed function we're going to call our login function and the parameters will be login email.txt and login password.txt then in our login function we will call playfab client API Dot Login with email address and the first parameter will be a new login with email address request and the fields we should set here is email set to email password set to password and the next part we'll have to do is we'll have to set info request parameters is equal to a new get player combined info request params and here we'll have to say get player profile is equal to true and these few lines over here are just telling playfab that you want to return information about the user's profile then again the same as our register playfab user function we need to take a success callback so success result we will then call the Lambda function and if we successfully logged in we want to save all this information to player press so what we'll do is we'll say playpress dot set string lost email key and we'll set that to email and playerpress.set string loss password key and we'll set that to password then lastly we'll say playerpress dot set string successresult dot info request payload dot player profile dot display name so if you didn't specify these parameters over here the info request payload dot player profile would be no because we didn't specify these over here so because we specified them that allows us to access the player profile and the player's display name and then just to display that the login was successful will debug.log successfully logged in user playerpress.getstring username and then the last parameter is the playfab failure callback so this should be our playfab login manage just script all complete so now back in unity what we'll do is we'll create an empty manager game object and we'll drag our playfab login manager onto it then we'll have to assign all the input Fields email username password and then login email and login password and on our register button what we'll do is we'll drag and I'll manage again object and say playfab login manager dot on register pressed and on our login button we'll drag and drop our manage again object in here and we'll say playfab login manager dot on login press okay so now we need to just go to our playfab edit the extensions window and you'll want to click on this playfab tab and then click on make playfab shed settings you'll want to make sure that you've selected your playfab shared settings and then you'll go to game studio and you'll have to reselect it over here in your settings and you should see that your title ID and developer secret key is automatically filled out for you in your shared settings asset now if I run the game and I enter Market gmail.com username I'll just say search and entering a password I'll type out my password and click register I can go to the login and I can type in Mike gmail.com and I'll enter my password and login and then you can see here successfully logged in user surge perfect that is now our user authentication using playfab in unity if you want to find out how to send password reset emails to users in case they forgot their passwords make sure you hit the Subscribe button and keep up to date with the latest videos because I'll be posting that next
Info
Channel: Skye Games
Views: 2,297
Rating: undefined out of 5
Keywords: User auth in Unity, playfab user auth, user login Unity, authenticate users in Unity, Playfab Login Unity, Playfab Authentication, playfab authentication unity
Id: YxF5pXyrZdU
Channel Id: undefined
Length: 9min 10sec (550 seconds)
Published: Fri Jul 14 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.