How to protect route using jwt tokens
Unanswered
Tan posted this in #help-forum
TanOP
I have a function that validates token. I have defined that in funcitn
and my admin auth funcitn is
But I am getting error like this
is there better way to do this .
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
@Tan I have a function that validates token. I have defined that in funcitn
and my admin auth funcitn is
js
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 .
you can't call this function in client component because it contains server code
if your token is from localStorage, then you would need to check the token in client side with route handler
@Ray if your token is from localStorage, then you would need to check the token in client side with route handler
TanOP
by creating api routes ?
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ðŸ‘ðŸ¾