Skip to content

Commit 6db07b8

Browse files
committed
fix login flow, temporarily disable auto-auth, fix OAuth login
1 parent 76df54a commit 6db07b8

File tree

8 files changed

+60
-34
lines changed

8 files changed

+60
-34
lines changed

components/login.js

+2
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ export default function Login ({ providers, callbackUrl, multiAuth, error, text,
109109
text={`${text || 'Login'} with`}
110110
/>
111111
)
112+
case 'Sync': // TODO: remove this
113+
return null
112114
default:
113115
return (
114116
<OverlayTrigger

components/nav/common.js

+14-23
Original file line numberDiff line numberDiff line change
@@ -249,34 +249,20 @@ export function SignUpButton ({ className = 'py-0', width }) {
249249
export default function LoginButton () {
250250
const router = useRouter()
251251

252-
// TODO: atp let main domain handle the login UX/UI
253-
// decree a better position/way for this
252+
// TODO: alternative to this, for test only
254253
useEffect(() => {
254+
console.log(router.query)
255255
if (router.query.type === 'sync') {
256256
signIn('sync', { token: router.query.token, callbackUrl: router.query.callbackUrl, redirect: false })
257257
}
258-
}, [router.query.type, router.query.token, router.query.callbackUrl])
258+
}, [router.query])
259259

260260
const handleLogin = useCallback(async () => {
261-
// todo: custom domain check
262-
const mainDomain = process.env.NEXT_PUBLIC_URL.replace(/^https?:\/\//, '')
263-
const isCustomDomain = window.location.hostname !== mainDomain
264-
265-
if (isCustomDomain && router.query.type !== 'noAuth') {
266-
// TODO: dirty of previous iterations, refactor
267-
// redirect to sync endpoint on main domain
268-
const protocol = window.location.protocol
269-
const mainDomainUrl = `${protocol}//${mainDomain}`
270-
const currentUrl = window.location.origin + router.asPath
271-
272-
window.location.href = `${mainDomainUrl}/api/auth/sync?redirectUrl=${encodeURIComponent(currentUrl)}`
273-
} else {
274-
// normal login on main domain
275-
await router.push({
276-
pathname: '/login',
277-
query: { callbackUrl: window.location.origin + router.asPath }
278-
})
279-
}
261+
// normal login on main domain
262+
await router.push({
263+
pathname: '/login',
264+
query: { callbackUrl: window.location.origin + router.asPath }
265+
})
280266
}, [router])
281267

282268
return (
@@ -297,6 +283,11 @@ function LogoutObstacle ({ onClose }) {
297283
const { removeLocalWallets } = useWallets()
298284
const { nextAccount } = useAccounts()
299285
const router = useRouter()
286+
const [isCustomDomain, setIsCustomDomain] = useState(false)
287+
288+
useEffect(() => {
289+
setIsCustomDomain(router.host !== process.env.NEXT_PUBLIC_URL.replace(/^https?:\/\//, ''))
290+
}, [router.host])
300291

301292
return (
302293
<div className='d-flex m-auto flex-column w-fit-content'>
@@ -328,7 +319,7 @@ function LogoutObstacle ({ onClose }) {
328319

329320
removeLocalWallets()
330321

331-
await signOut({ callbackUrl: '/' })
322+
await signOut({ callbackUrl: '/', redirect: !isCustomDomain })
332323
}}
333324
>
334325
logout

components/territory-header.js

+7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { gql, useMutation } from '@apollo/client'
1212
import { useToast } from './toast'
1313
import ActionDropdown from './action-dropdown'
1414
import { TerritoryTransferDropdownItem } from './territory-transfer'
15+
import { useRouter } from 'next/router'
1516

1617
export function TerritoryDetails ({ sub, children }) {
1718
return (
@@ -77,6 +78,10 @@ export function TerritoryInfo ({ sub }) {
7778
export default function TerritoryHeader ({ sub }) {
7879
const { me } = useMe()
7980
const toaster = useToast()
81+
const router = useRouter()
82+
// TODO: this works but it can be better
83+
const path = router.asPath.split('?')[0]
84+
const isCustomDomain = sub && !path.includes(`/~${sub?.name}`)
8085

8186
const [toggleMuteSub] = useMutation(
8287
gql`
@@ -96,6 +101,8 @@ export default function TerritoryHeader ({ sub }) {
96101

97102
const isMine = Number(sub.userId) === Number(me?.id)
98103

104+
if (isCustomDomain && !isMine) return null
105+
99106
return (
100107
<>
101108
<TerritoryPaymentDue sub={sub} />

lib/url.js

+14
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,20 @@ export function isMisleadingLink (text, href) {
264264
return misleading
265265
}
266266

267+
export function isCustomDomain ({ req, url }) {
268+
const mainDomain = new URL(process.env.NEXT_PUBLIC_URL).hostname
269+
if (typeof window !== 'undefined') {
270+
return window.location.hostname !== mainDomain
271+
}
272+
if (req) {
273+
return req.headers.host !== mainDomain
274+
}
275+
if (url) {
276+
return new URL(url).hostname !== mainDomain
277+
}
278+
return false
279+
}
280+
267281
// eslint-disable-next-line
268282
export const URL_REGEXP = /^((https?|ftp):\/\/)?(www.)?(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i
269283

middleware.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const SN_REFERRER_NONCE = 'sn_referrer_nonce'
1414
const SN_REFEREE_LANDING = 'sn_referee_landing'
1515

1616
const TERRITORY_PATHS = ['/~', '/recent', '/random', '/top', '/post', '/edit']
17-
const NO_REWRITE_PATHS = ['/api', '/_next', '/_error', '/404', '/500', '/offline', '/static', '/signup', '/login', '/logout']
17+
const NO_REWRITE_PATHS = ['/api', '/_next', '/_error', '/404', '/500', '/offline', '/static', '/logout']
1818

1919
// TODO: move this to a separate file
2020
// fetch custom domain mappings from our API, caching it for 5 minutes
@@ -70,6 +70,14 @@ export async function customDomainMiddleware (request, referrerResp) {
7070
console.log('pathname', pathname)
7171
console.log('query', url.searchParams)
7272

73+
if (pathname === '/login' || pathname === '/signup') {
74+
const redirectUrl = new URL(pathname, mainDomain)
75+
redirectUrl.searchParams.set('domain', host)
76+
redirectUrl.searchParams.set('callbackUrl', url.searchParams.get('callbackUrl'))
77+
const redirectResp = NextResponse.redirect(redirectUrl)
78+
return applyReferrerCookies(redirectResp, referrerResp)
79+
}
80+
7381
// if the url contains the territory path, remove it
7482
if (pathname.startsWith(`/~${domainInfo.subName}`)) {
7583
// remove the territory prefix from the path
@@ -80,12 +88,14 @@ export async function customDomainMiddleware (request, referrerResp) {
8088
}
8189

8290
// if coming from main domain, handle auth automatically
83-
if (referer && referer === mainDomain) {
91+
// TODO: uncomment and work on this
92+
93+
/* if (referer && referer === mainDomain) {
8494
const authResp = customDomainAuthMiddleware(request, url)
8595
if (authResp && authResp.status !== 200) {
8696
return applyReferrerCookies(authResp, referrerResp)
8797
}
88-
}
98+
} */
8999

90100
const internalUrl = new URL(url)
91101
// rewrite to the territory path if we're at the root

pages/api/auth/[...nextauth].js

+1-1
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ const getProviders = res => [
276276
}),
277277
CredentialsProvider({
278278
id: 'sync',
279-
name: 'Auth Sync',
279+
name: 'Sync',
280280
credentials: {
281281
token: { label: 'token', type: 'text' }
282282
},

pages/login.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { StaticLayout } from '@/components/layout'
66
import Login from '@/components/login'
77
import { isExternal } from '@/lib/url'
88

9-
export async function getServerSideProps ({ req, res, query: { callbackUrl, multiAuth = false, error = null } }) {
9+
export async function getServerSideProps ({ req, res, query: { callbackUrl, multiAuth = false, error = null, domain } }) {
1010
let session = await getServerSession(req, res, getAuthOptions(req))
1111

1212
// required to prevent infinite redirect loops if we switch to anon
@@ -25,11 +25,17 @@ export async function getServerSideProps ({ req, res, query: { callbackUrl, mult
2525
console.error('error decoding callback:', callbackUrl, err)
2626
}
2727

28-
// TODO: custom domain mapping
2928
if (external) {
3029
callbackUrl = '/'
3130
}
3231

32+
// TODO: custom domain mapping security
33+
if (domain) {
34+
callbackUrl = '/api/auth/sync?redirectUrl=https://' + domain
35+
}
36+
37+
console.log('callbackUrl', callbackUrl)
38+
3339
if (session && callbackUrl && !multiAuth) {
3440
// in the case of auth linking we want to pass the error back to settings
3541
// in the case of multi auth, don't redirect if there is already a session

pages/~/index.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,9 @@ export default function Sub ({ ssrData }) {
2121
if (!data && !ssrData) return <PageLoading />
2222
const { sub } = data || ssrData
2323

24-
const path = router.asPath.split('?')[0]
25-
// TODO: this works but it can be better
26-
const isCustomDomain = sub && !path.includes(`/~${sub?.name}`)
27-
2824
return (
2925
<Layout sub={sub?.name}>
30-
{sub && !isCustomDomain
26+
{sub
3127
? <TerritoryHeader sub={sub} />
3228
: (
3329
<>

0 commit comments

Comments
 (0)