showing results for - "setup google oauth2 login with express"
Henry
10 Apr 2016
1/// STEP 2. make main.ejs 
2// include get route, get some data callback, get some data 
3const express = require('express');
4const google = require('googleapis').google;
5const jwt = require('jsonwebtoken');
6// Google's OAuth2 client
7const OAuth2 = google.auth.OAuth2;
8// Including our config file
9const CONFIG = require('./config');
10// Creating our express application
11const app = express();
12// Allowing ourselves to use cookies
13const cookieParser = require('cookie-parser');
14app.use(cookieParser());
15// Setting up Views
16app.set('view engine', 'ejs');
17app.set('views', __dirname);
18////////// app.get '/' Route /////////////////////////
19// GET route where we’ll put our link to log in with google.
20app.get('/', function (req, res) {
21  // Create an OAuth2 client object from the credentials in our config file
22  const oauth2Client = new OAuth2(CONFIG.oauth2Credentials.client_id, CONFIG.oauth2Credentials.client_secret, CONFIG.oauth2Credentials.redirect_uris[0]);
23  // Obtain the google login link to which we'll send our users to give us access
24  const loginLink = oauth2Client.generateAuthUrl({
25    access_type: 'offline', // Indicates that we need to be able to access data continously without the user constantly giving us consent
26    scope: CONFIG.oauth2Credentials.scopes // Using the access scopes from our config file
27  });
28  return res.render("index", { loginLink: loginLink });
29});
30/////////////////////////////////////////////////////////////
31//  Redirect user to /get_some_data page,
32app.get('/auth_callback', function (req, res) {
33  // Create an OAuth2 client object from the credentials in our config file
34  const oauth2Client = new OAuth2(CONFIG.oauth2Credentials.client_id, CONFIG.oauth2Credentials.client_secret, CONFIG.oauth2Credentials.redirect_uris[0]);
35
36  if (req.query.error) {
37    // The user did not give us permission.
38    return res.redirect('/');
39  } else {
40    oauth2Client.getToken(req.query.code, function(err, token) {
41      if (err)
42        return res.redirect('/');
43      
44      // Store the credentials given by google into a jsonwebtoken in a cookie called 'jwt'
45      res.cookie('jwt', jwt.sign(token, CONFIG.JWTsecret));
46      return res.redirect('/get_some_data'); // renders index template with login link 
47    });
48  }
49});
50/////////////// get_some_data page ////////////////////////////
51// In aidan's example, 5 user subscribed channels are displayed.
52// Needs to create OAuth2 client then add user’s credentials 
53// to access anything. Then Gets subscriptions, sends to template. 
54///////////////////////////////////////////////////////////////
55app.get('/get_some_data', function (req, res) {
56  if (!req.cookies.jwt) {
57    // We haven't logged in
58    return res.redirect('/');
59  }
60  // Create an OAuth2 client object from the credentials in our config file
61  const oauth2Client = new OAuth2(CONFIG.oauth2Credentials.client_id, CONFIG.oauth2Credentials.client_secret, CONFIG.oauth2Credentials.redirect_uris[0]);
62  // Add this specific user's credentials to our OAuth2 client
63  oauth2Client.credentials = jwt.verify(req.cookies.jwt, CONFIG.JWTsecret);
64  // Get the youtube service
65  const service = google.youtube('v3');
66  // Get five of the user's subscriptions (the channels they're subscribed to)
67  service.subscriptions.list({
68    auth: oauth2Client,
69    mine: true,
70    part: 'snippet,contentDetails',
71    maxResults: 5
72  }).then(response => {
73    // Render the data view, passing the subscriptions to it
74    return res.render('data', { subscriptions: response.data.items });
75  });
76});
77// Listen on the port defined in the config file
78app.listen(CONFIG.port, function () {
79  console.log(`Listening on port ${CONFIG.port}`);
80});
Cristina
09 Feb 2017
1// STEP 4. ///////////////////////////////////////////////
2// Lastly create the data.ejs template in order 
3// to display the data.
4//////////////////////////////////////////////////////////
5
6
7<!DOCTYPE html>
8<html lang="en">
9<head>
10  <meta charset="UTF-8">
11  <meta name="viewport" content="width=device-width, initial-scale=1.0">
12  <meta http-equiv="X-UA-Compatible" content="ie=edge">
13  <title>Express Google OAuth2 Tutorial by Aidan Lovelace</title>
14</head>
15<body>
16  <ul>
17    <% subscriptions.forEach(function (subscription) { %>
18      <li><%= subscription.snippet.title %></li>
19    <% }) %>
20  </ul>
21</body>
22</html>
23
Miguel
15 Jul 2018
1//////// STEP 3. //////////////////////////////////////
2////// Create base html(ish) file named index.ejs /////
3// with a login link to the page we passed to the file.
4/////////////// index.ejs /////////////////////////////
5
6<!DOCTYPE html>
7<html lang="en">
8<head>
9  <meta charset="UTF-8">
10  <meta name="viewport" content="width=device-width, initial-scale=1.0">
11  <meta http-equiv="X-UA-Compatible" content="ie=edge">
12  <title>Express Google OAuth2 Tutorial by Aidan Lovelace</title>
13</head>
14<body>
15  <a href="<%= loginLink %>">Login</a>
16</body>
17</html>
18