Skip to content

Commit 07b63ed

Browse files
committed
Google Sing-In
1 parent 66ab113 commit 07b63ed

6 files changed

Lines changed: 299 additions & 3 deletions

File tree

package-lock.json

Lines changed: 118 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"bcrypt": "^1.0.3",
1414
"body-parser": "^1.18.2",
1515
"express": "^4.16.2",
16+
"google-auth-library": "^1.3.2",
1617
"jsonwebtoken": "^8.2.0",
1718
"mongoose": "^5.0.9",
1819
"mongoose-unique-validator": "^2.0.0",

public/index.html

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
8+
<title>Google Sign-In Demo</title>
9+
10+
<script src="https://apis.google.com/js/platform.js" async defer></script>
11+
<meta name="google-signin-client_id" content="219758474264-vh1bibcphgvbc32km508lubtqkanikf1.apps.googleusercontent.com">
12+
13+
</head>
14+
15+
<body>
16+
17+
18+
<div class="g-signin2" data-onsuccess="onSignIn"></div>
19+
20+
21+
<a href="#" onclick="signOut();">Sign out</a>
22+
<script>
23+
function signOut() {
24+
var auth2 = gapi.auth2.getAuthInstance();
25+
auth2.signOut().then(function() {
26+
console.log('User signed out.');
27+
});
28+
}
29+
</script>
30+
31+
32+
<script>
33+
function onSignIn(googleUser) {
34+
var profile = googleUser.getBasicProfile();
35+
console.log('ID: ' + profile.getId()); // Do not send to your backend! Use an ID token instead.
36+
console.log('Name: ' + profile.getName());
37+
console.log('Image URL: ' + profile.getImageUrl());
38+
console.log('Email: ' + profile.getEmail()); // This is null if the 'email' scope is not present.
39+
40+
41+
var id_token = googleUser.getAuthResponse().id_token;
42+
43+
console.log(id_token);
44+
45+
46+
var xhr = new XMLHttpRequest();
47+
xhr.open('POST', '/google');
48+
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
49+
xhr.onload = function() {
50+
console.log('Signed in as: ' + xhr.responseText);
51+
};
52+
xhr.send('idtoken=' + id_token);
53+
54+
}
55+
</script>
56+
57+
</body>
58+
59+
</html>

server/config/config.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,9 @@ if (process.env.NODE_ENV === 'dev') {
3535
} else {
3636
urlDB = process.env.MONGO_URI;
3737
}
38-
process.env.URLDB = urlDB;
38+
process.env.URLDB = urlDB;
39+
40+
// ============================
41+
// Google Client ID
42+
// ============================
43+
process.env.CLIENT_ID = process.env.CLIENT_ID || '219758474264-vh1bibcphgvbc32km508lubtqkanikf1.apps.googleusercontent.com';

0 commit comments

Comments
 (0)