-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathaccept.tsx
More file actions
95 lines (76 loc) · 3.1 KB
/
Copy pathaccept.tsx
File metadata and controls
95 lines (76 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
'use client'
import { useEffect, useRef, useState } from 'react'
import { useSearchParams, useRouter } from 'next/navigation'
import { useSession } from 'next-auth/react'
import { Logo } from '@repo/ui/logo'
import { useAcceptOrganizationInvite } from '../../../lib/user'
import Link from 'next/link'
import { Button } from '@repo/ui/button'
export const InviteAccepter = () => {
const searchParams = useSearchParams()
const token = searchParams?.get('token')
const { data: session, update, status } = useSession()
const { push } = useRouter()
const hasUpdatedRef = useRef(false)
const [enabled, setEnabled] = useState(false)
const { isLoading, verified } = useAcceptOrganizationInvite(token ?? null, enabled)
useEffect(() => {
if (status === 'authenticated' && token) {
setEnabled(true)
return
}
if (status !== 'unauthenticated') {
return
}
const email = searchParams?.get('email')
// route on the invite link's account hint: new users to signup, existing users to login
const destination = searchParams?.get('new') === 'true' ? 'signup' : 'login'
const params = new URLSearchParams()
if (token) params.set('token', token)
if (email) params.set('email', email)
const query = params.toString()
push(`/${destination}${query ? `?${query}` : ''}`)
}, [status, token, push, searchParams])
useEffect(() => {
if (hasUpdatedRef.current || !verified?.success || !session) return
hasUpdatedRef.current = true
const expires = new Date(Date.now() + 5 * 60 * 1000).toUTCString()
document.cookie = `direct_oauth=true; path=/; expires=${expires}; SameSite=Lax`
update({
...session,
user: {
...session.user,
accessToken: verified?.access_token,
refreshToken: verified?.refresh_token,
organization: verified?.joined_org_id,
isOnboarding: false,
},
}).then(() => {
let redirectURL = '/'
if (verified?.needs_sso) {
redirectURL = `/login/sso/enforce?email=${session.user?.email}&organization_id=${verified.joined_org_id}`
}
window.location.href = redirectURL
})
}, [verified, session, update])
const showError = verified && !verified.success
return (
<main className="flex flex-col min-h-screen w-full items-center justify-center dark:bg-dk-surface-0 bg-surface-0">
<div className="flex flex-col justify-center mx-auto w-full p-6 sm:w-1/3 h-full relative">
<div className="mx-auto mb-6">
<Logo width={200} />
</div>
{isLoading && <h1 className="text-3xl text-center mt-4 animate-pulse">Accepting invite...</h1>}
{showError && (
<div className="text-center mt-4">
<h2 className="text-xl font-semibold text-red-600">Invite not found</h2>
<p className="mt-2 text-sm">Looks like that invite link’s invalid or expired. Reach out to the person who sent it and ask them to send you a new one.</p>
<Link href="/dashboard" className="mt-4 block">
<Button>Go to Dashboard</Button>
</Link>
</div>
)}
</div>
</main>
)
}