Redirect if user logged in quickly
Unanswered
Saltwater Crocodile posted this in #help-forum
Saltwater CrocodileOP
Hi, Im tryng to implement authentication in my app. Once a user is logged in if he goes to the sigin route I want to redirect to hs profile page. I've done that using useSession to check if there is a session and thn redirect. The problem is that it takes too much time and I wonder if there's a more efficent way to do this check and redirect. This is the code:
'use client'
const page: FC = () => {
const router = useRouter()
const { data: session, status } = useSession()
if(status === 'authenticated'){
router.push('/profile')
}1 Reply
Saltwater CrocodileOP
Im trying to solve it with getServerSideProps but not working either. It doing nothing:
export async function getServerSideProps(context: GetServerSidePropsContext) {
console.log('server')
const session = await getAuthSession()
// If the user is already logged in, redirect.
// Note: Make sure not to redirect to the same page
// To avoid an infinite loop!
if (session) {
return {
redirect: {
destination: "/profile",
statusCode: 302,
}
}
}
return {
props: { session: session },
}
}