Next.js Discord

Discord Forum

Passage - refactor from /pages/ to /app/ - two files

Unanswered
Asiatic Lion posted this in #help-forum
Open in Discord
Asiatic LionOP
Question: how to refactor this to use the /app/ folder. As server components dont have "req, or res"
// Two files: index.js and utils/passage.ts

// index.js (page file)
import styles from '@/styles/Home.module.css'
import PassageLogin from '@/components/login'
import { getAuthenticatedUserFromSession } from '@/utils/passage'
import { useEffect } from 'react'
import Router from 'next/router';

export default function Home({isAuthorized}) {
  useEffect(()=> {
    if(isAuthorized){
      Router.push('/dashboard')
    }
  })
  
    return(
      <div className={styles.main}>
        <PassageLogin />
      </div>
    ) 
}

export const getServerSideProps = async (context) => {
  const loginProps = await getAuthenticatedUserFromSession(context.req, context.res)
    return {
      props: {
        isAuthorized: loginProps.isAuthorized?? false,
        userID: loginProps.userID?? ''
      },
    }
}

// utils/passage.js
import Passage from '@passageidentity/passage-node';

const passage = new Passage({
    appID: process.env.NEXT_PUBLIC_PASSAGE_APP_ID,
    apiKey: process.env.PASSAGE_API_KEY,
});

export const getAuthenticatedUserFromSession = async (req, res) => {
    try {
        const userID = await passage.authenticateRequest(req);
        if (userID) {
          return {isAuthorized: true, userID: userID};
        }
      } catch (error) {
        // authentication failed
        return {isAuthorized: false, userID: ''};
      }
  }

0 Replies