Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
Some cleanup and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
barankyle committed Feb 29, 2024
1 parent 656bf39 commit 9ddd052
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 59 deletions.
107 changes: 49 additions & 58 deletions packages/client-core/src/user/services/AuthService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,33 @@ const resolveWalletUser = (credentials: any): UserType => {
}
}

const waitForToken = async(win, clientUrl): Promise<string> => {
return new Promise(resolve => {
console.log('waitForToken')
win.postMessage(JSON.stringify({
key: `${stateNamespaceKey}.AuthState.authUser`,
method: "get"
}), clientUrl);
const getIframeResponse = function (e) {
console.log('got message from iframe for getIframeResponse', e, e?.data)
if (e.origin !== clientUrl) return
if (e?.data) {
try {
const value = JSON.parse(e.data)
console.log('value', value)
if (value?.accessToken != null) {
console.log('accessToken exists')
window.removeEventListener('message', getIframeResponse)
resolve(value?.accessToken)
}
} catch {
resolve('')
}
} else resolve(e)
}
window.addEventListener('message', getIframeResponse)
})
}


const getToken = async(): Promise<string> => {
Expand All @@ -139,6 +166,21 @@ const getToken = async(): Promise<string> => {
win = iframe!.contentWindow;
}

window.addEventListener('message', (e) => {
if (e?.data) {
try {
const value = JSON.parse(e.data)
console.log('value', value)
if (value?.invalidDomain != null) {
console.log('invalid Domain')
localStorage.setItem('invalidCrossOriginDomain', 'true')
}
} catch(err) {
//
}
}
})

console.log('app host', `https://${process.env.VITE_APP_HOST}`)
console.log('posting checkAccess', config.client.clientUrl)
const clientUrl = config.client.clientUrl
Expand Down Expand Up @@ -170,25 +212,12 @@ const getToken = async(): Promise<string> => {
})
console.log('hasAccess', hasAccess)
console.log('retrieving token from root storage')
if (!hasAccess.cookieSet) {
console.log('cookieSet is null')
const skipCheck = localStorage.getItem('skipCrossOriginCookieCheck')
console.log('skipCheck', skipCheck)
if (skipCheck === 'true') {
console.log('JORTS')
const authState = getMutableState(AuthState)
const accessToken = authState?.authUser?.accessToken?.value
return Promise.resolve(accessToken?.length > 0 ? accessToken : null)
} //else window.location.href = `${clientUrl}/main-site-cookie-acknowledgment.html?redirect=${window.location}`
else {
iframe.style.display = 'block'
}
}
else if (!hasAccess.hasStorageAccess) {
if (!hasAccess.cookieSet || !hasAccess.hasStorageAccess) {
console.log('does not have storage access')
const skipCheck = localStorage.getItem('skipCrossOriginCookieCheck')
const invalidCrossOriginDomain = localStorage.getItem('invalidCrossOriginDomain')
console.log('skipCheck', skipCheck)
if (skipCheck === 'true') {
if (skipCheck === 'true' || invalidCrossOriginDomain === 'true') {
console.log('SHORTS')
const authState = getMutableState(AuthState)
const accessToken = authState?.authUser?.accessToken?.value
Expand All @@ -213,27 +242,9 @@ const getToken = async(): Promise<string> => {
if (!e.data) resolve({ hasStorageAccess: false, cookieSet: false })
const data = JSON.parse(e.data)
console.log('data', data)
win.postMessage(JSON.stringify({
key: `${stateNamespaceKey}.AuthState.authUser`,
method: "get"
}), clientUrl);
return await new Promise(resolve => {
const getIframeResponse = function (e) {
console.log('got message from iframe for getIframeResponse', e, e?.data)
window.removeEventListener('message', getIframeResponse)
if (e.origin !== clientUrl) return
if (e?.data) {
try {
const value = JSON.parse(e.data)
console.log('value', value)
resolve(value?.accessToken)
} catch {
resolve(null)
}
} else resolve(e)
}
window.addEventListener('message', getIframeResponse)
})
console.log('Waiting for token after click')
const token = await waitForToken(win, clientUrl)
resolve(token)
}
window.addEventListener('message', hasAccessListener)
}
Expand All @@ -245,27 +256,7 @@ const getToken = async(): Promise<string> => {
})
}
} else {
win.postMessage(JSON.stringify({
key: `${stateNamespaceKey}.AuthState.authUser`,
method: "get"
}), clientUrl);
return await new Promise(resolve => {
const getIframeResponse = function (e) {
console.log('got message from iframe for getIframeResponse', e, e?.data)
window.removeEventListener('message', getIframeResponse)
if (e.origin !== clientUrl) return
if (e?.data) {
try {
const value = JSON.parse(e.data)
console.log('value', value)
resolve(value?.accessToken)
} catch {
resolve(null)
}
} else resolve(e)
}
window.addEventListener('message', getIframeResponse)
})
return waitForToken(win, clientUrl)
}
}

Expand Down
2 changes: 2 additions & 0 deletions packages/client/public/root-cookie-accessor-template.html
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@
document.cookie = `allowedDomains=${JSON.stringify(allowedDomains)}; SameSite=None; Secure`
await processMessage(e)
}
} else {
parent.postMessage(JSON.stringify({ invalidDomain: true }), e.origin)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const checkDomain = async (context: HookContext<AllowedDomainsService>) => {
console.log('domainToCheck', domainToCheck)
const additionalDomains = params.additionalDomains
console.log('additionalDomains', additionalDomains)
let allowedDomains = [`https://${appConfig.server.clientHost}`, 'https://api-local.etherealengine.com:3000', 'https://local.etherealengine.com:3000']
let allowedDomains = [`https://${appConfig.server.clientHost}`, 'https://cool.pants.com', 'https://hot.pants.com']
console.log('initial allowedDomains', allowedDomains)

if (additionalDomains && Array.isArray(additionalDomains))
Expand Down

0 comments on commit 9ddd052

Please sign in to comment.