Next.js Discord

Discord Forum

iskeyobject undefined?

Unanswered
American black bear posted this in #help-forum
Open in Discord
Avatar
American black bearOP
moved from #discussions

21 Replies

Avatar
American black bearOP
@Araucanian herring so should i run the getserversession only on the server side?
bc i'm not getting any initialprops
Avatar
Araucanian herring
i guess
Avatar
American black bearOP
i mean this is in a component
i can't just let session be null on the client side
Avatar
Araucanian herring
so user is required to be authorized?
@American black bear
Avatar
American black bearOP
yeah
Avatar
Araucanian herring
where ever the page loads you can do like this

const { data: session } = useSession({ required: true })
so if user is not authed, it will redirect on auth is required
idk how to style that specific page because its made by next-auth
Avatar
American black bearOP
alr cool
gonna try that out
Avatar
Araucanian herring
here is
@American black bear which next version are you using?
if you are using pages you can directly check in server-side is user authed or not

import { getSession } from 'next-auth/react'

export const getServerSideProps: GetServerSideProps = async ({
    locale,
    req,
}) => {
    const session = await getSession({ req })
    if (!session) {
        return {
            redirect: {
                destination: '/', // this is where to redirect
                permanent: false,
            },
            props: {},
        }
    }

    return {
        props: { }
    }
}
Avatar
American black bearOP
13.5.2
app router
so yeah
Avatar
Araucanian herring
i think its still possible to do this because root file of route is still serverside