From e4cbc83f77e18cd09ba1dea58bcc413396589bba Mon Sep 17 00:00:00 2001 From: jbphet Date: Tue, 13 Jun 2023 16:30:56 -0600 Subject: [PATCH] improve handling of user ID for logged out case, see https://github.com/phetsims/rosetta/issues/412 --- src/client/js/getWebsiteUserData.js | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/client/js/getWebsiteUserData.js b/src/client/js/getWebsiteUserData.js index 53406a3..5f03995 100644 --- a/src/client/js/getWebsiteUserData.js +++ b/src/client/js/getWebsiteUserData.js @@ -28,8 +28,7 @@ import publicConfig from '../../common/publicConfig.js'; * For a user that is NOT logged in, it looks like this: * * websiteUserData = { - * "loggedIn": false, - * "userId": null + * "loggedIn": false * } * * @returns {Promise} @@ -40,16 +39,14 @@ const getWebsiteUserData = async () => { { withCredentials: true } // Include cookies so website backend can look up the session. ); - // Make sure the user ID is a number. We do this because on some servers it's a string and on others it's a number, - // see https://github.com/phetsims/rosetta/issues/373. - console.log( `--> websiteUserDataRes.data.userId = ${websiteUserDataRes.data.userId}` ); - console.log( `--> typeof websiteUserDataRes.data.userId = ${typeof websiteUserDataRes.data.userId}` ); - const userId = Number( websiteUserDataRes.data.userId ); - console.log( `--> userId = ${userId}` ); - const websiteUserData = websiteUserDataRes.data; - console.log( `--> websiteUserData = ${JSON.stringify( websiteUserData, null, 2 )}` ); - websiteUserData.userId = userId; + + // Make sure the user ID is a number if it is present. We do this because on some servers it's a string and on others + // it's a number, see https://github.com/phetsims/rosetta/issues/373. + if ( typeof websiteUserData.userId === 'string' && !isNaN( websiteUserData.userId ) ) { + websiteUserData.userId = Number( websiteUserData.userId ); + } + console.log( `websiteUserData.userId = ${websiteUserData.userId}` ); return websiteUserData; };