Skip to content

Commit

Permalink
PORTALS-3299: proposed solution for oauth sign in app when prompt is …
Browse files Browse the repository at this point in the history
…set to login
  • Loading branch information
jay-hodgson committed Oct 29, 2024
1 parent bf37dd4 commit 25db386
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 3 deletions.
1 change: 1 addition & 0 deletions apps/synapse-oauth-signin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"react-dom": "^18.2.0",
"react-router-dom": "^5.3.4",
"sass": "^1.71.1",
"universal-cookie": "^4.0.4",
"synapse-react-client": "workspace:*"
},
"devDependencies": {
Expand Down
8 changes: 7 additions & 1 deletion apps/synapse-oauth-signin/src/AppInitializer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ import {
SynapseConstants,
} from 'synapse-react-client'
import { handleErrorRedirect } from './URLUtils'
import UniversalCookies from 'universal-cookie'

const cookies = new UniversalCookies()
function AppInitializer(
props: React.PropsWithChildren<Record<string, unknown>>,
) {
const accountSitePrompted = cookies.get(
SynapseConstants.ACCOUNT_SITE_PROMPTED_FOR_LOGIN_COOKIE_KEY,
) // short-lived cookie
const urlSearchParams = new URLSearchParams(window.location.search)
const prompt = urlSearchParams.get('prompt')
const prompt =
accountSitePrompted == 'true' ? 'none' : urlSearchParams.get('prompt')

let maxAge = undefined
// check max age when re-establishing the session, not to auto-consent.
Expand Down
19 changes: 17 additions & 2 deletions packages/synapse-react-client/src/utils/AppUtils/AppUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { useHistory } from 'react-router-dom'
import { LAST_PLACE_LOCALSTORAGE_KEY } from '../SynapseConstants'
import {
ACCOUNT_SITE_PROMPTED_FOR_LOGIN_COOKIE_KEY,
LAST_PLACE_LOCALSTORAGE_KEY,
} from '../SynapseConstants'
import { useEffect, useState } from 'react'
import UniversalCookies from 'universal-cookie'

Expand Down Expand Up @@ -33,10 +36,22 @@ export function storeRedirectURLForOneSageLoginAndGotoURL(href: string) {
}

export function processRedirectURLInOneSage() {
// PORTALS-3299 : Indicate that we have completed the login workflow (cookie expires in a minute) to break out of a cycle
const expireDate = new Date()
expireDate.setMinutes(expireDate.getMinutes() + 1)
const hostname = window.location.hostname.toLowerCase()
cookies.set(ACCOUNT_SITE_PROMPTED_FOR_LOGIN_COOKIE_KEY, 'true', {
path: '/',
expires: expireDate,
domain: hostname.endsWith('.synapse.org') ? 'synapse.org' : undefined,
})

if (cookies.get(ONE_SAGE_REDIRECT_COOKIE_KEY)) {
const href = cookies.get(ONE_SAGE_REDIRECT_COOKIE_KEY)
cookies.remove(ONE_SAGE_REDIRECT_COOKIE_KEY)
window.location.assign(href)
setTimeout(() => {
window.location.assign(href)
}, 10)
return true
}
//else
Expand Down
3 changes: 3 additions & 0 deletions packages/synapse-react-client/src/utils/SynapseConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ export const ACCESS_TOKEN_COOKIE_KEY =
'org.sagebionetworks.security.user.login.token'
export const LAST_PLACE_LOCALSTORAGE_KEY = 'last_place_url'

export const ACCOUNT_SITE_PROMPTED_FOR_LOGIN_COOKIE_KEY =
'org.sagebionetworks.account.promptedforlogin'

/* Persistent localStorage keys on SWC logout */
export const PERSISTENT_LOCAL_STORAGE_KEYS = [
...ORIENTATION_BANNER_KEYS,
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 25db386

Please sign in to comment.