Skip to content

Commit

Permalink
Google Sign-In
Browse files Browse the repository at this point in the history
  • Loading branch information
francislagares committed Oct 8, 2019
1 parent 01be857 commit 2f6edcf
Show file tree
Hide file tree
Showing 6 changed files with 337 additions and 14 deletions.
175 changes: 175 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"bcryptjs": "^2.4.3",
"body-parser": "^1.19.0",
"express": "^4.17.1",
"google-auth-library": "^5.3.0",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.7.3",
"mongoose-unique-validator": "^2.0.3",
Expand Down
46 changes: 46 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=0.8, maximum-scale=1" />
<title>Google Sign-in Demo</title>

<script src="https://apis.google.com/js/platform.js" async defer></script>
<meta name="google-signin-client_id" content="272700700372-1q41l582v477dg2hdj28h6v2k5853nkg.apps.googleusercontent.com">
</head>

<body>
<div class="g-signin2" data-onsuccess="onSignIn"></div>
<a href="#" onclick="signOut();">Sign out</a>
<script>
function signOut() {
var auth2 = gapi.auth2.getAuthInstance();
auth2.signOut().then(function() {
console.log('User signed out.');
});
}
</script>
<script>
function onSignIn(googleUser) {
var profile = googleUser.getBasicProfile();
console.log('ID: ' + profile.getId()); // Do not send to your backend! Use an ID token instead.
console.log('Name: ' + profile.getName());
console.log('Image URL: ' + profile.getImageUrl());
console.log('Email: ' + profile.getEmail()); // This is null if the 'email' scope is not present.

let id_token = googleUser.getAuthResponse().id_token;
console.log(id_token);

let xhr = new XMLHttpRequest();
xhr.open('POST', '/google');
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onload = function() {
console.log('Signed in as: ' + xhr.responseText);
};
xhr.send('idtoken=' + id_token);
}
</script>
</body>

</html>
5 changes: 4 additions & 1 deletion server/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ if (process.env.NODE_ENV === 'dev') {
urlDB = process.env.MONGO_URI;
}

process.env.URLDB = urlDB;
process.env.URLDB = urlDB;

// Google Client ID
process.env.CLIENT_ID = process.env.CLIENT_ID || '272700700372-1q41l582v477dg2hdj28h6v2k5853nkg.apps.googleusercontent.com';
Loading

0 comments on commit 2f6edcf

Please sign in to comment.