Skip to content

Commit

Permalink
Google Sign-In
Browse files Browse the repository at this point in the history
  • Loading branch information
rjvelazco committed Nov 24, 2020
1 parent 7c48ad2 commit 0723819
Show file tree
Hide file tree
Showing 6 changed files with 381 additions and 1 deletion.
226 changes: 226 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 @@ -13,6 +13,7 @@
"bcrypt": "^5.0.0",
"body-parser": "^1.19.0",
"express": "^4.17.1",
"google-auth-library": "^6.1.3",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.10.14",
"mongoose-unique-validator": "^2.0.3",
Expand Down
45 changes: 45 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<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="826396277298-ejlh1a03k5ddda8davgdcnf8g8ngb6l9.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.

var id_token = googleUser.getAuthResponse().id_token;
console.log(id_token);
var 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>
8 changes: 7 additions & 1 deletion server/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,10 @@ 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 || '826396277298-ejlh1a03k5ddda8davgdcnf8g8ngb6l9.apps.googleusercontent.com'
Loading

0 comments on commit 0723819

Please sign in to comment.