All files / app/(dynamic) layout.tsx

100% Statements 12/12
100% Branches 2/2
100% Functions 4/4
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                    108x                     76x 108x   108x 74x 74x 24x 24x     50x 49x   74x     108x    
'use client'
import { useEffect } from 'react'
import { Provider } from 'react-redux'
import { hasSession } from '../../services/auth/session'
import { login, logout, refreshToken } from '../../store/auth/auth'
import { useAppDispatch } from '../../store/hooks'
import { store } from '../../store/store'
import { SessionData } from '../../types/auth'
 
export default function RootLayout({ children }: { children: React.ReactNode }) {
	return (
		<Provider store={store}>
			<Auth>{children}</Auth>
		</Provider>
	)
}
 
type AuthProps = {
	children: React.ReactNode
}
 
const Auth: React.FC<AuthProps> = ({ children }) => {
	const dispatch = useAppDispatch()
 
	useEffect(() => {
		const checkAuthenticated = async () => {
			if (!hasSession()) {
				dispatch(logout())
				return null
			}
 
			const sessionPayload = await dispatch(refreshToken({ bypassExpiryCheck: true }))
			dispatch(login(sessionPayload.payload as SessionData))
		}
		checkAuthenticated()
	}, []) // eslint-disable-line react-hooks/exhaustive-deps
 
	return <>{children}</>
}