title: Next.js Handling User Logged In Status
body: <p>I'm using next-iron-session in my next.js project with discord oauth2.</p>
  <p>My Session Code:</p>
  <pre><code>import {
      GetServerSidePropsContext,
      NextApiRequest,
      NextApiResponse,
  } from &quot;next&quot;;
  import { withIronSession, Session } from &quot;next-iron-session&quot;;
  
  
  
  export type NextIronRequest = NextApiRequest &amp; { session: Session };
  export type NextIronHandler = (
      req: NextIronRequest,
      res: NextApiResponse
  ) =&gt; void | Promise&lt;void&gt;;
  
  export type NextRoute = (
      ctx: GetServerSidePropsContext &amp; { req: { session: Session } },
      redirect: string
  ) =&gt; any;
  
  export function withSession(handler: NextIronHandler | NextRoute) {
      return withIronSession(handler, {
          password: process.env.COOKIE_SECRET as string,
          cookieName: &quot;session&quot;,
          ttl: 15 * 24 * 3600,
          cookieOptions: {
              secure: process.env.NODE_ENV === &quot;production&quot;,
              sameSite: &quot;strict&quot;,
              httpOnly: true,
          },
      });
  }
  </code></pre>
  <p>Auth Handler Code:</p>
  <pre><code>import next, { GetServerSidePropsContext } from 'next'
  import { withSession } from './session'
  
  export const withAuth = (gssp: any) =&gt; {
      return async (context: GetServerSidePropsContext) =&gt; {
          const { req } = context
          const user = req.session.user
  
          if (!user) {
              return {
                  redirect: {
                      destination: '/api/auth/login',
                      statusCode: 302,
                  },
              }
          } 
  
          return await gssp(context)
      }
  }
  
  export const withAuthSsr = (handler: any) =&gt; withSession(withAuth(handler))
  </code></pre>
  <p>Dashboard Code:</p>
  <pre><code>import { GetServerSideProps, NextApiRequest } from &quot;next&quot;;
  import Head from &quot;next/head&quot;;
  import Image from &quot;next/image&quot;;
  import Link from &quot;next/link&quot;;
  import { NextIronRequest } from &quot;../util/session&quot;;
  import { User, UserData } from &quot;src/types&quot;;
  import { developerRoute } from &quot;src/util/redirects&quot;;
  import { withSession } from &quot;src/util/session&quot;;
  import Router from &quot;next/router&quot;;
  import { redirect } from &quot;next/dist/server/api-utils&quot;;
  import { withAuthSsr } from '../util/AuthHandler'
  
  interface Props {
    user?: User;
  }
  
  
  export default function Dashboard(req: NextIronRequest) {
    function Icon(icon: string) {
      console.log(icon)
    }
    return (
      &lt;&gt;
        &lt;Head&gt;
          &lt;title&gt;{req.user.user.username} | Dashboard&lt;/title&gt;
          &lt;link rel=&quot;shortcut icon&quot; href={req.user.avatar} /&gt;
        &lt;/Head&gt;
        &lt;body&gt;
          {
            req.user.guilds.map((guild: any) =&gt; {
              return (
                &lt;&gt;
                  &lt;div className=&quot;p-10 items-center justify-center flex flex-center grid grid-cols-2 grid-flow-row sm:grid-cols-1 md:grid-cols-3 lg:grid-cols-3 xl:grid-cols-3 gap-5&quot;&gt;
                    &lt;div className=&quot;rounded overflow-hidden shadow-lg&quot;&gt;
                      &lt;img className=&quot;w-full&quot; src={Icon(guild.icon)} alt=&quot;Guild Icon&quot; /&gt;
                      &lt;div className=&quot;px-6 py-4&quot;&gt;
                        &lt;div className=&quot;font-bold text-center text-xl mb-2&quot;&gt;{guild.name}&lt;/div&gt;
                      &lt;/div&gt;
                      &lt;div className=&quot;px-6 pt-4 pb-2 items-center content-center justify-center&quot;&gt;
                        &lt;a href=&quot;#_&quot; className=&quot;relative inline-flex items-center justify-center p-4 px-6 py-3 overflow-hidden font-medium text-indigo-600 transition duration-300 ease-out border-2 border-purple-500 rounded-full shadow-md group&quot;&gt;
                          &lt;span className=&quot;absolute inset-0 flex items-center justify-center w-full h-full text-white duration-300 -translate-x-full bg-purple-500 group-hover:translate-x-0 ease&quot;&gt;
                            &lt;svg className=&quot;w-6 h-6&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; viewBox=&quot;0 0 24 24&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot;&gt;&lt;path strokeLinecap=&quot;round&quot; strokeLinejoin=&quot;round&quot; strokeWidth=&quot;2&quot; d=&quot;M14 5l7 7m0 0l-7 7m7-7H3&quot;&gt;&lt;/path&gt;&lt;/svg&gt;
                          &lt;/span&gt;
                          &lt;span className=&quot;absolute flex items-center justify-center w-full h-full text-purple-500 transition-all duration-300 transform group-hover:translate-x-full ease&quot;&gt;Manage Settings&lt;/span&gt;
                          &lt;span className=&quot;relative invisible&quot;&gt;Manage Settings&lt;/span&gt;
                        &lt;/a&gt;
                      &lt;/div&gt;
                    &lt;/div&gt;
                  &lt;/div&gt;
                &lt;/&gt;
              )
            })
          }
        &lt;/body&gt;
      &lt;/&gt;
    )
  }
  
  export const getServerSideProps: GetServerSideProps = withSession(withAuthSsr(developerRoute))
  </code></pre>
  <p>What's happening now is, if the user is even loggedIn it will redirect for login again, How do i fix that?</p>
  <p>What did i do wrong? If the user isn't logged he/she gets redirected.</p>
  <p>Next.js Version: 12.0.7
  Next-Iron-Session Version: 4.2.0</p>