Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dont require server config base url on login #3375

Merged
merged 4 commits into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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