Next.js Discord

Discord Forum

How to protect route using jwt tokens

Unanswered
Tan posted this in #help-forum
Open in Discord
TanOP
I have a function that validates token. I have defined that in funcitn
and my admin auth funcitn is
export const adminAuth = async (token: string) => {
  await connectToDatabase();
  if (!token) {
    return {
      success: false,
      message: "Not Authorized, token not available for the admin",
    };
  }

  jwt.verify(token, jwtSecret, (err) => {
    if (err) {
      console.log(err);
      return {
        message: "Token not Authorized",
        success: false,
      };
    } else {
      return {
        success: true,
        message: "Authorized",
      };
    }
  });
};


But I am getting error like this
is there better way to do this .

10 Replies

yes
or store the token in cookie and check it on server side
@Ray or store the token in cookie and check it on server side
TanOP
this will also be a better option
then you can call the adminAuth function in server component
@Ray then you can call the `adminAuth` function in server component
TanOP
Thank you I will do that
please mark solution if your issue has been solved👍🏾