Login with Google using PHP - Beginner Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right guys so let's start integrating google login in our php page our website [Music] okay so first of all let me create a folder i'll just call it g-login oh my god it's not just z-login all right now in here what we need to do is uh first we need to install the google's sdk or set up the google sdk so to do that i'll just go cd and go to this directory so this is the directory where i'll be installing my google sdk so to do that we can use composer and this is the command right and you just hit enter and once you do that composer will be installed as you can see here composer.json is already here not composer my bad the google auth library or sdk is being installed okay so now we will create a page that will have a button that says login with google and when we click on it it will be redirected to the google uh login page and then it will be returned back to our page where we can access the user's email and username and all that good stuff so i'll just create a new file here let's call it uh login.php and in here first of all what we need to do is we will do require once what do we do we require vendor autoload.php if you are using if you have been using composer libraries or php you will know this stuff and now first and now we need to get our client id and that stuff but where do we get this client id client secret and retired well we need to get this from google itself so first i should have started with this first what we need to do is now i have already done this but i'll show you how to do this again first of all we will create a new project here all right if you haven't already done this you can just follow along but if you have you can you know if you haven't you can follow along so you create a new project let's call this my g login and just create the project now it's still working on it now since it's done you can click select project my g login or you can go here and select your available projects from that place all right so you can select from here also if that didn't work for you you can select any picture from project from here and then what we need to do is we go to api and services you go to credentials and you will create credentials you create oauth client id and then you need to do this you need to configure consent screen now i'll just do it this external at the moment and if your app is public you'll want to do this external also you just hit create now what's your app name uh it's just my g login support email you can just add your email here and just add our email an app logo you don't need to add this at the moment i think an app domain you can add anything here at the moment i'll just add localhost okay and the same thing here and add the same thing here now in here you'll want to add your actual pages your website's home page or websites or apps privacy policy link and stuff but i'm just doing this at the moment because we don't have anything and just add your email here all right now you need to add domain all right let's let's not add this at the moment let's see if it works or not because if you are developing at the moment you'll not have the domain okay it worked so you don't need to add scope also this scope is actually regarding how much data you want to access for just email and stuff you don't need to do this just hit save and continue and we are done with this you can go back to dashboard now now your oath consent is set up so you can go to credentials and you can go create credentials oauth client id our application type is let's say web application and name is ig login and in here since we want the user to be redirected to our localhost login page after login because that's where we will be handling what we do with that data the data like username user email once he has logged in so we will just add our localhost name here localhost glogin slash login dot php because if you go here and check this is where uh g login is where we have our login page all right so just hit save and once this is saved you can just copy those client from this and if you open this you'll see you can check credentials here like client id client secret and all that good stuff all right now let's go back to our code but before that let's copy this first and now we have our client id here and then we'll have client secret what is our client secret this is the guy in secret let's copy this too and then we have our redirect url it's the same this one i know what am i doing it's the url that we had put there g login login.php all right now we need to create a client request to google creating client request to google all right so how do you do that you first initialize a new google client right and then what we need to do is we need to set our details set client id what is the client id it's client it id capital here and then we need to send the client secret to the client secret is all right and then we need to set the redirect url to you actually that the redirect url is direct url all right now once this is set we can use this to get the data like the name we're not actually adding the name here we're just saying we want name we're adding name in the scope all right so for that the keyword is actually profile not name you can get name later once we once the user has allowed login and then we add scope and we add email so these two things have different things that you can get from these scopes like from profile you can get username date and date of what and that stuff all right so different scope has different things that you can get from now since this is also the page where we will be redirected after the user are successfully logged in we need to handle uh that case where we will be opening this page after the login has been complete so to do that what we can do is we check if there is something called the code here so we check if is set we have received a code here if you have received a code that means that we have been bought here by google alright so in here we can get user name email all that good stuff but if you are not today to this page by google and we are opening this on our own we need to add a button here that will say login using google all right so let's do that no not this will add a button and on click we want to go this thing to client create auth url all right so this the url will be generated by google itself all right this is it we are done but now so okay it's not sudo it's good okay so if we check here we just have our client details here and the redirect url here and we are initializing the client google client that we uh installed using composer google sdk so this client is accessible only if we have installed the google oauth sdk that we did using composer if you know if you remember during the first step and then we set our details here like client id client secret and set to direct url and then we have the scope like profile and email and then this is where we will be handling our stuff once the user logs in and if the user is not logged in and this is the page that the user has just opened we will show this button that says login with google and clicking this will redirect the user to a url created by our google client okay now let's run this so let me turn on the xampp first now you go localhost g login and login.php there you go on the file method so we have we are getting this error here it says it's undefined redirect url okay it's not rewrite url it's actually to direct uri here my bad now we reload this now we have this button here on clicking this we should be redirected to google and we are rightfully so if we click back here now we are redirected to our login page again but we are not handling this code here see we have this code here but we are not handling this code so now let's go ahead and handle that and get that name and you email okay so first of all once you have received the code we can we use that code to get a token using the google client so we go client fetch access token and in here we pass this code so once we have the token we can use that token we need to set access token this token is a token that we just got and finally we are getting user profile all right how do you do that you create a g auth object that is new google service so many objects right okay once this is also done we need to pass our client here all right finally we get our google info here finally so we can use our g auth to get user info so what do we do we just write user info here and then we get it okay so since we have our google info here we can get the email from google info like this google info we get email and we can get a name like this google info name all right we are now done now we'll just echo and say welcome user welcome what is your name and you are registered using email what is the email it's the email all right now let's check this again and see if this is displayed or not okay let's reload this this will not work let's start this from the beginning localhost z login login.php we have our button here we click here we are redirect to the google page we choose the email which we want with which we want to log in so it's saying undefined method fetch access token so do we not have what is the name client fetch access token okay it's not just this it's with auth code there are other ways to get access token but we are since we are using this code we need to use with auth code all right we'll go back here now click mail the code city class google service was not found oh my god i'm having so many problems here where is google service or what okay it's oh what true we have moved on from oauth hits or two since a long time okay i hope we are done with the bugs now and we are all right so it's saying welcome to code city you are registered using email mail the call city at the gmail.com this is it guys now we have just and displayed the email here you might want to you know save it into a database or do all that good stuff there but since this is the short tutorial that teaches you how to do login using google we have done that congratulations if you have any problems let me know below in the comment section i'll try to help you out peace
Info
Channel: The Code City
Views: 31,154
Rating: undefined out of 5
Keywords: google login with php, google oauth login, login with google account using php, google login, google php login
Id: i5IybpgcInY
Channel Id: undefined
Length: 17min 59sec (1079 seconds)
Published: Sat Mar 27 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.