diff --git a/app/controllers/home.controller.js b/app/controllers/home.controller.js index 7e063c9..3f95fe4 100644 --- a/app/controllers/home.controller.js +++ b/app/controllers/home.controller.js @@ -110,7 +110,7 @@ function events(req, res) { } else { // console.info(response) res.jsonp(ggToKendo(response)) - res.end + res.end() } }) } @@ -166,7 +166,7 @@ function oauth2callback(req, res) { res.send(err) res.end() } else { - res.redirect('/home') + res.redirect('http://localhost:8000/calendar') } }) } diff --git a/app/lib/index.js b/app/lib/index.js index 3f2e2f8..3b026b1 100644 --- a/app/lib/index.js +++ b/app/lib/index.js @@ -1,6 +1,7 @@ 'use strict'; const google = require('googleapis'); +const plus = google.plus('v1'); const googleAuth = require('google-auth-library'); const calendar = google.calendar('v3'); const fs = require('fs'); @@ -8,11 +9,15 @@ const path = require('path'); const yamlConfig = require('node-yaml-config'); const config = yamlConfig.load(path.join(__dirname, '/../../config/config.yml')); const moment = require('moment') +const Promise = require('bluebird') +const Mongoose = Promise.promisifyAll(require('mongoose')); +const Token = Mongoose.model('Token'); + const CALENDAR_ID = config.ggapi.calendarID const REDIRECTURL = config.ggapi.redirectUrl -const SCOPES = ['https://www.googleapis.com/auth/calendar']; +const SCOPES = ['https://www.googleapis.com/auth/calendar', 'https://www.googleapis.com/auth/userinfo.email']; const TOKEN_DIR = (process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE) + '/.credentials/'; const TOKEN_PATH = TOKEN_DIR + 'calendar-nodejs-quickstart.json'; @@ -24,8 +29,6 @@ function hasTimezone(timezone) { } } - - function getNewToken(oauth2Client, callback) { var authUrl = oauth2Client.generateAuthUrl({ access_type: 'offline', @@ -47,16 +50,32 @@ function setNewToken(code) { }) } -function storeToken(token) { +function storeToken(token, emails) { + var tokendb; + token.email = emails[0].value try { - fs.mkdirSync(TOKEN_DIR); + // ** new + tokendb = new Token(token); + // -- old + // fs.mkdirSync(TOKEN_DIR); } catch (err) { - if (err.code != 'EEXIST') { - throw err; - } + throw err; } - fs.writeFile(TOKEN_PATH, JSON.stringify(token)); - console.log('Token stored to ' + TOKEN_PATH); + // ** new + Promise.try(function () { }) + .then(function () { + tokendb.save(function (err, result) { + if (err) { + console.error(err) + } else { + console.log(result); + } + }); + }); + + // -- old + // fs.writeFile(TOKEN_PATH, JSON.stringify(token)); + // console.log('Token stored to ' + TOKEN_PATH); } module.exports = { @@ -71,15 +90,28 @@ module.exports = { var redirectUrl = credentials.installed.redirect_uris[1]; var auth = new googleAuth(); var oauth2Client = new auth.OAuth2(clientId, clientSecret, REDIRECTURL); + var setToken = {} // set token from callback + Token.findOne({ email: 'tzbattleboy@gmail.com' }, function (err, token) { - fs.readFile(TOKEN_PATH, (err, token) => { if (err) { return callback(null, null, getNewToken(oauth2Client, callback)); - } else { - oauth2Client.credentials = JSON.parse(token); + } else if (token) { + setToken = { + access_token: token.access_token, + refresh_token: token.refresh_token, + token_type: token.token_type, + expiry_date: token.expiry_date + } + // console.log(setToken) + oauth2Client.credentials = setToken; return callback(null, oauth2Client); + } else { + return callback(null, null, getNewToken(oauth2Client, callback)); } }); + // fs.readFile(TOKEN_PATH, (err, token) => { + + // }); }); }, @@ -103,7 +135,16 @@ module.exports = { return; } else { oauth2Client.credentials = token; - storeToken(token); + var params = { userId: 'me', fields: 'emails', auth: oauth2Client }; + + plus.people.get(params, function (err, response) { + if (err) { + consol.error(err) + } else { + storeToken(token, response.emails); + } + }); + return callback(null, oauth2Client); } diff --git a/app/models/tokens.model.js b/app/models/tokens.model.js new file mode 100644 index 0000000..961103b --- /dev/null +++ b/app/models/tokens.model.js @@ -0,0 +1,12 @@ +var mongoose = require('mongoose') +var Schema = mongoose.Schema + +var TokenSchema = new Schema({ + email: String, + access_token: String, + expiry_date: Number, + refresh_token: String, + token_type: String +}) + +mongoose.model('Token', TokenSchema) \ No newline at end of file diff --git a/config/config.yml b/config/config.yml index 4dc1b50..5253081 100644 --- a/config/config.yml +++ b/config/config.yml @@ -4,12 +4,12 @@ localhost: server: url: 'localhost' urlto: 'localhost' - port: 3001 + port: 3030 portto: 4001 database: host: 'localhost' port: 27017 - name: 'ss7' + name: 'calendar' options: user: pass: @@ -17,4 +17,4 @@ localhost: apitimeout: 3000 #millisecond ggapi: calendarID: 'rvmbg3kg7uqninf7n3au1ku4mc@group.calendar.google.com' - redirectUrl: 'http://localhost:3001/oauth2callback' \ No newline at end of file + redirectUrl: 'http://localhost:3030/oauth2callback' \ No newline at end of file diff --git a/config/mongoose.js b/config/mongoose.js new file mode 100644 index 0000000..39b25bb --- /dev/null +++ b/config/mongoose.js @@ -0,0 +1,23 @@ +var mongoose = require('mongoose') +var yaml_config = require('node-yaml-config') +var config = yaml_config.load(__dirname + '/config.yml') +var url = config.server.url +var port = config.server.port +var dbhost = config.database.host +var dbport = config.database.port +var dbuser = config.database.options.user +var dbpass = config.database.options.pass +var dbname = config.database.name + +module.exports = function () { + // mongoose.connect('mongodb://username:password@host:port/database?options...') + var db + if (dbuser && dbpass) { + db = mongoose.connect('mongodb://' + dbuser + ':' + dbpass + '@' + dbhost + ':' + dbport + '/' + dbname) + } else { + db = mongoose.connect('mongodb://' + dbhost + ':' + dbport + '/' + dbname) + } + + require('../app/models/tokens.model') + return db +} \ No newline at end of file diff --git a/package.json b/package.json index 019aed5..bd2ab0d 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,8 @@ "moment-timezone": "^0.5.5", "node-yaml-config": "0.0.4", "scribe-js": "^2.0.4", - "unirest": "^0.5.0" + "unirest": "^0.5.0", + "mongoose": "^4.6.6" }, "devDependencies": { "bower": "^1.7.7", diff --git a/server.js b/server.js index fd324b1..9390a01 100644 --- a/server.js +++ b/server.js @@ -4,7 +4,8 @@ var yaml_config = require('node-yaml-config') var busboyBodyParser = require('busboy-body-parser') var scribe = require('scribe-js')() var Async = require('async') - +var mongoose = require('./config/mongoose') +var db = mongoose() var console = process.console var app = express() var router = express.Router() @@ -41,6 +42,6 @@ app.use('/logs', scribe.webPanel()) app.use(busboyBodyParser()) app.listen(port) - // console.log('Server is running at http://'+url+':'+port+''); +// console.log('Server is running at http://'+url+':'+port+''); console.tag('START').time().file().log('Server is running at http://' + url + ':' + port) \ No newline at end of file -- libgit2 0.21.2