From 855e6cb056986b8e53c9c73f0b0e410c211dd19a Mon Sep 17 00:00:00 2001 From: benweet Date: Sun, 6 Aug 2017 15:04:00 +0100 Subject: [PATCH] Added oauth2 callback --- build/dev-server.js | 4 ++-- index.html | 2 +- package.json | 2 +- src/components/SideBar.vue | 5 +++++ src/services/userSvc.js | 15 +++++++++++---- src/store/modules/data.js | 30 ++++++++++++++++++++---------- static/.gitkeep | 0 static/oauth2/callback.html | 28 ++++++++++++++++++++++++++++ 8 files changed, 68 insertions(+), 18 deletions(-) delete mode 100644 static/.gitkeep create mode 100644 static/oauth2/callback.html diff --git a/build/dev-server.js b/build/dev-server.js index 782dc6fc..3e9549e8 100644 --- a/build/dev-server.js +++ b/build/dev-server.js @@ -59,8 +59,8 @@ app.use(devMiddleware) app.use(hotMiddleware) // serve pure static assets -var staticPath = path.posix.join(config.dev.assetsPublicPath, config.dev.assetsSubDirectory) -app.use(staticPath, express.static('./static')) +// var staticPath = path.posix.join(config.dev.assetsPublicPath, config.dev.assetsSubDirectory) +app.use(express.static('./static')) var uri = 'http://localhost:' + port diff --git a/index.html b/index.html index a8261783..962f580a 100644 --- a/index.html +++ b/index.html @@ -2,7 +2,7 @@ - my-project + StackEdit
diff --git a/package.json b/package.json index 92f71bef..36e27453 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "my-project", + "name": "StackEdit", "version": "1.0.0", "description": "A Vue.js project", "author": "", diff --git a/src/components/SideBar.vue b/src/components/SideBar.vue index 04cc9ba2..f55e7b06 100644 --- a/src/components/SideBar.vue +++ b/src/components/SideBar.vue @@ -18,6 +18,11 @@
Sign in with Google
Have all your files and settings backed up and synced. + + +
Sign in on CouchDB
+ Save and collaborate on a CouchDB hosted by you. +
Table of contents diff --git a/src/services/userSvc.js b/src/services/userSvc.js index 9a9f0083..8a5321eb 100644 --- a/src/services/userSvc.js +++ b/src/services/userSvc.js @@ -1,4 +1,5 @@ import utils from './utils'; +import store from '../store'; const googleClientId = '241271498917-t4t7d07qis7oc0ahaskbif3ft6tk63cd.apps.googleusercontent.com'; const appUri = 'http://localhost:8080/'; @@ -12,8 +13,8 @@ export default { const state = utils.uid(); let authorizeUrl = 'https://accounts.google.com/o/oauth2/v2/auth'; authorizeUrl = utils.addQueryParam(authorizeUrl, 'client_id', googleClientId); - authorizeUrl = utils.addQueryParam(authorizeUrl, 'response_type', 'code'); - authorizeUrl = utils.addQueryParam(authorizeUrl, 'redirect_uri', `${appUri}oauth2/google/callback`); + authorizeUrl = utils.addQueryParam(authorizeUrl, 'response_type', 'token'); + authorizeUrl = utils.addQueryParam(authorizeUrl, 'redirect_uri', `${appUri}oauth2/callback.html`); authorizeUrl = utils.addQueryParam(authorizeUrl, 'state', state); if (googleAppsDomain) { authorizeUrl = utils.addQueryParam(authorizeUrl, 'scope', 'openid email'); @@ -33,8 +34,14 @@ export default { && event.data.state === state ) { this.cleanOauth2Context(); - console.log(event.data); - resolve(); + if (event.data.accessToken) { + store.dispatch('data/patchTokens', { + googleToken: { + accessToken: event.data.accessToken, + }, + }); + resolve(); + } } }; window.addEventListener('message', msgHandler); diff --git a/src/store/modules/data.js b/src/store/modules/data.js index d4452e46..44f87151 100644 --- a/src/store/modules/data.js +++ b/src/store/modules/data.js @@ -1,24 +1,28 @@ import moduleTemplate from './moduleTemplate'; import defaultLocalSettings from '../../data/defaultLocalSettings'; +const empty = (id) => { + switch (id) { + case 'localSettings': + return defaultLocalSettings(); + default: + return { id, updated: 0 }; + } +}; +const module = moduleTemplate(empty); + +const getter = id => state => state.itemMap[id] || empty(id); + const localSettingsToggler = propertyName => ({ getters, dispatch }, value) => { dispatch('patchLocalSettings', { [propertyName]: value === undefined ? !getters.localSettings[propertyName] : value, }); }; -const module = moduleTemplate((id) => { - switch (id) { - case 'localSettings': - return defaultLocalSettings(); - default: - throw new Error(`Unknown data id ${id}`); - } -}); - module.getters = { ...module.getters, - localSettings: state => state.itemMap.localSettings || defaultLocalSettings(), + localSettings: getter('localSettings'), + tokens: getter('tokens'), }; module.actions = { @@ -29,6 +33,12 @@ module.actions = { id: 'localSettings', }); }, + patchTokens({ getters, commit }, value) { + commit('patchOrSetItem', { + ...value, + id: 'tokens', + }); + }, toggleNavigationBar: localSettingsToggler('showNavigationBar'), toggleEditor: localSettingsToggler('showEditor'), toggleSidePreview: localSettingsToggler('showSidePreview'), diff --git a/static/.gitkeep b/static/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/static/oauth2/callback.html b/static/oauth2/callback.html new file mode 100644 index 00000000..17c3059a --- /dev/null +++ b/static/oauth2/callback.html @@ -0,0 +1,28 @@ + + + + + +