-
Notifications
You must be signed in to change notification settings - Fork 7
/
access_token.js
41 lines (37 loc) · 1.31 KB
/
access_token.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const express = require('express')
const session = require('express-session')
const gauth = require('../index.js')
const app = express()
const allowedLoginFromDomains = ['reaktor.fi', 'reaktor.com']
// Initialize Google auth middleware. You need your Google app id and secret.
const myGauth = gauth({
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
clientDomain: 'http://localhost:5555',
allowedDomains: allowedLoginFromDomains,
googleAuthorizationParams: {
scope: ['profile', 'email', '<GOOGLE API SCOPE>'],
// To gain refreshable tokens
accessType: 'offline',
// This forces Google to send refresh tokens on each session start
// By default Google sends refresh token only once per authentication to Google
prompt: 'consent',
},
// Refresh access token 10 seconds before the expiration time
refreshBefore: 10
})
// Session must be initialized first
app.use(session({
secret: 'lol',
resave: false,
saveUninitialized: true
}))
// Use your configured gauth middleware
app.use(myGauth)
app.get('/', function(req, res) {
const accessToken = req.session.passport.user.credentials.access_token
// Access Google APIs using the access token
})
app.listen(5555, function() {
console.log('Waiting for Reaktorian Google login at http://localhost:5555')
})