2025-01-31 14:47:08 -05:00
|
|
|
import { useEffect } from 'react'
|
|
|
|
import { useLocation, useNavigate } from 'react-router-dom'
|
|
|
|
|
2025-04-01 23:54:26 -07:00
|
|
|
import { PATHS } from '@src/lib/paths'
|
|
|
|
import { useAuthState } from '@src/machines/appMachine'
|
|
|
|
|
2025-01-31 14:47:08 -05:00
|
|
|
/**
|
|
|
|
* A simple hook that listens to the auth state of the app and navigates
|
|
|
|
* accordingly.
|
|
|
|
*/
|
|
|
|
export function useAuthNavigation() {
|
|
|
|
const navigate = useNavigate()
|
|
|
|
const location = useLocation()
|
|
|
|
const authState = useAuthState()
|
|
|
|
|
|
|
|
// Subscribe to the auth state of the app and navigate accordingly.
|
|
|
|
useEffect(() => {
|
2025-04-21 10:05:13 -04:00
|
|
|
console.log('authState', authState.value)
|
2025-01-31 14:47:08 -05:00
|
|
|
if (
|
|
|
|
authState.matches('loggedIn') &&
|
|
|
|
location.pathname.includes(PATHS.SIGN_IN)
|
|
|
|
) {
|
|
|
|
navigate(PATHS.INDEX)
|
|
|
|
} else if (
|
|
|
|
authState.matches('loggedOut') &&
|
|
|
|
!location.pathname.includes(PATHS.SIGN_IN)
|
|
|
|
) {
|
|
|
|
navigate(PATHS.SIGN_IN)
|
|
|
|
}
|
2025-04-21 10:05:13 -04:00
|
|
|
}, [authState, location.pathname])
|
2025-01-31 14:47:08 -05:00
|
|
|
}
|