• In our NextJS/React application we are using AWS amplify and AWS cognito for auth.
• In our NextJS "getServerSideProps" we will authenticate the user on certain pages using this "protectedPageRoute" function.
import { withSSRContext } from "aws-amplify";
export default async function protectedPageRoute({
context,
redirectTo,
getProps
}: Props) {
const { Auth } = withSSRContext(context);
try {
const user = await Auth.currentAuthenticatedUser();
const userJwtToken = (await Auth.currentSession()).getAccessToken().getJwtToken()
// rest of code
} catch (error) {
// catch error code
}
}
Issue:
• When the user first loads the page after their auth token has expired, the re-authentication takes a long time to succeed.
Questions:
• Is there a better way to handle authentication on our NextJS server side?
• Is there anyway to speed up the re-authentication process?