{"id":1175,"date":"2019-04-10T14:48:35","date_gmt":"2019-04-10T09:18:35","guid":{"rendered":"http:\/\/www.siriinnovations.com\/blog\/?p=1175"},"modified":"2019-04-12T13:17:19","modified_gmt":"2019-04-12T07:47:19","slug":"login-with-googleoauth2-0-account-in-codeigniter","status":"publish","type":"post","link":"https:\/\/siriinnovations.com\/blog\/login-with-googleoauth2-0-account-in-codeigniter\/","title":{"rendered":"Google oauth 2.0 in CodeIgniter"},"content":{"rendered":"\n<p>Google OAuth API is the simple and powerful way to integrate login system in the website. Google OAuth Login API let you allow the user to sign in the web application using their Google account without registration. The main advantage of Google login is the user can log in to the web application with their existing Google account without register account on the website. Nowadays almost all web users have a Google account, Sign-in with Google helps to increase the user\u2019s subscription on your website.<\/p>\n<p>Step1:-<br>Create Google API Console Project<br>-&gt; Go to <a href=\"https:\/\/console.developers.google.com\">Google API Console.<\/a><br>-&gt; Create project,Auth credentials for the project.<\/p>\n<p>Step2:-<br>-&gt; Download the latest version of codeigniter and extract into your root folder.<\/p>\n<p>Step3:-<br>Create a google_config.php in \/config folder, place below code.<\/p>\n<pre class=\"code\" lang=\"php\">defined('BASEPATH') OR exit('No direct script access allowed'); \n\/* | To get API details you have to create a Google Project \n| at Google API Console (https:\/\/console.developers.google.com) \n| client_id string Your Google API Client ID.\n| client_secret string Your Google API Client secret. \n| redirect_uri string URL to redirect back to after login. \n| application_name string Your Google application name. \n| api_key string Developer key. \n| scopes string Specify scopes *\/\n$config['google']['client_id'] = 'Google_API_Client_ID';\n$config['google']['client_secret'] = 'Google_API_Client_Secret';\n$config['google']['redirect_uri'] = 'https:\/\/example.com\/project_folder_name\/user_authentication\/';\n$config['google']['application_name'] = 'Login to example.com';\n$config['google']['api_key'] = '';\n$config['google']['scopes'] = array(); \n<\/pre>\n<p>Step4:-<\/p>\n<p>Create Controller Auth.php In yor controllers\/ folder.<\/p>\n<pre class=\"code\" lang=\"php\">\nif ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Auth extends CI_Controller { function __construct() { parent::__construct(); $this-&gt;load-&gt;library('google');&lt;br ?--&gt; }\npublic function index(){\n$data['google_login_url']=$this-&gt;google-&gt;get_login_url();\n$this-&gt;load-&gt;view('home',$data);\n}\npublic function oauth2callback(){\n$google_data=$this-&gt;google-&gt;validate();\n$session_data=array(\n'name'=&gt;$google_data['name'],\n'email'=&gt;$google_data['email'],\n'source'=&gt;'google',\n'profile_pic'=&gt;$google_data['profile_pic'],\n'link'=&gt;$google_data['link'],\n'sess_logged_in'=&gt;1\n);\n$this-&gt;session-&gt;set_userdata($session_data);\nredirect(base_url());\n}\npublic function logout(){\nsession_destroy();\nunset($_SESSION['access_token']);\n$session_data=array('sess_logged_in'=&gt;0);\n$this-&gt;session-&gt;set_userdata($session_data);\nredirect(base_url());\n}\n}<\/pre>\n<p>Step5:-<br>Create A view home.php file in view forlder.<\/p>\n<pre class=\"code\" lang=\"html\">&lt;html&gt;\n&lt;head&gt;\n&lt;title&gt;Google authentication library for codeigniter&lt;\/title&gt;\n&lt;!-- Compiled and minified CSS --&gt;\n&lt;link rel=\"stylesheet\" href=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/materialize\/0.96.1\/css\/materialize.min.css\"&gt;\n&lt;link rel=\"stylesheet\" href=\"\/\/maxcdn.bootstrapcdn.com\/font-awesome\/4.3.0\/css\/font-awesome.min.css\"&gt;\n\n&lt;!-- Compiled and minified JavaScript --&gt;\n&lt;script src=\"https:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/2.1.4\/jquery.min.js\"&gt;&lt;\/script&gt;\n&lt;script src=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/materialize\/0.96.1\/js\/materialize.min.js\"&gt;&lt;\/script&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;div align=\"center\"&gt;\n&lt;h2&gt;Google authentication library for Codeigniter&lt;\/h2&gt;\n&lt;\/div&gt;\n&lt;div class=\"container\"&gt;\n&lt;div class=\"row\"&gt;\n&lt;div class=\"col s12 m6 offset-m3 l6 offset-l3\"&gt;\n\n&lt;?php\nif($this-&gt;session-&gt;userdata('sess_logged_in')==0){?&gt;\n&lt;a href=\"&lt;?=$google_login_url?&gt;\"class=\"waves-effect waves-light btn red\"&gt;&lt;i class=\"fa fa-google left\"&gt;&lt;\/i&gt;Google login&lt;\/a&gt;\n&lt;?php }else{?&gt;\n&lt;a href=\"&lt;?=base_url()?&gt;auth\/logout\" class=\"waves-effect waves-light btn red\"&gt;&lt;i class=\"fa fa-google left\"&gt;&lt;\/i&gt;Google logout&lt;\/a&gt;\n&lt;?php }\n?&gt;\n\n&lt;\/div&gt;\n&lt;\/div&gt;\n&lt;div class=\"row\"&gt;\n\n&lt;?php if(isset($_SESSION['name'])){?&gt;\n&lt;div class=\"col s12 m6 l4 offset-l3 \" &gt;\n&lt;div class=\"card \"&gt;\n&lt;div class=\"card-image waves-effect waves-block waves-light\"&gt;\n&lt;img class=\"activator\" src=\"&lt;?=$_SESSION['profile_pic']?&gt;\"&gt;\n&lt;\/div&gt;\n&lt;div class=\"card-content\"&gt;\n&lt;span class=\"card-title activator grey-text text-darken-4\"&gt; &lt;i class=\"material-icons\"&gt;&lt;?=$_SESSION['name']?&gt;&lt;\/i&gt;&lt;\/span&gt;\n&lt;\/div&gt;\n&lt;div class=\"card-reveal\"&gt;\n&lt;span class=\"card-title grey-text text-darken-4\"&gt;&lt;?=$_SESSION['name']?&gt;&lt;i class=\"material-icons right\"&gt;close&lt;\/i&gt;&lt;\/span&gt;\n&lt;p&gt;Email Id: &lt;?=$_SESSION['email']?&gt;&lt;\/p&gt;\n&lt;\/div&gt;\n&lt;\/div&gt;\n&lt;\/div&gt;\n&lt;?php }?&gt;\n&lt;\/div&gt;\n&lt;\/div&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Google OAuth API is the simple and powerful way to integrate login system in the website. Google OAuth Login API let you allow the user to sign in the web application using their Google account without registration. The main advantage of Google login is the user can log in to the web application with their [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1000,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[113,122,120],"class_list":["post-1175","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-general","tag-codeigniter","tag-google-login","tag-oauth-api"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Google oauth 2.0 in CodeIgniter - Siri Innovations<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/siriinnovations.com\/blog\/login-with-googleoauth2-0-account-in-codeigniter\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Google oauth 2.0 in CodeIgniter - Siri Innovations\" \/>\n<meta property=\"og:description\" content=\"Google OAuth API is the simple and powerful way to integrate login system in the website. Google OAuth Login API let you allow the user to sign in the web application using their Google account without registration. The main advantage of Google login is the user can log in to the web application with their [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/siriinnovations.com\/blog\/login-with-googleoauth2-0-account-in-codeigniter\/\" \/>\n<meta property=\"og:site_name\" content=\"Siri Innovations\" \/>\n<meta property=\"article:published_time\" content=\"2019-04-10T09:18:35+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-04-12T07:47:19+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/siriinnovations.com\/blog\/wp-content\/uploads\/2014\/07\/10.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"540\" \/>\n\t<meta property=\"og:image:height\" content=\"241\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/siriinnovations.com\/blog\/login-with-googleoauth2-0-account-in-codeigniter\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/siriinnovations.com\/blog\/login-with-googleoauth2-0-account-in-codeigniter\/\"},\"author\":{\"name\":\"admin\",\"@id\":\"https:\/\/siriinnovations.com\/blog\/#\/schema\/person\/d73ce03810dedf6ffc67ed4068c107be\"},\"headline\":\"Google oauth 2.0 in CodeIgniter\",\"datePublished\":\"2019-04-10T09:18:35+00:00\",\"dateModified\":\"2019-04-12T07:47:19+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/siriinnovations.com\/blog\/login-with-googleoauth2-0-account-in-codeigniter\/\"},\"wordCount\":158,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/siriinnovations.com\/blog\/login-with-googleoauth2-0-account-in-codeigniter\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/siriinnovations.com\/blog\/wp-content\/uploads\/2014\/07\/10.jpg\",\"keywords\":[\"codeigniter\",\"Google Login\",\"OAuth API\"],\"articleSection\":[\"General\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/siriinnovations.com\/blog\/login-with-googleoauth2-0-account-in-codeigniter\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/siriinnovations.com\/blog\/login-with-googleoauth2-0-account-in-codeigniter\/\",\"url\":\"https:\/\/siriinnovations.com\/blog\/login-with-googleoauth2-0-account-in-codeigniter\/\",\"name\":\"Google oauth 2.0 in CodeIgniter - Siri Innovations\",\"isPartOf\":{\"@id\":\"https:\/\/siriinnovations.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/siriinnovations.com\/blog\/login-with-googleoauth2-0-account-in-codeigniter\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/siriinnovations.com\/blog\/login-with-googleoauth2-0-account-in-codeigniter\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/siriinnovations.com\/blog\/wp-content\/uploads\/2014\/07\/10.jpg\",\"datePublished\":\"2019-04-10T09:18:35+00:00\",\"dateModified\":\"2019-04-12T07:47:19+00:00\",\"author\":{\"@id\":\"https:\/\/siriinnovations.com\/blog\/#\/schema\/person\/d73ce03810dedf6ffc67ed4068c107be\"},\"breadcrumb\":{\"@id\":\"https:\/\/siriinnovations.com\/blog\/login-with-googleoauth2-0-account-in-codeigniter\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/siriinnovations.com\/blog\/login-with-googleoauth2-0-account-in-codeigniter\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/siriinnovations.com\/blog\/login-with-googleoauth2-0-account-in-codeigniter\/#primaryimage\",\"url\":\"https:\/\/siriinnovations.com\/blog\/wp-content\/uploads\/2014\/07\/10.jpg\",\"contentUrl\":\"https:\/\/siriinnovations.com\/blog\/wp-content\/uploads\/2014\/07\/10.jpg\",\"width\":540,\"height\":241,\"caption\":\"code\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/siriinnovations.com\/blog\/login-with-googleoauth2-0-account-in-codeigniter\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/siriinnovations.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Google oauth 2.0 in CodeIgniter\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/siriinnovations.com\/blog\/#website\",\"url\":\"https:\/\/siriinnovations.com\/blog\/\",\"name\":\"Technical blog from Siri Innovations\",\"description\":\"Innovative like no other\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/siriinnovations.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/siriinnovations.com\/blog\/#\/schema\/person\/d73ce03810dedf6ffc67ed4068c107be\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/siriinnovations.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/0502de904af36be1590ebd238e45c079e504330e5ef528f9a607c84a218015b6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/0502de904af36be1590ebd238e45c079e504330e5ef528f9a607c84a218015b6?s=96&d=mm&r=g\",\"caption\":\"admin\"},\"url\":\"https:\/\/siriinnovations.com\/blog\/author\/admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Google oauth 2.0 in CodeIgniter - Siri Innovations","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/siriinnovations.com\/blog\/login-with-googleoauth2-0-account-in-codeigniter\/","og_locale":"en_US","og_type":"article","og_title":"Google oauth 2.0 in CodeIgniter - Siri Innovations","og_description":"Google OAuth API is the simple and powerful way to integrate login system in the website. Google OAuth Login API let you allow the user to sign in the web application using their Google account without registration. The main advantage of Google login is the user can log in to the web application with their [&hellip;]","og_url":"https:\/\/siriinnovations.com\/blog\/login-with-googleoauth2-0-account-in-codeigniter\/","og_site_name":"Siri Innovations","article_published_time":"2019-04-10T09:18:35+00:00","article_modified_time":"2019-04-12T07:47:19+00:00","og_image":[{"width":540,"height":241,"url":"https:\/\/siriinnovations.com\/blog\/wp-content\/uploads\/2014\/07\/10.jpg","type":"image\/jpeg"}],"author":"admin","twitter_misc":{"Written by":"admin","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/siriinnovations.com\/blog\/login-with-googleoauth2-0-account-in-codeigniter\/#article","isPartOf":{"@id":"https:\/\/siriinnovations.com\/blog\/login-with-googleoauth2-0-account-in-codeigniter\/"},"author":{"name":"admin","@id":"https:\/\/siriinnovations.com\/blog\/#\/schema\/person\/d73ce03810dedf6ffc67ed4068c107be"},"headline":"Google oauth 2.0 in CodeIgniter","datePublished":"2019-04-10T09:18:35+00:00","dateModified":"2019-04-12T07:47:19+00:00","mainEntityOfPage":{"@id":"https:\/\/siriinnovations.com\/blog\/login-with-googleoauth2-0-account-in-codeigniter\/"},"wordCount":158,"commentCount":0,"image":{"@id":"https:\/\/siriinnovations.com\/blog\/login-with-googleoauth2-0-account-in-codeigniter\/#primaryimage"},"thumbnailUrl":"https:\/\/siriinnovations.com\/blog\/wp-content\/uploads\/2014\/07\/10.jpg","keywords":["codeigniter","Google Login","OAuth API"],"articleSection":["General"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/siriinnovations.com\/blog\/login-with-googleoauth2-0-account-in-codeigniter\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/siriinnovations.com\/blog\/login-with-googleoauth2-0-account-in-codeigniter\/","url":"https:\/\/siriinnovations.com\/blog\/login-with-googleoauth2-0-account-in-codeigniter\/","name":"Google oauth 2.0 in CodeIgniter - Siri Innovations","isPartOf":{"@id":"https:\/\/siriinnovations.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/siriinnovations.com\/blog\/login-with-googleoauth2-0-account-in-codeigniter\/#primaryimage"},"image":{"@id":"https:\/\/siriinnovations.com\/blog\/login-with-googleoauth2-0-account-in-codeigniter\/#primaryimage"},"thumbnailUrl":"https:\/\/siriinnovations.com\/blog\/wp-content\/uploads\/2014\/07\/10.jpg","datePublished":"2019-04-10T09:18:35+00:00","dateModified":"2019-04-12T07:47:19+00:00","author":{"@id":"https:\/\/siriinnovations.com\/blog\/#\/schema\/person\/d73ce03810dedf6ffc67ed4068c107be"},"breadcrumb":{"@id":"https:\/\/siriinnovations.com\/blog\/login-with-googleoauth2-0-account-in-codeigniter\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/siriinnovations.com\/blog\/login-with-googleoauth2-0-account-in-codeigniter\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/siriinnovations.com\/blog\/login-with-googleoauth2-0-account-in-codeigniter\/#primaryimage","url":"https:\/\/siriinnovations.com\/blog\/wp-content\/uploads\/2014\/07\/10.jpg","contentUrl":"https:\/\/siriinnovations.com\/blog\/wp-content\/uploads\/2014\/07\/10.jpg","width":540,"height":241,"caption":"code"},{"@type":"BreadcrumbList","@id":"https:\/\/siriinnovations.com\/blog\/login-with-googleoauth2-0-account-in-codeigniter\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/siriinnovations.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Google oauth 2.0 in CodeIgniter"}]},{"@type":"WebSite","@id":"https:\/\/siriinnovations.com\/blog\/#website","url":"https:\/\/siriinnovations.com\/blog\/","name":"Technical blog from Siri Innovations","description":"Innovative like no other","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/siriinnovations.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/siriinnovations.com\/blog\/#\/schema\/person\/d73ce03810dedf6ffc67ed4068c107be","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/siriinnovations.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/0502de904af36be1590ebd238e45c079e504330e5ef528f9a607c84a218015b6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/0502de904af36be1590ebd238e45c079e504330e5ef528f9a607c84a218015b6?s=96&d=mm&r=g","caption":"admin"},"url":"https:\/\/siriinnovations.com\/blog\/author\/admin\/"}]}},"jetpack_featured_media_url":"https:\/\/siriinnovations.com\/blog\/wp-content\/uploads\/2014\/07\/10.jpg","_links":{"self":[{"href":"https:\/\/siriinnovations.com\/blog\/wp-json\/wp\/v2\/posts\/1175","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/siriinnovations.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/siriinnovations.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/siriinnovations.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/siriinnovations.com\/blog\/wp-json\/wp\/v2\/comments?post=1175"}],"version-history":[{"count":26,"href":"https:\/\/siriinnovations.com\/blog\/wp-json\/wp\/v2\/posts\/1175\/revisions"}],"predecessor-version":[{"id":1212,"href":"https:\/\/siriinnovations.com\/blog\/wp-json\/wp\/v2\/posts\/1175\/revisions\/1212"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/siriinnovations.com\/blog\/wp-json\/wp\/v2\/media\/1000"}],"wp:attachment":[{"href":"https:\/\/siriinnovations.com\/blog\/wp-json\/wp\/v2\/media?parent=1175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/siriinnovations.com\/blog\/wp-json\/wp\/v2\/categories?post=1175"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/siriinnovations.com\/blog\/wp-json\/wp\/v2\/tags?post=1175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}