iskeyobject undefined?
Unanswered
American black bear posted this in #help-forum
American black bearOP
moved from #discussions
21 Replies
American black bearOP
@Araucanian herring so should i run the getserversession only on the server side?
bc i'm not getting any initialprops
@American black bear <@484717395722895360> so should i run the getserversession only on the server side?
Araucanian herring
i guess
American black bearOP
i mean this is in a component
i can't just let
session
be null on the client sideAraucanian herring
so user is required to be authorized?
@American black bear
American black bearOP
yeah
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
American black bearOP
alr cool
gonna try that out
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 notimport { 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: { }
}
}
@Araucanian herring <@441672776345190421> which next version are you using?
American black bearOP
13.5.2
app router
so yeah
@Araucanian herring if you are using `pages` you can directly check in server-side is user authed or not
ts
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: { }
}
}
Araucanian herring
i think its still possible to do this because root file of route is still serverside