Skip to content

Commit

Permalink
fix: dont require server config base url on login (#3375)
Browse files Browse the repository at this point in the history
  • Loading branch information
BruceMacD authored Oct 14, 2022
1 parent ec95247 commit d066e27
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
4 changes: 3 additions & 1 deletion ui/lib/serverconfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import useSWR from 'swr'

export function useServerConfig() {
const { data: { isEmailConfigured, isSignupEnabled, baseDomain } = {} } =
useSWR(`/api/server-configuration`)
useSWR(`/api/server-configuration`, {
revalidateIfStale: false,
})

return {
isEmailConfigured,
Expand Down
13 changes: 3 additions & 10 deletions ui/pages/login/callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ import { useRouter } from 'next/router'
import { useSWRConfig } from 'swr'

import { useUser } from '../../lib/hooks'
import { useServerConfig } from '../../lib/serverconfig'
import { saveToVisitedOrgs } from '.'

import LoginLayout from '../../components/layouts/login'

export default function Callback() {
const { mutate } = useSWRConfig()
const { baseDomain } = useServerConfig()
const { login } = useUser()

const router = useRouter()
Expand All @@ -30,11 +28,7 @@ export default function Callback() {
router.replace(next ? decodeURIComponent(next) : '/')

window.localStorage.removeItem('next')
saveToVisitedOrgs(
window.location.host,
baseDomain,
user?.organizationName
)
saveToVisitedOrgs(window.location.host, user?.organizationName)
}

const providerID = window.localStorage.getItem('providerID')
Expand All @@ -45,8 +39,7 @@ export default function Callback() {
state === window.localStorage.getItem('state') &&
code &&
providerID &&
redirectURL &&
baseDomain
redirectURL
) {
finish({
providerID,
Expand All @@ -58,7 +51,7 @@ export default function Callback() {
window.localStorage.removeItem('state')
window.localStorage.removeItem('redirectURL')
}
}, [code, state, mutate, router, baseDomain, login])
}, [code, state, mutate, router, login])

if (!isReady) {
return null
Expand Down
20 changes: 12 additions & 8 deletions ui/pages/login/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function oidcLogin({ id, clientID, authURL, scopes }, next) {
)}&state=${state}`
}

export function saveToVisitedOrgs(domain, baseDomain, orgName) {
export function saveToVisitedOrgs(domain, orgName) {
const cookies = new Cookies()

let visitedOrgs = cookies.get('orgs') || []
Expand All @@ -40,9 +40,17 @@ export function saveToVisitedOrgs(domain, baseDomain, orgName) {
name: orgName,
})

// set the cookie domain to a general base domain
let cookieDomain = window.location.host
let parts = cookieDomain.split('.')
if (parts.length > 2) {
parts.shift() // remove the org
cookieDomain = parts.join('.') // join the last two parts of the domain
}

cookies.set('orgs', visitedOrgs, {
path: '/',
domain: `.${baseDomain}`,
domain: `.${cookieDomain}`,
})
}
}
Expand Down Expand Up @@ -100,7 +108,7 @@ export default function Login() {
const [password, setPassword] = useState('')
const [error, setError] = useState('')
const [errors, setErrors] = useState({})
const { baseDomain, isEmailConfigured } = useServerConfig()
const { isEmailConfigured } = useServerConfig()
const { login } = useUser()

async function onSubmit(e) {
Expand All @@ -125,11 +133,7 @@ export default function Login() {

router.replace(next ? decodeURIComponent(next) : '/')

saveToVisitedOrgs(
window.location.host,
baseDomain,
data?.organizationName
)
saveToVisitedOrgs(window.location.host, data?.organizationName)
} catch (e) {
console.error(e)
if (e.fieldErrors) {
Expand Down

0 comments on commit d066e27

Please sign in to comment.