feat(auth): add callback, silent renew and logout routes
This commit is contained in:
18
auth/RequireAuth.tsx
Normal file
18
auth/RequireAuth.tsx
Normal file
@ -0,0 +1,18 @@
|
||||
import { PropsWithChildren, useEffect } from 'react'
|
||||
import { useLocation } from 'react-router-dom'
|
||||
import { useAuth } from './AuthProvider'
|
||||
|
||||
|
||||
export function RequireAuth({ children }: PropsWithChildren) {
|
||||
const { isAuthenticated, signIn } = useAuth()
|
||||
const loc = useLocation()
|
||||
|
||||
|
||||
useEffect(() => {
|
||||
if (!isAuthenticated) void signIn(loc.pathname + loc.search)
|
||||
}, [isAuthenticated, signIn, loc])
|
||||
|
||||
|
||||
if (!isAuthenticated) return null
|
||||
return <>{children}</>
|
||||
}
|
||||
15
src/pages/AuthCallbackPage.tsx
Normal file
15
src/pages/AuthCallbackPage.tsx
Normal file
@ -0,0 +1,15 @@
|
||||
import { useEffect } from 'react'
|
||||
import { useNavigate } from 'react-router-dom'
|
||||
import { userManager } from 'auth/oidc'
|
||||
|
||||
|
||||
export default function AuthCallbackPage() {
|
||||
const navigate = useNavigate()
|
||||
useEffect(() => {
|
||||
userManager.signinRedirectCallback().then((res) => {
|
||||
const target = (res?.state as any)?.returnTo || '/'
|
||||
navigate(target, { replace: true })
|
||||
})
|
||||
}, [navigate])
|
||||
return <p>Completing sign-in…</p>
|
||||
}
|
||||
7
src/pages/LogoutPage.tsx
Normal file
7
src/pages/LogoutPage.tsx
Normal file
@ -0,0 +1,7 @@
|
||||
import { useEffect } from 'react'
|
||||
import { useAuth } from 'auth/AuthProvider'
|
||||
export default function LogoutPage() {
|
||||
const { signOut } = useAuth()
|
||||
useEffect(() => { void signOut() }, [signOut])
|
||||
return <p>Signing out…</p>
|
||||
}
|
||||
8
src/pages/SilentRenewPage.tsx
Normal file
8
src/pages/SilentRenewPage.tsx
Normal file
@ -0,0 +1,8 @@
|
||||
import { useEffect } from 'react'
|
||||
import { userManager } from 'auth/oidc'
|
||||
|
||||
|
||||
export default function SilentRenewPage() {
|
||||
useEffect(() => { userManager.signinSilentCallback() }, [])
|
||||
return <p>Silent renew…</p>
|
||||
}
|
||||
Reference in New Issue
Block a user