type checking error??

import { GetServerSidePropsContext } from "next";
import { getServerAuthSession } from "../server/auth";
import { Session } from "next-auth";

type Callback = (data: { session: Session }) => void;

type RoleRedirects = {
[key: string]: {
[key: string]: string;
};
};

const roleRedirects: RoleRedirects = {
cashier: {
ADMIN: "/admin",
MANAGER: "/manager",
},
manager: {
ADMIN: "/admin",
CASHIER: "/cashier",
},
admin: {
MANAGER: "/manager",
CASHIER: "/cashier",
},
};

export async function roleGuard(
ctx: GetServerSidePropsContext,
cb: Callback,
role: string
) {
try {
const session = await getServerAuthSession(ctx);
if (!session) {
return {
redirect: {
destination: "/",
permanent: false,
},
};
}
const redirectDestination = roleRedirects[role][session.user?.role as string];
if (redirectDestination) {
return {
redirect: {
destination: redirectDestination,
permanent: false,
},
};
}

return cb({ session });
} catch (error) {
return {
redirect: {
destination: "/",
permanent: false,
},
};
}
}
import { GetServerSidePropsContext } from "next";
import { getServerAuthSession } from "../server/auth";
import { Session } from "next-auth";

type Callback = (data: { session: Session }) => void;

type RoleRedirects = {
[key: string]: {
[key: string]: string;
};
};

const roleRedirects: RoleRedirects = {
cashier: {
ADMIN: "/admin",
MANAGER: "/manager",
},
manager: {
ADMIN: "/admin",
CASHIER: "/cashier",
},
admin: {
MANAGER: "/manager",
CASHIER: "/cashier",
},
};

export async function roleGuard(
ctx: GetServerSidePropsContext,
cb: Callback,
role: string
) {
try {
const session = await getServerAuthSession(ctx);
if (!session) {
return {
redirect: {
destination: "/",
permanent: false,
},
};
}
const redirectDestination = roleRedirects[role][session.user?.role as string];
if (redirectDestination) {
return {
redirect: {
destination: redirectDestination,
permanent: false,
},
};
}

return cb({ session });
} catch (error) {
return {
redirect: {
destination: "/",
permanent: false,
},
};
}
}
const redirectDestination = roleRedirects[role][session.user?.role as string];
const redirectDestination = roleRedirects[role][session.user?.role as string];
this line show error any idea why? Thanks in advance
4 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
cje
cje2y ago
could also just type the function args more precisely
const role = {
cashier: {foo: "bar"},
manager: {biz: "baz"},
admin: {hello: "world"},
} as const;

type RoleType = keyof typeof role;

function doThingWithRole(roleKey: RoleType) {
const foo = role[roleKey];
}
const role = {
cashier: {foo: "bar"},
manager: {biz: "baz"},
admin: {hello: "world"},
} as const;

type RoleType = keyof typeof role;

function doThingWithRole(roleKey: RoleType) {
const foo = role[roleKey];
}
arete
areteOP2y ago
im trying to use this but it show different error
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server