Added oauth2 callback

This commit is contained in:
benweet 2017-08-06 15:04:00 +01:00
parent d09375dc4c
commit 855e6cb056
8 changed files with 68 additions and 18 deletions

View File

@ -59,8 +59,8 @@ app.use(devMiddleware)
app.use(hotMiddleware) app.use(hotMiddleware)
// serve pure static assets // serve pure static assets
var staticPath = path.posix.join(config.dev.assetsPublicPath, config.dev.assetsSubDirectory) // var staticPath = path.posix.join(config.dev.assetsPublicPath, config.dev.assetsSubDirectory)
app.use(staticPath, express.static('./static')) app.use(express.static('./static'))
var uri = 'http://localhost:' + port var uri = 'http://localhost:' + port

View File

@ -2,7 +2,7 @@
<html> <html>
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<title>my-project</title> <title>StackEdit</title>
</head> </head>
<body> <body>
<div id="app"></div> <div id="app"></div>

View File

@ -1,5 +1,5 @@
{ {
"name": "my-project", "name": "StackEdit",
"version": "1.0.0", "version": "1.0.0",
"description": "A Vue.js project", "description": "A Vue.js project",
"author": "", "author": "",

View File

@ -18,6 +18,11 @@
<div>Sign in with Google</div> <div>Sign in with Google</div>
<span>Have all your files and settings backed up and synced.</span> <span>Have all your files and settings backed up and synced.</span>
</side-bar-item> </side-bar-item>
<side-bar-item @click.native="signin">
<icon-login slot="icon"></icon-login>
<div>Sign in on CouchDB</div>
<span>Save and collaborate on a CouchDB hosted by you.</span>
</side-bar-item>
<side-bar-item @click.native="panel = 'toc'"> <side-bar-item @click.native="panel = 'toc'">
<icon-toc slot="icon"></icon-toc> <icon-toc slot="icon"></icon-toc>
Table of contents Table of contents

View File

@ -1,4 +1,5 @@
import utils from './utils'; import utils from './utils';
import store from '../store';
const googleClientId = '241271498917-t4t7d07qis7oc0ahaskbif3ft6tk63cd.apps.googleusercontent.com'; const googleClientId = '241271498917-t4t7d07qis7oc0ahaskbif3ft6tk63cd.apps.googleusercontent.com';
const appUri = 'http://localhost:8080/'; const appUri = 'http://localhost:8080/';
@ -12,8 +13,8 @@ export default {
const state = utils.uid(); const state = utils.uid();
let authorizeUrl = 'https://accounts.google.com/o/oauth2/v2/auth'; let authorizeUrl = 'https://accounts.google.com/o/oauth2/v2/auth';
authorizeUrl = utils.addQueryParam(authorizeUrl, 'client_id', googleClientId); authorizeUrl = utils.addQueryParam(authorizeUrl, 'client_id', googleClientId);
authorizeUrl = utils.addQueryParam(authorizeUrl, 'response_type', 'code'); authorizeUrl = utils.addQueryParam(authorizeUrl, 'response_type', 'token');
authorizeUrl = utils.addQueryParam(authorizeUrl, 'redirect_uri', `${appUri}oauth2/google/callback`); authorizeUrl = utils.addQueryParam(authorizeUrl, 'redirect_uri', `${appUri}oauth2/callback.html`);
authorizeUrl = utils.addQueryParam(authorizeUrl, 'state', state); authorizeUrl = utils.addQueryParam(authorizeUrl, 'state', state);
if (googleAppsDomain) { if (googleAppsDomain) {
authorizeUrl = utils.addQueryParam(authorizeUrl, 'scope', 'openid email'); authorizeUrl = utils.addQueryParam(authorizeUrl, 'scope', 'openid email');
@ -33,9 +34,15 @@ export default {
&& event.data.state === state && event.data.state === state
) { ) {
this.cleanOauth2Context(); this.cleanOauth2Context();
console.log(event.data); if (event.data.accessToken) {
store.dispatch('data/patchTokens', {
googleToken: {
accessToken: event.data.accessToken,
},
});
resolve(); resolve();
} }
}
}; };
window.addEventListener('message', msgHandler); window.addEventListener('message', msgHandler);
const checkClosedInterval = setInterval(() => { const checkClosedInterval = setInterval(() => {

View File

@ -1,24 +1,28 @@
import moduleTemplate from './moduleTemplate'; import moduleTemplate from './moduleTemplate';
import defaultLocalSettings from '../../data/defaultLocalSettings'; 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) => { const localSettingsToggler = propertyName => ({ getters, dispatch }, value) => {
dispatch('patchLocalSettings', { dispatch('patchLocalSettings', {
[propertyName]: value === undefined ? !getters.localSettings[propertyName] : value, [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 = {
...module.getters, ...module.getters,
localSettings: state => state.itemMap.localSettings || defaultLocalSettings(), localSettings: getter('localSettings'),
tokens: getter('tokens'),
}; };
module.actions = { module.actions = {
@ -29,6 +33,12 @@ module.actions = {
id: 'localSettings', id: 'localSettings',
}); });
}, },
patchTokens({ getters, commit }, value) {
commit('patchOrSetItem', {
...value,
id: 'tokens',
});
},
toggleNavigationBar: localSettingsToggler('showNavigationBar'), toggleNavigationBar: localSettingsToggler('showNavigationBar'),
toggleEditor: localSettingsToggler('showEditor'), toggleEditor: localSettingsToggler('showEditor'),
toggleSidePreview: localSettingsToggler('showSidePreview'), toggleSidePreview: localSettingsToggler('showSidePreview'),

View File

View File

@ -0,0 +1,28 @@
<!DOCTYPE html>
<html>
<body>
<script>
var state;
var accessToken;
function parse(search) {
(search || '').slice(1).split('&').forEach(function (param) {
var split = param.split('=');
var key = decodeURIComponent(split.shift());
var value = decodeURIComponent(split.join('='));
if (key === 'state') {
state = value;
} else if (key === 'access_token') {
accessToken = value;
}
});
}
parse(location.search);
parse(location.hash);
var origin = location.protocol + '//' + location.host;
opener.postMessage({
state: state,
accessToken: accessToken
}, origin);
</script>
</body>
</html>