
How To Add Google Social Login Button
Purpose
Hi there! In this post, we’ll show you how easy it is to add a Google login button to your app or website 🙂
Customers have told us that this is the simplest Javascript step-by-step guide, with full working code (< 15 lines), to create a Social Login Button for Google!
Outcome
Fully Functional Code
To demonstrate the simplicity of this solution, let us look at the final code we create. You can try out the code instantly here: https://jsfiddle.net/a7Ljj18e/1/
HTML
<a id="google-button" class="btn btn-block btn-social btn-google">
<i class="fa fa-google"></i> Sign in with Google
</a>
CSS
None
JS
$('#google-button').on('click', function() {
// Initialize with your OAuth.io app public key
OAuth.initialize('TMRAkcJl9nNercaeP5tFhqPMSLA');
// Use popup for OAuth
OAuth.popup('google').then(google => {
console.log(google);
// Retrieves user data from oauth provider
console.log(google.me());
});
})
External Resources
*OAuth.io: https://oauth.io * jQuery: https://code.jquery.com/jquery-3.2.1.min.js * oauth.io JS: https://cdn.rawgit.com/oauth-io/oauth-js/c5af4519/dist/oauth.js * Bootstrap: https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css * Font-Aweseome: https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css * Bootstrap-social: https://cdnjs.cloudflare.com/ajax/libs/bootstrap-social/4.12.0/bootstrap-social.min.css
General Steps
- Create Google app
- Create OAuth.io account
- Link Google app keys to oauth.io account
- Create social login button in HTML/CSS/JS with oauth.io app key
1. Create Google app
Go to https://console.developers.google.com and sign in or create a new account.
Click on ‘Create Project’ on top left.
Name your project and click ‘Create’
Check that you’re on the project you just created and then click on ‘Credentials’
Select OAuth consent screen and enter email address and a product name. Click Save.
Click on the Credentials tab and then click ‘Create credentials’ and select ‘OAuth client ID’ from the dropdown menu.
Select ‘Web application’ and then enter https://oauth.io and https://oauth.io/auth for ‘Authorized JavaScript origins’ and Authorized redirect URLs, respectively. Click Create.
You now have your client ID and client secret which you will add to your OAuth.io dashboard.
2. Create oauth.io Account
Create an account on OAuth.io.
On the main dashboard, click on ‘Integrated APIs’ on the left menu.
On the ‘Integration APIs’ dashboard, click ‘Add APIs’.
Select ‘Google’ as the OAuth provider that you want to add.
3. Link Google app keys to oauth.io account
Set access_type to ‘online’. From your Google app page, copy the Google ‘Client ID’ and ‘Client Secret’ that you noted earlier into ‘client_id’, and ‘client_secret’ fields, respectively, select your desired scope and then click ‘Save’.
Click on ‘Try Auth’ to see if you have configured oauth.io to access your Google app correctly.
4. Create social login button in HTML/CSS/JS with oauth.io app key
Host the code below on your server. If you have no server yet, you can test the code here: https://jsfiddle.net/a7Ljj18e/1/
<html>
<header>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"></script>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://cdn.rawgit.com/oauth-io/oauth-js/c5af4519/dist/oauth.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-social/4.12.0/bootstrap-social.min.css">
</header>
<body>
<a id="google-button" class="btn btn-block btn-social btn-google">
<i class="fa fa-google"></i> Sign in with google
</a>
<script>
$('#google-button').on('click', function() {
// Initialize with your OAuth.io app public key
OAuth.initialize('YOUR_OAUTH_KEY');
// Use popup for OAuth
OAuth.popup('google').then(google => {
console.log(google);
// Retrieves user data from oauth provider
console.log(google.me());
});
})
</script>
</body>
</html>
Should you have any further questions, just ask our team on our live chat.