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,7 +110,7 @@ function events(req, res) { | ||
| 110 | } else { | 110 | } else { |
| 111 | // console.info(response) | 111 | // console.info(response) |
| 112 | res.jsonp(ggToKendo(response)) | 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,7 +166,7 @@ function oauth2callback(req, res) { | ||
| 166 | res.send(err) | 166 | res.send(err) |
| 167 | res.end() | 167 | res.end() |
| 168 | } else { | 168 | } else { |
| 169 | - res.redirect('/home') | 169 | + res.redirect('http://localhost:8000/calendar') |
| 170 | } | 170 | } |
| 171 | }) | 171 | }) |
| 172 | } | 172 | } |
app/lib/index.js
| 1 | 'use strict'; | 1 | 'use strict'; |
| 2 | 2 | ||
| 3 | const google = require('googleapis'); | 3 | const google = require('googleapis'); |
| 4 | +const plus = google.plus('v1'); | ||
| 4 | const googleAuth = require('google-auth-library'); | 5 | const googleAuth = require('google-auth-library'); |
| 5 | const calendar = google.calendar('v3'); | 6 | const calendar = google.calendar('v3'); |
| 6 | const fs = require('fs'); | 7 | const fs = require('fs'); |
| @@ -8,11 +9,15 @@ const path = require('path'); | @@ -8,11 +9,15 @@ const path = require('path'); | ||
| 8 | const yamlConfig = require('node-yaml-config'); | 9 | const yamlConfig = require('node-yaml-config'); |
| 9 | const config = yamlConfig.load(path.join(__dirname, '/../../config/config.yml')); | 10 | const config = yamlConfig.load(path.join(__dirname, '/../../config/config.yml')); |
| 10 | const moment = require('moment') | 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 | const CALENDAR_ID = config.ggapi.calendarID | 16 | const CALENDAR_ID = config.ggapi.calendarID |
| 12 | const REDIRECTURL = config.ggapi.redirectUrl | 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 | const TOKEN_DIR = (process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE) + '/.credentials/'; | 21 | const TOKEN_DIR = (process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE) + '/.credentials/'; |
| 17 | const TOKEN_PATH = TOKEN_DIR + 'calendar-nodejs-quickstart.json'; | 22 | const TOKEN_PATH = TOKEN_DIR + 'calendar-nodejs-quickstart.json'; |
| 18 | 23 | ||
| @@ -24,8 +29,6 @@ function hasTimezone(timezone) { | @@ -24,8 +29,6 @@ function hasTimezone(timezone) { | ||
| 24 | } | 29 | } |
| 25 | } | 30 | } |
| 26 | 31 | ||
| 27 | - | ||
| 28 | - | ||
| 29 | function getNewToken(oauth2Client, callback) { | 32 | function getNewToken(oauth2Client, callback) { |
| 30 | var authUrl = oauth2Client.generateAuthUrl({ | 33 | var authUrl = oauth2Client.generateAuthUrl({ |
| 31 | access_type: 'offline', | 34 | access_type: 'offline', |
| @@ -47,16 +50,32 @@ function setNewToken(code) { | @@ -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 | try { | 56 | try { |
| 52 | - fs.mkdirSync(TOKEN_DIR); | 57 | + // ** new |
| 58 | + tokendb = new Token(token); | ||
| 59 | + // -- old | ||
| 60 | + // fs.mkdirSync(TOKEN_DIR); | ||
| 53 | } catch (err) { | 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 | module.exports = { | 81 | module.exports = { |
| @@ -71,15 +90,28 @@ module.exports = { | @@ -71,15 +90,28 @@ module.exports = { | ||
| 71 | var redirectUrl = credentials.installed.redirect_uris[1]; | 90 | var redirectUrl = credentials.installed.redirect_uris[1]; |
| 72 | var auth = new googleAuth(); | 91 | var auth = new googleAuth(); |
| 73 | var oauth2Client = new auth.OAuth2(clientId, clientSecret, REDIRECTURL); | 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 | if (err) { | 96 | if (err) { |
| 77 | return callback(null, null, getNewToken(oauth2Client, callback)); | 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 | return callback(null, oauth2Client); | 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,7 +135,16 @@ module.exports = { | ||
| 103 | return; | 135 | return; |
| 104 | } else { | 136 | } else { |
| 105 | oauth2Client.credentials = token; | 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 | return callback(null, oauth2Client); | 148 | return callback(null, oauth2Client); |
| 108 | } | 149 | } |
| 109 | 150 |
| @@ -0,0 +1,12 @@ | @@ -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 | \ No newline at end of file | 13 | \ No newline at end of file |
config/config.yml
| @@ -4,12 +4,12 @@ localhost: | @@ -4,12 +4,12 @@ localhost: | ||
| 4 | server: | 4 | server: |
| 5 | url: 'localhost' | 5 | url: 'localhost' |
| 6 | urlto: 'localhost' | 6 | urlto: 'localhost' |
| 7 | - port: 3001 | 7 | + port: 3030 |
| 8 | portto: 4001 | 8 | portto: 4001 |
| 9 | database: | 9 | database: |
| 10 | host: 'localhost' | 10 | host: 'localhost' |
| 11 | port: 27017 | 11 | port: 27017 |
| 12 | - name: 'ss7' | 12 | + name: 'calendar' |
| 13 | options: | 13 | options: |
| 14 | user: | 14 | user: |
| 15 | pass: | 15 | pass: |
| @@ -17,4 +17,4 @@ localhost: | @@ -17,4 +17,4 @@ localhost: | ||
| 17 | apitimeout: 3000 #millisecond | 17 | apitimeout: 3000 #millisecond |
| 18 | ggapi: | 18 | ggapi: |
| 19 | calendarID: 'rvmbg3kg7uqninf7n3au1ku4mc@group.calendar.google.com' | 19 | calendarID: 'rvmbg3kg7uqninf7n3au1ku4mc@group.calendar.google.com' |
| 20 | - redirectUrl: 'http://localhost:3001/oauth2callback' | ||
| 21 | \ No newline at end of file | 20 | \ No newline at end of file |
| 21 | + redirectUrl: 'http://localhost:3030/oauth2callback' | ||
| 22 | \ No newline at end of file | 22 | \ No newline at end of file |
| @@ -0,0 +1,23 @@ | @@ -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 | \ No newline at end of file | 24 | \ No newline at end of file |
package.json
| @@ -16,7 +16,8 @@ | @@ -16,7 +16,8 @@ | ||
| 16 | "moment-timezone": "^0.5.5", | 16 | "moment-timezone": "^0.5.5", |
| 17 | "node-yaml-config": "0.0.4", | 17 | "node-yaml-config": "0.0.4", |
| 18 | "scribe-js": "^2.0.4", | 18 | "scribe-js": "^2.0.4", |
| 19 | - "unirest": "^0.5.0" | 19 | + "unirest": "^0.5.0", |
| 20 | + "mongoose": "^4.6.6" | ||
| 20 | }, | 21 | }, |
| 21 | "devDependencies": { | 22 | "devDependencies": { |
| 22 | "bower": "^1.7.7", | 23 | "bower": "^1.7.7", |
server.js
| @@ -4,7 +4,8 @@ var yaml_config = require('node-yaml-config') | @@ -4,7 +4,8 @@ var yaml_config = require('node-yaml-config') | ||
| 4 | var busboyBodyParser = require('busboy-body-parser') | 4 | var busboyBodyParser = require('busboy-body-parser') |
| 5 | var scribe = require('scribe-js')() | 5 | var scribe = require('scribe-js')() |
| 6 | var Async = require('async') | 6 | var Async = require('async') |
| 7 | - | 7 | +var mongoose = require('./config/mongoose') |
| 8 | +var db = mongoose() | ||
| 8 | var console = process.console | 9 | var console = process.console |
| 9 | var app = express() | 10 | var app = express() |
| 10 | var router = express.Router() | 11 | var router = express.Router() |
| @@ -41,6 +42,6 @@ app.use('/logs', scribe.webPanel()) | @@ -41,6 +42,6 @@ app.use('/logs', scribe.webPanel()) | ||
| 41 | app.use(busboyBodyParser()) | 42 | app.use(busboyBodyParser()) |
| 42 | 43 | ||
| 43 | app.listen(port) | 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 | console.tag('START').time().file().log('Server is running at http://' + url + ':' + port) | 47 | console.tag('START').time().file().log('Server is running at http://' + url + ':' + port) |
| 47 | \ No newline at end of file | 48 | \ No newline at end of file |