diff --git a/client/coral-admin/src/AppRouter.js b/client/coral-admin/src/AppRouter.js index 1faaa6b51..fc3bbd0c0 100644 --- a/client/coral-admin/src/AppRouter.js +++ b/client/coral-admin/src/AppRouter.js @@ -1,6 +1,6 @@ import React from 'react'; -import {Router, Route, IndexRedirect, browserHistory} from 'react-router'; -import {useBasename} from 'history'; +import {Router, Route, IndexRedirect, Redirect} from 'react-router'; +import {history} from 'coral-framework/helpers/router'; import Configure from 'routes/Configure'; import Dashboard from 'routes/Dashboard'; @@ -54,16 +54,11 @@ const routes = ( - - - - ); -const AppRouter = () => browserHistory)({ -})} routes={routes}/>; +const AppRouter = () => ; export default AppRouter; diff --git a/client/coral-embed-stream/src/AppRouter.js b/client/coral-embed-stream/src/AppRouter.js index 6ee63caab..c2e7b2c5d 100644 --- a/client/coral-embed-stream/src/AppRouter.js +++ b/client/coral-embed-stream/src/AppRouter.js @@ -1,5 +1,6 @@ import React from 'react'; -import {Router, Route, browserHistory} from 'react-router'; +import {Router, Route} from 'react-router'; +import {history} from 'coral-framework/helpers/router'; import Embed from './containers/Embed'; import {LoginContainer} from 'coral-sign-in/containers/LoginContainer'; @@ -11,6 +12,6 @@ const routes = ( ); -const AppRouter = () => ; +const AppRouter = () => ; export default AppRouter; diff --git a/client/coral-embed-stream/src/components/Embed.js b/client/coral-embed-stream/src/components/Embed.js index a069984dc..f918b7a16 100644 --- a/client/coral-embed-stream/src/components/Embed.js +++ b/client/coral-embed-stream/src/components/Embed.js @@ -36,7 +36,7 @@ export default class Embed extends React.Component {
{ init(JSON.parse(config)); }); + + pym.onMessage('login', (token) => { + if (token) { + store.dispatch(handleAuthToken(token)); + } + store.dispatch(checkLogin()); + }); + + pym.onMessage('logout', () => { + store.dispatch(logout()); + }); } else { init(); } diff --git a/client/coral-embed/src/index.js b/client/coral-embed/src/index.js index fda97c4b3..64c274660 100644 --- a/client/coral-embed/src/index.js +++ b/client/coral-embed/src/index.js @@ -5,8 +5,6 @@ import {buildUrl} from 'coral-framework/utils'; import queryString from 'query-string'; import EventEmitter from 'eventemitter2'; -const eventEmitter = new EventEmitter({wildcard: true}); - // TODO: Styles should live in a separate file const snackbarStyles = { position: 'fixed', @@ -50,7 +48,7 @@ function buildStreamIframeUrl(talkBaseUrl, query) { // Set up postMessage listeners/handlers on the pymParent // e.g. to resize the iframe, and navigate the host page -function configurePymParent(pymParent, opts) { +function configurePymParent(pymParent, eventEmitter, opts) { let notificationOffset = 200; let cachedHeight; const snackbar = document.createElement('div'); @@ -203,6 +201,23 @@ function configurePymParent(pymParent, opts) { * @param {String} [opts.asset_url] - Asset URL * @param {String} [opts.asset_id] - Asset ID * @param {String} [opts.auth_token] - (optional) A jwt representing the session + * @return {Object} + * + * Example: + * ``` + * const embed = Talk.render(document.getElementById('talkStreamEmbed'), opts); + * + * // trigger a login with optional token. + * embed.login(token); + * + * // trigger a logout. + * embed.logout(); + * + * // listen to events (in this case all events). + * embed.on('**', function(value) { + * console.log(this.event, value); + * }); + * ``` */ Talk.render = function(el, opts) { if (!el) { @@ -256,14 +271,31 @@ Talk.render = function(el, opts) { } } + const pymParent = new pym.Parent(el.id, buildStreamIframeUrl(opts.talk, query), { + title: opts.title, + id: `${el.id}_iframe`, + name: `${el.id}_iframe` + }); + + const eventEmitter = new EventEmitter({wildcard: true}); + configurePymParent( - new pym.Parent(el.id, buildStreamIframeUrl(opts.talk, query), { - title: opts.title, - id: `${el.id}_iframe`, - name: `${el.id}_iframe` - }), + pymParent, + eventEmitter, opts ); + + return { + on(eventName, callback) { + eventEmitter.on(eventName, callback); + }, + login(token) { + pymParent.sendMessage('login', token); + }, + logout() { + pymParent.sendMessage('logout'); + } + }; }; export default Coral; diff --git a/client/coral-framework/actions/auth.js b/client/coral-framework/actions/auth.js index d3c29dc3c..f4cf5217e 100644 --- a/client/coral-framework/actions/auth.js +++ b/client/coral-framework/actions/auth.js @@ -276,9 +276,7 @@ export const fetchForgotPassword = (email) => (dispatch, getState) => { export const logout = () => (dispatch) => { return coralApi('/auth', {method: 'DELETE'}).then(() => { - if (!bowser.safari && !bowser.ios) { - Storage.removeItem('token'); - } + Storage.removeItem('token'); // Reset the websocket. resetWebsocket(); @@ -306,9 +304,7 @@ export const checkLogin = () => (dispatch) => { coralApi('/auth') .then((result) => { if (!result.user) { - if (!bowser.safari && !bowser.ios) { - Storage.removeItem('token'); - } + Storage.removeItem('token'); throw new Error('Not logged in'); } @@ -325,6 +321,11 @@ export const checkLogin = () => (dispatch) => { }) .catch((error) => { console.error(error); + if (error.status && error.status === 401) { + + // Unauthorized. + Storage.removeItem('token'); + } const errorMessage = error.translation_key ? t(`error.${error.translation_key}`) : error.toString(); dispatch(checkLoginFailure(errorMessage)); }); diff --git a/client/coral-framework/helpers/request.js b/client/coral-framework/helpers/request.js index 782aa83f6..ffddc9c5e 100644 --- a/client/coral-framework/helpers/request.js +++ b/client/coral-framework/helpers/request.js @@ -67,6 +67,7 @@ const handleResp = (res) => { } error.message = message; + error.status = res.status; throw error; }); } else if (res.status === 204) { diff --git a/client/coral-framework/helpers/router.js b/client/coral-framework/helpers/router.js new file mode 100644 index 000000000..e6277e9d8 --- /dev/null +++ b/client/coral-framework/helpers/router.js @@ -0,0 +1,7 @@ +import {useBasename} from 'history'; +import {browserHistory} from 'react-router'; +import {BASE_PATH} from 'coral-framework/constants/url'; + +export const history = useBasename(() => browserHistory)({ + basename: BASE_PATH +}); diff --git a/views/article.ejs b/views/article.ejs index a98aa81c3..098a95524 100644 --- a/views/article.ejs +++ b/views/article.ejs @@ -26,7 +26,7 @@

Admin - All Assets