From 613982884479bb53ff55f220973c36868f13cc88 Mon Sep 17 00:00:00 2001 From: Max Eddy Date: Tue, 6 Jun 2017 15:31:50 -0400 Subject: [PATCH 1/5] Send post message with auth state to parent window. --- client/coral-embed/src/index.js | 4 ++++ client/coral-framework/actions/auth.js | 6 +++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/client/coral-embed/src/index.js b/client/coral-embed/src/index.js index 65e77ad1d..47c761bd3 100644 --- a/client/coral-embed/src/index.js +++ b/client/coral-embed/src/index.js @@ -54,6 +54,10 @@ function configurePymParent(pymParent, opts) { pymParent.sendMessage('config', JSON.stringify(config)); } + pymParent.onMessage('checkLogin', function(result) { + console.log(JSON.parse(result)); + }); + // Sends config to the child pymParent.onMessage('getConfig', function() { sendConfig(opts || {}); diff --git a/client/coral-framework/actions/auth.js b/client/coral-framework/actions/auth.js index eaaee3034..e4871a7e2 100644 --- a/client/coral-framework/actions/auth.js +++ b/client/coral-framework/actions/auth.js @@ -3,6 +3,7 @@ import bowser from 'bowser'; import * as actions from '../constants/auth'; import * as Storage from '../helpers/storage'; import coralApi, {base} from '../helpers/request'; +import pym from '../services/PymConnection'; import {resetWebsocket} from 'coral-framework/services/client'; import t from 'coral-framework/services/i18n'; @@ -300,7 +301,7 @@ const checkLoginSuccess = (user, isAdmin) => ({ isAdmin }); -export const checkLogin = () => (dispatch) => { +export const checkLogin = () => (dispatch, getState) => { dispatch(checkLoginRequest()); coralApi('/auth') .then((result) => { @@ -324,6 +325,9 @@ export const checkLogin = () => (dispatch) => { .catch((error) => { console.error(error); dispatch(checkLoginFailure(`${error.translation_key}`)); + }) + .then(() => { + pym.sendMessage('checkLogin', JSON.stringify(getState().auth)); }); }; From fd9b14a560fb306db7459f59d90ea15fd423bcc4 Mon Sep 17 00:00:00 2001 From: Max Eddy Date: Tue, 6 Jun 2017 15:54:28 -0400 Subject: [PATCH 2/5] cb option for auth change event --- client/coral-embed/src/index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/client/coral-embed/src/index.js b/client/coral-embed/src/index.js index 47c761bd3..d90591f2f 100644 --- a/client/coral-embed/src/index.js +++ b/client/coral-embed/src/index.js @@ -55,7 +55,9 @@ function configurePymParent(pymParent, opts) { } pymParent.onMessage('checkLogin', function(result) { - console.log(JSON.parse(result)); + if (opts.onAuthChange) { + opts.onAuthChange(JSON.parse(result)); + } }); // Sends config to the child From 4982d8a9adbf9f20fb56547d35cc49de94bbf42b Mon Sep 17 00:00:00 2001 From: Max Eddy Date: Thu, 8 Jun 2017 11:22:07 -0400 Subject: [PATCH 3/5] Use REST API result for check login message. --- client/coral-embed/src/index.js | 6 +++--- client/coral-framework/actions/auth.js | 7 +++---- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/client/coral-embed/src/index.js b/client/coral-embed/src/index.js index d90591f2f..c6c29c30e 100644 --- a/client/coral-embed/src/index.js +++ b/client/coral-embed/src/index.js @@ -54,9 +54,9 @@ function configurePymParent(pymParent, opts) { pymParent.sendMessage('config', JSON.stringify(config)); } - pymParent.onMessage('checkLogin', function(result) { - if (opts.onAuthChange) { - opts.onAuthChange(JSON.parse(result)); + pymParent.onMessage('coral-check-login', function(result) { + if (opts.onCheckLogin) { + opts.onCheckLogin(JSON.parse(result)); } }); diff --git a/client/coral-framework/actions/auth.js b/client/coral-framework/actions/auth.js index e4871a7e2..13a1bc24e 100644 --- a/client/coral-framework/actions/auth.js +++ b/client/coral-framework/actions/auth.js @@ -301,10 +301,12 @@ const checkLoginSuccess = (user, isAdmin) => ({ isAdmin }); -export const checkLogin = () => (dispatch, getState) => { +export const checkLogin = () => (dispatch) => { dispatch(checkLoginRequest()); coralApi('/auth') .then((result) => { + pym.sendMessage('coral-check-login', JSON.stringify(result)); + if (!result.user) { if (!bowser.safari && !bowser.ios) { Storage.removeItem('token'); @@ -325,9 +327,6 @@ export const checkLogin = () => (dispatch, getState) => { .catch((error) => { console.error(error); dispatch(checkLoginFailure(`${error.translation_key}`)); - }) - .then(() => { - pym.sendMessage('checkLogin', JSON.stringify(getState().auth)); }); }; From fe0c581ea926ee49dd5f2e91098965937a164c70 Mon Sep 17 00:00:00 2001 From: Max Eddy Date: Thu, 8 Jun 2017 13:05:34 -0400 Subject: [PATCH 4/5] Note logout action as well for onAuthChanged event. --- client/coral-embed/src/index.js | 12 ++++++++---- client/coral-framework/actions/auth.js | 3 ++- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/client/coral-embed/src/index.js b/client/coral-embed/src/index.js index c6c29c30e..0a901c341 100644 --- a/client/coral-embed/src/index.js +++ b/client/coral-embed/src/index.js @@ -54,11 +54,15 @@ function configurePymParent(pymParent, opts) { pymParent.sendMessage('config', JSON.stringify(config)); } - pymParent.onMessage('coral-check-login', function(result) { - if (opts.onCheckLogin) { - opts.onCheckLogin(JSON.parse(result)); + function onAuthChanged(message) { + if (opts.onAuthChanged) { + opts.onAuthChanged(message ? JSON.parse(message) : null); } - }); + } + + pymParent.onMessage('coral-login', onAuthChanged); + + pymParent.onMessage('coral-logout', onAuthChanged); // Sends config to the child pymParent.onMessage('getConfig', function() { diff --git a/client/coral-framework/actions/auth.js b/client/coral-framework/actions/auth.js index 13a1bc24e..b7ec4ee4c 100644 --- a/client/coral-framework/actions/auth.js +++ b/client/coral-framework/actions/auth.js @@ -285,6 +285,7 @@ export const logout = () => (dispatch) => { resetWebsocket(); dispatch({type: actions.LOGOUT}); + pym.sendMessage('coral-logout'); }); }; @@ -305,7 +306,6 @@ export const checkLogin = () => (dispatch) => { dispatch(checkLoginRequest()); coralApi('/auth') .then((result) => { - pym.sendMessage('coral-check-login', JSON.stringify(result)); if (!result.user) { if (!bowser.safari && !bowser.ios) { @@ -318,6 +318,7 @@ export const checkLogin = () => (dispatch) => { resetWebsocket(); dispatch(checkLoginSuccess(result.user)); + pym.sendMessage('coral-login', JSON.stringify(result.user)); // Display create username dialog if necessary. if (result.user.canEditName && result.user.status !== 'BANNED') { From 64174b97aff710d10b61a945a6c6919be1078b5b Mon Sep 17 00:00:00 2001 From: Max Eddy Date: Thu, 8 Jun 2017 13:20:30 -0400 Subject: [PATCH 5/5] Cleanup --- client/coral-embed/src/index.js | 8 ++------ client/coral-framework/actions/auth.js | 5 ++--- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/client/coral-embed/src/index.js b/client/coral-embed/src/index.js index 0a901c341..14de2756e 100644 --- a/client/coral-embed/src/index.js +++ b/client/coral-embed/src/index.js @@ -54,15 +54,11 @@ function configurePymParent(pymParent, opts) { pymParent.sendMessage('config', JSON.stringify(config)); } - function onAuthChanged(message) { + pymParent.onMessage('coral-auth-changed', function(message) { if (opts.onAuthChanged) { opts.onAuthChanged(message ? JSON.parse(message) : null); } - } - - pymParent.onMessage('coral-login', onAuthChanged); - - pymParent.onMessage('coral-logout', onAuthChanged); + }); // Sends config to the child pymParent.onMessage('getConfig', function() { diff --git a/client/coral-framework/actions/auth.js b/client/coral-framework/actions/auth.js index b7ec4ee4c..614b87632 100644 --- a/client/coral-framework/actions/auth.js +++ b/client/coral-framework/actions/auth.js @@ -285,7 +285,7 @@ export const logout = () => (dispatch) => { resetWebsocket(); dispatch({type: actions.LOGOUT}); - pym.sendMessage('coral-logout'); + pym.sendMessage('coral-auth-changed'); }); }; @@ -306,7 +306,6 @@ export const checkLogin = () => (dispatch) => { dispatch(checkLoginRequest()); coralApi('/auth') .then((result) => { - if (!result.user) { if (!bowser.safari && !bowser.ios) { Storage.removeItem('token'); @@ -318,7 +317,7 @@ export const checkLogin = () => (dispatch) => { resetWebsocket(); dispatch(checkLoginSuccess(result.user)); - pym.sendMessage('coral-login', JSON.stringify(result.user)); + pym.sendMessage('coral-auth-changed', JSON.stringify(result.user)); // Display create username dialog if necessary. if (result.user.canEditName && result.user.status !== 'BANNED') {