Login using Google Account with Codeigniter

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends in this video tutorial we are going to learn how to login into website by using Google account with codeigniter framework nowadays most of you sir have Google account so if your website can provide feature of log in with Google account then user can directly login into website without register into your website most of the websites use Google authenticate API for login using Google account because it will easy to integrate in our website and powerful way to verify user login details while login into our website using Google account so in this tutorial we will explain you how to implement log in with Google open authentication with codeigniter from work now let's start discussing this topic so this is our codeigniter chat database and here we have chat user table with table column like user ID login ID first name last name email address profile picture created at an updated at when user has been logging in our website using google account then user data will be stored in this table if user login at first time but if user login in our website using Google account then only that user data will be updated so here user do not want to register on our website then can directly login into our website by using Google account for make log in with Google account so first we have to register our application on Google developer console website so first we have go login into our Google account you after login into Google account we have go to console dot developers.google.com URL from this URL we can get Google API credential for login using Google account here first we want to create new project so we have click on create project link here after this here first we want to define project name so here we have enter whoops lesson demo and click on create button after click on create button here we can see project with named whips lesson demo has been created which we can see on web page now we have click on logo of Google API and then we have go to home page of Google API console now we want to select our newly created project so we have clicked here and here we have select our project with named webs lesson demo we have click on open authenticate consent screen menu after click on this link here new page has been loaded here we have select external option and click on create button here in application name field we have enter whips lesson demo and click on Save button you now we have click on credential link from left menu after click on save button web page redirect to this credential web page so we have click on create credential button and here we have select open authenticate client ID menu and here page has redirect to new web page here we can see different application type option so here we have select web application option after select this option here we can see all form field has been enabled and here in name field we have enter webs lesson demo next we want to define redirect URL here we can define redirect link on which after login into Google account then web page will be redirect to this link so here we have defined this link lastly we have click on create button after click on create button our credential is ready so we have to copy this credential like client ID and client secret key so this way we can generate API key for meit login using Google account now we have proceed for download Google API PHP client library for this we have open command prompt and go-to CodeIgniter framework library folder here first we have run composer command here we will use composer for download Google PHP client library after this we have write command like composer required google slash api client 2.0 this command will download library in CodeIgniter library folder so this way we can download Google PHP client library this process will take some time for download library now we have proceeded for login with Google so this is our codeigniter framework folder in this library folder we have download Google client library in CodeIgniter framework first we have open config folder and here we have open autoload dot PHP file here we have $2 autoload libraries variable and here we have defined database and session library so it will load both library automatically after this we have open config dot PHP file and here in dollar config baseurl variable we have defined base URL of this codeigniter application next we want to make database connection so we have open database dot PHP file and here we have didney property like host name set to localhost username set to root password set to blank value and database name set to codeigniter chat now we have proceed for coding so here we have already made Google login dot PHP in controller folder Google login model dot PHP in model folder and Google login dot PHP in View folder first we have open Google login dot PHP in controller folder and here we have write construct magic function this function will be called if object of this class has been created under this function we have right parent class with construct method it will execute parent class construct function next we have right dollar this with load object with model method and under this we have right Google login model it will load Google login model class here so we can use method of this class here now we have created login function under this function we have right include one statement with a pot dot libraries folder name slash vendor folder slash auto load dot PHP here we have used Google API PHP client library directly under controller method below this we want to create client requests to access Google API so here we have write dollar Google client variable is equal to new Google client class after this we want to set client ID so here we have write dollar Google client variable is equal to set client ID method and under this we have defined client ID which we have G at the time of creating Google API credential now we want set client secret key for this here we have right dollar Google client variable with set client secret method and under this we have defined Google API secret key next we want to define redirect URL so here we have write dollar Google client variable width set redirect URI method and under this we have write defined redirect URL which we have defined at the time of creating google api credential now we want to add scope for access Google API for this here we have right dollar Google client variable with add scope method and under this we have write email same forget profile data here we have write dollar Google client variable with add scope method and under this we have write profile so here our code is ready for client requests to access Google API after this we want to authenticate code from Google open authentication flow for this we have write if statement and under condition we have write a set function with dollar get code variable if this condition true that means this page has received authenticate code from Google open authentication flow under this block we have right dollar token variable is equal to dollar Google client variable with fetch access token with auth code method under this method we have right dollar gate code variable this method will attempt to exchange a code for valid authentication token after this we have again right if statement and under condition we have write a set function with dollar token error variable this condition will check is there any error acure while get authenticate code token if this condition true that means there is no any error occur in getting token next we want to set access token so here we have write dollar Google client variable with set access token method and under this we have write dollar token access token variable after this we want to give access to Google user for this here we have write this with session object with set user data method with two argument in first argument we have write access token as session variable name in session data argument we have write dollar token access token variable it will store this data in session variable so Google user can access our application all pages based on value of this session access token variable now we have right Dola Google service variable is equal to new Google service open authentication to class with object like dollar Google client variable here we have create new object forget user profile data forget Google user profile data we have right dollar data variable is equal to dollar Google service variable with user info with get method it will return Google user profile data like email name profile image detail below this we have right current data time variable is equal to date function and under this we have defined date and time format next we want to check logging user data is already stored in our database or not for this here we have write if statement and under condition we have write dollar this with Google login model with is already register method under this method we have write dollar data ID variable if this condition true that means user data is already stored in our database and it will execute if block of code for update user data but first we have go to Google login model class and here we have make is already register method with dollar ID variable as argument under this block we have right dollar this with database object with where method with two argument in first argument we have right login ID and in second argument we have right dollar ID variable it will make where condition next we have right dollar query variable is equal to dollar with with database object with get method and under this we have write chat user table name it will make select query and execute it now we have right if statement and under condition we have right dollar query variable with numrows function value is greater than 0 here numrows method will return number of rows affected after execute above query if this condition true then it will return true otherwise it will execute else block and return false now we have go to Google login controller and here we have go to if block of code and here we have write dollar user data variable is equal to array in first key we have right first name table column name and in value we have right dollar data give name variable in second key we have right last name table column name and in value we have right dollar data family name variable in third key we have right email address table column name and in value we have right dollar data email variable in fourth key we have right profile picture table column name and in value we have right dollar data picture variable and in last key we have right updated a table column name and in value we have right dollar current date time variable value so here we have store data in dollar user data variable in array format below this we have right dollar this with Google login model with update user data method with two argument in first argument we have right dollar user data variable and in second argument we have right dollar data ID variable this method will update user data now we have go to Google login model and here we have make update user data method with two arguments like dollar data variable and dollar ID variable under this we have right dollar this with database object with where method with two argument in first argument we have right login ID and in second argument we have right dollar ID variable next we have right dollar this with database object with update method with two argument in first argument we have right chat user table name and in second argument we have right dollar data variable it will make update query and execute update query and it will update data in database now we have go to Google login controller and here suppose user not found in our database then it will execute else block of code and here we have write dollar user data variable is equal to array in first key we have right login open authentication user ID table column name and in value we have right dollar data ID variable in second key we have right first name table column name and in value we have right dollar data give name variable in third key we have right last name table column name and in value we have right dollar data family name variable in fourth key we have right email address table column name and in value we have right dollar data email variable in fifth key we have right profile picture table column name and in value we have right dollar data picture variable and in last key we have right created a table column name and in value we have right dollar current date time variable value so here we have store data in dollar user data variable in array format below this array we have right dollar this with Google login model with insa user data method and under this we have right dollar user data variable this method will insert user data in database now we have go to Google login model class and here we have make insert user data method with dollar data variable under this method we have right dollar this with database object with insert method with two argument in first argument we have right table name chat user and in second argument we have right dollar data variable this method will make insert query and execute it and it will insert data in chat user table now we have go to Google login dot PHP file and here we have write dollar this with session object with set user data method with two argument in first argument we have write user data as session variable name and in second argument we have write dollar user data variable it will store this array data in session variable with name user data now here we have write if statement and under condition we have write not operator after this we have write dollar this with session object with user data method and under this we have write access token this condition will be true if this access token variable value is not found in session that means there is not any user has tried to log in with Google account so under this block we want to make Google login button so here we have write dollar login button variable is equal to and contact with ref is equal to dollar Google client variable with create auth URL method this method will create URL to obtain user information under anchor text we have right image tag with source is equal to image path here we have store goggle login image in this asset folder it will make URL for Google authorization after this we have right dollar data login button variable is equal to dollar login button variable below this we have right Dola this with load object with view method with two argument in first argument we have right Google login view file name and in second argument we have right dollar data variable now we have go to Google login dot PHP view file and here we have write if statement and under condition we have write not operator a with a set function and under this function we have right dollar login button variable if this variable value is not set then it will execute if block of code otherwise it will execute else block of code suppose above condition falls then it will execute else block of code and here we have right echo statement with dollar login button variable value but suppose above condition true then it will execute if block of code and here we have right dollar user data variable is equal to dollar this with session object with user data method under this method we have write user data so here we have store session data under this dollar user data variable first we want to display user profile image so here we have right echo statement with image tag and here we have write source is equal to dollar user data profile PC fuel variable it will display login user profile picture below this we want to display first name and last name data so here we have write echo statement and under this we have write name text with double colon and here for display first name here we have right dollar user first name variable and for display last name here we have right dollar user data last name variable same way for display email details here we have right echo statement with email text and for display email here we have right dollar user data email address variable after this we want to make logout link so here we have write echo statement and here we have write and Curtin breath is equal to base URL function after this we have write Google login controller name slash logout method and in anchor text we have write logout it will make logout link now we have go to Google login dot PHP controller file and here we have make logout method under this method we have right dollar this with session object with unset user data method under this method we have right access token this method will remove session access token variable value same way we want to remove session user data variable value so here we have write dollar this with session object with unset user data method and under this we have write user data after logout from system for redirect page to login page here we have write redirect method and under this we have write Google login controller slash login method so it will redirect page to login method of Google login controller so here our code is ready now we have checked output in browser friends here in browser tab we have enter base URL / Google login controller / login method then here on web page we can see login with google image on web page now we have click on image after click on image here page has been redirect to Google login page so here we have enter Google login account details after Phil Google login details we have click on login button after click on button here page has been redirect to localhost link which we have defined redirect URL you here on web page we can see google user profile details like profile image user name email address and lastly we can see logout link for sign out from system here we have refresh web page after refresh of page here Google user details has not be removed this is because we have store in session variable so in system we can validate Google user with this session variable value we have click on logout link after click on logout link page again redirect to login page and here we can see image light login with Google on webpage lastly if you have any query or inputs regarding this video tutorial please comment your query or input in comment box or if you like this video tutorial please share with your friends or even you can also share on social media also
Info
Channel: Webslesson
Views: 17,103
Rating: undefined out of 5
Keywords: API, codeigniter google login, google oauth, login with google account in codeigniter, CodeIgniter, codeigniter login with google account, google login codeigniter, login with google account, Google API, login with google, Google Login, google authentication codeigniter, google account in codeigniter, Google oAuth, google login in codeigniter, codeigniter google login api, login with google in codeigniterSocial Login, login with google codeigniter
Id: Kd9Yp9CcVIY
Channel Id: undefined
Length: 33min 31sec (2011 seconds)
Published: Sat Mar 21 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.