├── .gitignore ├── .DS_Store ├── views ├── index.ejs ├── profile.ejs └── login.ejs ├── package.json └── server.js /.gitignore: -------------------------------------------------------------------------------- 1 | .env 2 | node_modules/ 3 | .DS_Store 4 | -------------------------------------------------------------------------------- /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/conorbailey90/Google-Auth/HEAD/.DS_Store -------------------------------------------------------------------------------- /views/index.ejs: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Document 7 | 8 | 9 | 10 |

Welcome

11 | 12 | Sign In 13 | 14 | 15 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "google_auth", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "dev": "nodemon server.js" 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "ISC", 12 | "dependencies": { 13 | "cookie-parser": "^1.4.5", 14 | "dotenv": "^8.2.0", 15 | "ejs": "^3.1.5", 16 | "express": "^4.17.1", 17 | "google-auth-library": "^6.1.3", 18 | "jsonwebtoken": "^8.5.1" 19 | }, 20 | "devDependencies": { 21 | "nodemon": "^2.0.6" 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /views/profile.ejs: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Document 8 | 9 | 10 | 11 | Sign Out 12 |

Hi <%= user.name %>

13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /views/login.ejs: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | Login 9 | 10 | 11 | 12 |
13 | 14 | 15 | 23 | 24 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /server.js: -------------------------------------------------------------------------------- 1 | require('dotenv').config() 2 | 3 | const express = require('express'); 4 | const app = express(); 5 | const cookieParser = require('cookie-parser') 6 | 7 | // Google Auth 8 | const {OAuth2Client} = require('google-auth-library'); 9 | const CLIENT_ID = '333642631602-h2l1m29lfb5c1d0dta76nvv4so4bjeo4.apps.googleusercontent.com' 10 | const client = new OAuth2Client(CLIENT_ID); 11 | 12 | 13 | const PORT = 7000; 14 | 15 | // Middleware 16 | 17 | app.set('view engine', 'ejs'); 18 | app.use(express.json()); 19 | app.use(cookieParser()); 20 | app.use(express.static('public')); 21 | 22 | app.get('/', (req, res)=>{ 23 | res.render('index') 24 | }) 25 | 26 | app.get('/login', (req,res)=>{ 27 | res.render('login'); 28 | }) 29 | 30 | app.post('/login', (req,res)=>{ 31 | let token = req.body.token; 32 | 33 | async function verify() { 34 | const ticket = await client.verifyIdToken({ 35 | idToken: token, 36 | audience: CLIENT_ID, // Specify the CLIENT_ID of the app that accesses the backend 37 | }); 38 | const payload = ticket.getPayload(); 39 | const userid = payload['sub']; 40 | } 41 | verify() 42 | .then(()=>{ 43 | res.cookie('session-token', token); 44 | res.send('success') 45 | }) 46 | .catch(console.error); 47 | 48 | }) 49 | 50 | app.get('/profile', checkAuthenticated, (req, res)=>{ 51 | let user = req.user; 52 | res.render('profile', {user}); 53 | }) 54 | 55 | app.get('/protectedRoute', checkAuthenticated, (req,res)=>{ 56 | res.send('This route is protected') 57 | }) 58 | 59 | app.get('/logout', (req, res)=>{ 60 | res.clearCookie('session-token'); 61 | res.redirect('/login') 62 | 63 | }) 64 | 65 | 66 | function checkAuthenticated(req, res, next){ 67 | 68 | let token = req.cookies['session-token']; 69 | 70 | let user = {}; 71 | async function verify() { 72 | const ticket = await client.verifyIdToken({ 73 | idToken: token, 74 | audience: CLIENT_ID, // Specify the CLIENT_ID of the app that accesses the backend 75 | }); 76 | const payload = ticket.getPayload(); 77 | user.name = payload.name; 78 | user.email = payload.email; 79 | user.picture = payload.picture; 80 | } 81 | verify() 82 | .then(()=>{ 83 | req.user = user; 84 | next(); 85 | }) 86 | .catch(err=>{ 87 | res.redirect('/login') 88 | }) 89 | 90 | } 91 | 92 | 93 | app.listen(PORT, ()=>{ 94 | console.log(`Server running on port ${PORT}`); 95 | }) --------------------------------------------------------------------------------