Skip to content

Commit c9942fb

Browse files
committed
cleanup: auth sync, respect redirectUrl and callbackUrl
1 parent 3331353 commit c9942fb

File tree

5 files changed

+12
-10
lines changed

5 files changed

+12
-10
lines changed

components/form.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1424,7 +1424,9 @@ export function BrandingUpload ({ label, groupClassName, name, ...props }) {
14241424
<img
14251425
src={tempId ? `${MEDIA_URL}/${tempId}` : '/favicon.png'}
14261426
alt={name}
1427-
style={{ objectFit: 'contain', position: 'relative', width: '100%', height: '100%' }}
1427+
width={100}
1428+
height={100}
1429+
style={{ objectFit: 'contain', position: 'relative' }}
14281430
/>
14291431
<Avatar
14301432
onSuccess={handleSuccess}

components/login.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export default function Login ({ providers, callbackUrl, multiAuth, error, text,
124124
text={`${text || 'Login'} with`}
125125
/>
126126
)
127-
case 'Sync': // TODO: remove this
127+
case 'Sync':
128128
return null
129129
default:
130130
return (

components/territory-domains.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ export default function CustomDomainForm ({ sub }) {
184184
return (
185185
<>
186186
<Form
187-
initial={{ domain }}
187+
initial={{ domain: domain || sub?.customDomain?.domain }}
188188
schema={customDomainSchema}
189189
onSubmit={onSubmit}
190190
className='mb-2'

pages/api/auth/sync.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ export default async function handler (req, res) {
1010
}
1111

1212
// redirectUrl parse
13+
const decodedRedirectUrl = decodeURIComponent(redirectUrl)
1314
let customDomain
1415
try {
15-
customDomain = new URL(redirectUrl)
16+
customDomain = new URL(decodedRedirectUrl)
1617
const domain = await models.customDomain.findUnique({ where: { domain: customDomain.host, status: 'ACTIVE' } })
1718
if (!domain) {
1819
return res.status(400).json({ status: 'ERROR', reason: 'custom domain not found' })
@@ -27,7 +28,7 @@ export default async function handler (req, res) {
2728
const session = await getServerSession(req, res, getAuthOptions(req, res))
2829
if (!session) {
2930
// redirect to the login page, middleware will handle the rest
30-
return res.redirect(mainDomain + '/login?callbackUrl=' + encodeURIComponent(redirectUrl))
31+
return res.redirect(mainDomain + '/login?callbackUrl=' + encodeURIComponent(decodedRedirectUrl))
3132
}
3233

3334
try {
@@ -48,9 +49,9 @@ export default async function handler (req, res) {
4849
}
4950

5051
// domain provider will handle this sync request
51-
const customDomainCallback = new URL('/?type=sync', redirectUrl)
52+
const customDomainCallback = new URL('/?type=sync', decodedRedirectUrl)
5253
customDomainCallback.searchParams.set('token', token)
53-
customDomainCallback.searchParams.set('callbackUrl', redirectUrl)
54+
customDomainCallback.searchParams.set('callbackUrl', decodedRedirectUrl)
5455
if (multiAuth) {
5556
customDomainCallback.searchParams.set('multiAuth', multiAuth)
5657
}

pages/login.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export async function getServerSideProps ({ req, res, query: { callbackUrl, mult
2121
// let undefined urls through without redirect ... otherwise this interferes with multiple auth linking
2222
let external = true
2323
try {
24-
external = isExternal(decodeURIComponent(callbackUrl))
24+
external = isExternal(decodeURIComponent(callbackUrl)) && !domain
2525
} catch (err) {
2626
console.error('error decoding callback:', callbackUrl, err)
2727
}
@@ -30,11 +30,10 @@ export async function getServerSideProps ({ req, res, query: { callbackUrl, mult
3030
callbackUrl = '/'
3131
}
3232

33-
// TODO: custom domain mapping security
3433
// If we're coming from a custom domain, set as callbackUrl the auth sync endpoint
3534
if (domain) {
3635
const params = new URLSearchParams()
37-
params.set('redirectUrl', 'https://' + encodeURIComponent(domain))
36+
params.set('redirectUrl', encodeURIComponent(callbackUrl))
3837
if (multiAuth) { // take care of multiAuth if requested
3938
params.set('multiAuth', multiAuth)
4039
}

0 commit comments

Comments
 (0)