Commit d360cefc0b6a3e9f0dafd540fc86345560adb9b6
1 parent
21a1ff19
Exists in
master
move save google token file to db
Showing
7 changed files
with
100 additions
and
22 deletions
Show diff stats
app/controllers/home.controller.js
... | ... | @@ -110,7 +110,7 @@ function events(req, res) { |
110 | 110 | } else { |
111 | 111 | // console.info(response) |
112 | 112 | res.jsonp(ggToKendo(response)) |
113 | - res.end | |
113 | + res.end() | |
114 | 114 | } |
115 | 115 | }) |
116 | 116 | } |
... | ... | @@ -166,7 +166,7 @@ function oauth2callback(req, res) { |
166 | 166 | res.send(err) |
167 | 167 | res.end() |
168 | 168 | } else { |
169 | - res.redirect('/home') | |
169 | + res.redirect('http://localhost:8000/calendar') | |
170 | 170 | } |
171 | 171 | }) |
172 | 172 | } | ... | ... |
app/lib/index.js
1 | 1 | 'use strict'; |
2 | 2 | |
3 | 3 | const google = require('googleapis'); |
4 | +const plus = google.plus('v1'); | |
4 | 5 | const googleAuth = require('google-auth-library'); |
5 | 6 | const calendar = google.calendar('v3'); |
6 | 7 | const fs = require('fs'); |
... | ... | @@ -8,11 +9,15 @@ const path = require('path'); |
8 | 9 | const yamlConfig = require('node-yaml-config'); |
9 | 10 | const config = yamlConfig.load(path.join(__dirname, '/../../config/config.yml')); |
10 | 11 | const moment = require('moment') |
12 | +const Promise = require('bluebird') | |
13 | +const Mongoose = Promise.promisifyAll(require('mongoose')); | |
14 | +const Token = Mongoose.model('Token'); | |
15 | + | |
11 | 16 | const CALENDAR_ID = config.ggapi.calendarID |
12 | 17 | const REDIRECTURL = config.ggapi.redirectUrl |
13 | 18 | |
14 | 19 | |
15 | -const SCOPES = ['https://www.googleapis.com/auth/calendar']; | |
20 | +const SCOPES = ['https://www.googleapis.com/auth/calendar', 'https://www.googleapis.com/auth/userinfo.email']; | |
16 | 21 | const TOKEN_DIR = (process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE) + '/.credentials/'; |
17 | 22 | const TOKEN_PATH = TOKEN_DIR + 'calendar-nodejs-quickstart.json'; |
18 | 23 | |
... | ... | @@ -24,8 +29,6 @@ function hasTimezone(timezone) { |
24 | 29 | } |
25 | 30 | } |
26 | 31 | |
27 | - | |
28 | - | |
29 | 32 | function getNewToken(oauth2Client, callback) { |
30 | 33 | var authUrl = oauth2Client.generateAuthUrl({ |
31 | 34 | access_type: 'offline', |
... | ... | @@ -47,16 +50,32 @@ function setNewToken(code) { |
47 | 50 | }) |
48 | 51 | } |
49 | 52 | |
50 | -function storeToken(token) { | |
53 | +function storeToken(token, emails) { | |
54 | + var tokendb; | |
55 | + token.email = emails[0].value | |
51 | 56 | try { |
52 | - fs.mkdirSync(TOKEN_DIR); | |
57 | + // ** new | |
58 | + tokendb = new Token(token); | |
59 | + // -- old | |
60 | + // fs.mkdirSync(TOKEN_DIR); | |
53 | 61 | } catch (err) { |
54 | - if (err.code != 'EEXIST') { | |
55 | - throw err; | |
56 | - } | |
62 | + throw err; | |
57 | 63 | } |
58 | - fs.writeFile(TOKEN_PATH, JSON.stringify(token)); | |
59 | - console.log('Token stored to ' + TOKEN_PATH); | |
64 | + // ** new | |
65 | + Promise.try(function () { }) | |
66 | + .then(function () { | |
67 | + tokendb.save(function (err, result) { | |
68 | + if (err) { | |
69 | + console.error(err) | |
70 | + } else { | |
71 | + console.log(result); | |
72 | + } | |
73 | + }); | |
74 | + }); | |
75 | + | |
76 | + // -- old | |
77 | + // fs.writeFile(TOKEN_PATH, JSON.stringify(token)); | |
78 | + // console.log('Token stored to ' + TOKEN_PATH); | |
60 | 79 | } |
61 | 80 | |
62 | 81 | module.exports = { |
... | ... | @@ -71,15 +90,28 @@ module.exports = { |
71 | 90 | var redirectUrl = credentials.installed.redirect_uris[1]; |
72 | 91 | var auth = new googleAuth(); |
73 | 92 | var oauth2Client = new auth.OAuth2(clientId, clientSecret, REDIRECTURL); |
93 | + var setToken = {} // set token from callback | |
94 | + Token.findOne({ email: 'tzbattleboy@gmail.com' }, function (err, token) { | |
74 | 95 | |
75 | - fs.readFile(TOKEN_PATH, (err, token) => { | |
76 | 96 | if (err) { |
77 | 97 | return callback(null, null, getNewToken(oauth2Client, callback)); |
78 | - } else { | |
79 | - oauth2Client.credentials = JSON.parse(token); | |
98 | + } else if (token) { | |
99 | + setToken = { | |
100 | + access_token: token.access_token, | |
101 | + refresh_token: token.refresh_token, | |
102 | + token_type: token.token_type, | |
103 | + expiry_date: token.expiry_date | |
104 | + } | |
105 | + // console.log(setToken) | |
106 | + oauth2Client.credentials = setToken; | |
80 | 107 | return callback(null, oauth2Client); |
108 | + } else { | |
109 | + return callback(null, null, getNewToken(oauth2Client, callback)); | |
81 | 110 | } |
82 | 111 | }); |
112 | + // fs.readFile(TOKEN_PATH, (err, token) => { | |
113 | + | |
114 | + // }); | |
83 | 115 | }); |
84 | 116 | |
85 | 117 | }, |
... | ... | @@ -103,7 +135,16 @@ module.exports = { |
103 | 135 | return; |
104 | 136 | } else { |
105 | 137 | oauth2Client.credentials = token; |
106 | - storeToken(token); | |
138 | + var params = { userId: 'me', fields: 'emails', auth: oauth2Client }; | |
139 | + | |
140 | + plus.people.get(params, function (err, response) { | |
141 | + if (err) { | |
142 | + consol.error(err) | |
143 | + } else { | |
144 | + storeToken(token, response.emails); | |
145 | + } | |
146 | + }); | |
147 | + | |
107 | 148 | return callback(null, oauth2Client); |
108 | 149 | } |
109 | 150 | ... | ... |
... | ... | @@ -0,0 +1,12 @@ |
1 | +var mongoose = require('mongoose') | |
2 | +var Schema = mongoose.Schema | |
3 | + | |
4 | +var TokenSchema = new Schema({ | |
5 | + email: String, | |
6 | + access_token: String, | |
7 | + expiry_date: Number, | |
8 | + refresh_token: String, | |
9 | + token_type: String | |
10 | +}) | |
11 | + | |
12 | +mongoose.model('Token', TokenSchema) | |
0 | 13 | \ No newline at end of file | ... | ... |
config/config.yml
... | ... | @@ -4,12 +4,12 @@ localhost: |
4 | 4 | server: |
5 | 5 | url: 'localhost' |
6 | 6 | urlto: 'localhost' |
7 | - port: 3001 | |
7 | + port: 3030 | |
8 | 8 | portto: 4001 |
9 | 9 | database: |
10 | 10 | host: 'localhost' |
11 | 11 | port: 27017 |
12 | - name: 'ss7' | |
12 | + name: 'calendar' | |
13 | 13 | options: |
14 | 14 | user: |
15 | 15 | pass: |
... | ... | @@ -17,4 +17,4 @@ localhost: |
17 | 17 | apitimeout: 3000 #millisecond |
18 | 18 | ggapi: |
19 | 19 | calendarID: 'rvmbg3kg7uqninf7n3au1ku4mc@group.calendar.google.com' |
20 | - redirectUrl: 'http://localhost:3001/oauth2callback' | |
21 | 20 | \ No newline at end of file |
21 | + redirectUrl: 'http://localhost:3030/oauth2callback' | |
22 | 22 | \ No newline at end of file | ... | ... |
... | ... | @@ -0,0 +1,23 @@ |
1 | +var mongoose = require('mongoose') | |
2 | +var yaml_config = require('node-yaml-config') | |
3 | +var config = yaml_config.load(__dirname + '/config.yml') | |
4 | +var url = config.server.url | |
5 | +var port = config.server.port | |
6 | +var dbhost = config.database.host | |
7 | +var dbport = config.database.port | |
8 | +var dbuser = config.database.options.user | |
9 | +var dbpass = config.database.options.pass | |
10 | +var dbname = config.database.name | |
11 | + | |
12 | +module.exports = function () { | |
13 | + // mongoose.connect('mongodb://username:password@host:port/database?options...') | |
14 | + var db | |
15 | + if (dbuser && dbpass) { | |
16 | + db = mongoose.connect('mongodb://' + dbuser + ':' + dbpass + '@' + dbhost + ':' + dbport + '/' + dbname) | |
17 | + } else { | |
18 | + db = mongoose.connect('mongodb://' + dbhost + ':' + dbport + '/' + dbname) | |
19 | + } | |
20 | + | |
21 | + require('../app/models/tokens.model') | |
22 | + return db | |
23 | +} | |
0 | 24 | \ No newline at end of file | ... | ... |
package.json
server.js
... | ... | @@ -4,7 +4,8 @@ var yaml_config = require('node-yaml-config') |
4 | 4 | var busboyBodyParser = require('busboy-body-parser') |
5 | 5 | var scribe = require('scribe-js')() |
6 | 6 | var Async = require('async') |
7 | - | |
7 | +var mongoose = require('./config/mongoose') | |
8 | +var db = mongoose() | |
8 | 9 | var console = process.console |
9 | 10 | var app = express() |
10 | 11 | var router = express.Router() |
... | ... | @@ -41,6 +42,6 @@ app.use('/logs', scribe.webPanel()) |
41 | 42 | app.use(busboyBodyParser()) |
42 | 43 | |
43 | 44 | app.listen(port) |
44 | - // console.log('Server is running at http://'+url+':'+port+''); | |
45 | +// console.log('Server is running at http://'+url+':'+port+''); | |
45 | 46 | |
46 | 47 | console.tag('START').time().file().log('Server is running at http://' + url + ':' + port) |
47 | 48 | \ No newline at end of file | ... | ... |