set up google oauth2 login with express

Solutions on MaxInterview for set up google oauth2 login with express by the best coders in the world

showing results for - "set up google oauth2 login with express"
Badis
20 Jan 2017
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});
Tim
14 Apr 2020
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
Valery
31 Nov 2017
1// STEP 1. Install dependencies and make config.js
2// project for understanding google authorization with express
3//set up your Node.JS project, and install the following dependencies:
4// cookie-parser
5// ejs
6// express
7// google-auth-library
8// googleapis
9// jsonwebtoken
10// In the Credentials section of the Google Developer Console, 
11// create an OAuth Client ID credential of type Web Application.
12// Create a file named config.js with the following contents,
13// Fill in the client_id, project_id, and client_secret properties 
14// with the information for your project.
15////////////////////////// config.js 
16
17const port = 3002;
18const baseURL = `http://localhost:${port}`;
19module.exports = {
20  // The secret for the encryption of the jsonwebtoken
21  JWTsecret: 'mysecret',
22  baseURL: baseURL,
23  port: port,
24  // The credentials and information for OAuth2
25  oauth2Credentials: {
26    client_id: "",
27    project_id: "", // The name of your project
28    auth_uri: "https://accounts.google.com/o/oauth2/auth",
29    token_uri: "https://oauth2.googleapis.com/token",
30    auth_provider_x509_cert_url: "https://www.googleapis.com/oauth2/v1/certs",
31    client_secret: "",
32    redirect_uris: [
33      `${baseURL}/auth_callback`
34    ],
35    scopes: [
36      'https://www.googleapis.com/auth/youtube.readonly'
37    ]
38  }
39};
40
María
23 Jun 2018
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