https://www.ory.sh/ logo
d

damp-secretary-66941

01/12/2022, 2:35 PM
Hello, I have query regarding Next.js and Ory Integration, I can see session can be set based
kratos
but this same is available on server side?
m

magnificent-energy-493

01/12/2022, 3:15 PM
Hello Dhruv, I am not quite sure what you are trying to do. Do you want to check if the user is logged in? You can do a whoami call for that. Let me know if that was where you were having trouble.
d

damp-secretary-66941

01/12/2022, 4:53 PM
Sorry for the confusing question, but basically I am trying to setup API routes in next, so the route will return the data, if user is logged in else will return 401:
Copy code
import ory from '../../pkg/sdk'

export default async function handler(req, res) {
  if (req.method !== 'GET') {
    return res.status(405).send('Method Not Allowed')
  } else {
    ory
      .toSession()
      .then(({ data }) => {
        console.log('Has User', JSON.stringify(data, null, 2))
      })
      .catch((err) => {
        console.log('Has No User', err)
      })
    return res.status(200).send({ ok: true, message: 'Testing' })
  }
}
Ref: using the same code base as this: https://github.com/ory/kratos-selfservice-ui-react-nextjs
4 Views