Reputation: 49
I don't understand a bit how to make such a video correctly.
The cookie stores data, and when entering the page, it should be checked for access
/page/post.ts/
import CheckToken from '../../../utils/CheckToken';
export async function getServerSideProps(context) {
CheckToken(context.req, context.res);
return {
props: {
tabledb,setting
}, // will be passed to the page component as props
}
}
export default function post() {
return ()
}
..../../../utils/CheckToken
export default async function handler(req,res) {
try {
...//Data
} catch(err) {
res.redirect(401,"/admin/login" )
}
}
If it fails or if there is an error, I need to redirect to the authorization page But res.redirect doesn't work outside of the api. Tell me then how to make a check then?
Upvotes: 0
Views: 1406
Reputation: 8101
Your CheckToken
function should return some kind of result to indicate if the call was successful or not, then based on that you can do a redirect.
export async function getServerSideProps(context) {
const result = CheckToken(context.req, context.res);
// if "falsy" do the redirect
if (!result) {
return {
redirect: {
destination: '/signin',
permanent: false,
},
}
}
return {
props: {
tabledb,setting
}, // will be passed to the page component as props
}
}
Upvotes: 1