Showing
5 changed files
with
123 additions
and
1 deletions
This diff is collapsed. Click to expand it.
... | @@ -10,6 +10,7 @@ | ... | @@ -10,6 +10,7 @@ |
10 | "debug": "~2.6.9", | 10 | "debug": "~2.6.9", |
11 | "ejs": "^3.1.6", | 11 | "ejs": "^3.1.6", |
12 | "express": "^4.16.4", | 12 | "express": "^4.16.4", |
13 | + "google-auth-library": "^7.0.4", | ||
13 | "http-errors": "~1.6.3", | 14 | "http-errors": "~1.6.3", |
14 | "morgan": "~1.9.1" | 15 | "morgan": "~1.9.1" |
15 | } | 16 | } | ... | ... |
1 | var express = require('express'); | 1 | var express = require('express'); |
2 | var router = express.Router(); | 2 | var router = express.Router(); |
3 | +var {OAuth2Client} = require('google-auth-library'); | ||
4 | +var CLIENT_ID = "94679084723-s5f0686p2porp9mkakrp1p89a48n24nj.apps.googleusercontent.com" | ||
5 | +var client= new OAuth2Client(CLIENT_ID); | ||
3 | 6 | ||
4 | /* GET home page. */ | 7 | /* GET home page. */ |
5 | router.get('/', function(req, res, next) { | 8 | router.get('/', function(req, res, next) { |
6 | - res.render('index', { title: 'Express' }); | 9 | + res.render('index', { d: "94679084723-s5f0686p2porp9mkakrp1p89a48n24nj.apps.googleusercontent.com" }); |
10 | +}); | ||
11 | +router.get('/index', function(req, res, next) { | ||
12 | + res.render('index', { d: "94679084723-s5f0686p2porp9mkakrp1p89a48n24nj.apps.googleusercontent.com" }); | ||
13 | +}); | ||
14 | + | ||
15 | +router.post('/index', (req, res) => { | ||
16 | + let token=req.body.token; | ||
17 | + async function verify() { | ||
18 | + const ticket = await client.verifyIdToken({ | ||
19 | + idToken: token, | ||
20 | + audience: CLIENT_ID, // Specify the CLIENT_ID of the app that accesses the backend | ||
21 | + }); | ||
22 | + } | ||
23 | + verify() | ||
24 | + .then(()=>{ | ||
25 | + res.cookie('session-token', token); | ||
26 | + res.send('success') | ||
27 | + }) | ||
28 | + .catch(console.error); | ||
7 | }); | 29 | }); |
8 | 30 | ||
31 | +router.get('/login', checkAuthenticated, (req,res )=>{ | ||
32 | + let user=req.user; | ||
33 | + res.render('login', {user}) | ||
34 | +}); | ||
9 | module.exports = router; | 35 | module.exports = router; |
36 | + | ||
37 | + | ||
38 | + | ||
39 | +function checkAuthenticated(req, res, next){ | ||
40 | + | ||
41 | + let token = req.cookies['session-token']; | ||
42 | + | ||
43 | + let user = {}; | ||
44 | + async function verify() { | ||
45 | + const ticket = await client.verifyIdToken({ | ||
46 | + idToken: token, | ||
47 | + audience: CLIENT_ID, // Specify the CLIENT_ID of the app that accesses the backend | ||
48 | + }); | ||
49 | + const payload = ticket.getPayload(); | ||
50 | + user.name = payload.name; | ||
51 | + user.email = payload.email; | ||
52 | + user.picture = payload.picture; | ||
53 | + console.log(user.name); | ||
54 | + } | ||
55 | + verify() | ||
56 | + .then(()=>{ | ||
57 | + req.user = user; | ||
58 | + next(); | ||
59 | + }) | ||
60 | + .catch(err=>{ | ||
61 | + res.redirect('/login') | ||
62 | + }) | ||
63 | + | ||
64 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
1 | +<!DOCTYPE html> | ||
2 | +<html> | ||
3 | +<head> | ||
4 | + <meta charset="UTF-8"> | ||
5 | + <script src="https://apis.google.com/js/platform.js" async defer></script> | ||
6 | + <meta name="google-signin-client_id" content=<%=d%>> | ||
7 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
8 | + <title><%= d%></title> | ||
9 | + <link rel='stylesheet' href='/stylesheets/style.css' /> | ||
10 | +</head> | ||
11 | + | ||
12 | +<body> | ||
13 | +<h1>Login</h1> | ||
14 | +<div class="g-signin2" data-onsuccess="onSignIn"></div> | ||
15 | +<a href="#" onclick="signOut();">Sign out</a> | ||
16 | +</body> | ||
17 | + | ||
18 | + | ||
19 | +<script> | ||
20 | + function onSignIn(googleUser) { | ||
21 | + var id_token = googleUser.getAuthResponse().id_token; | ||
22 | + //console.log(id_token); | ||
23 | + var xhr = new XMLHttpRequest(); | ||
24 | + xhr.open('POST', '/index'); | ||
25 | + xhr.setRequestHeader('Content-Type', 'application/json'); | ||
26 | + xhr.onload = function() { | ||
27 | + console.log('Signed in as: ' + xhr.responseText); | ||
28 | + if(xhr.responseText == 'success'){ | ||
29 | + signOut(); | ||
30 | + location.assign('/login') | ||
31 | + } | ||
32 | + }; | ||
33 | + xhr.send(JSON.stringify({token: id_token})); | ||
34 | + } | ||
35 | + | ||
36 | + function signOut() { | ||
37 | + var auth2 = gapi.auth2.getAuthInstance(); | ||
38 | + auth2.signOut().then(function () { | ||
39 | + console.log('User signed out.'); | ||
40 | + }); | ||
41 | + } | ||
42 | +</script> | ||
43 | +</html> | ... | ... |
views/login.ejs
0 → 100644
1 | +<!DOCTYPE html> | ||
2 | +<html lang="en"> | ||
3 | +<head> | ||
4 | + <meta charset="UTF-8"> | ||
5 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
6 | + | ||
7 | + <title>logined</title> | ||
8 | +</head> | ||
9 | +<body> | ||
10 | + | ||
11 | +<a href="/index" onclick="signOut();">Sign Out</a> | ||
12 | +<h1>Hi <%= user.name %></h1> | ||
13 | + | ||
14 | +</body> | ||
15 | +<script> | ||
16 | + function signOut() { | ||
17 | + var auth2 = gapi.auth2.getAuthInstance(); | ||
18 | + auth2.signOut().then(function () { | ||
19 | + console.log('User signed out.'); | ||
20 | + }); | ||
21 | + } | ||
22 | +</script> | ||
23 | +</html> | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or login to post a comment