admin管理员组文章数量:1277391
I am trying to follow the next-auth documentation using TypeScript. Using the following code and some code in _app.tsx
from the docs I can protect a page:
AdminDashboard.auth = {
role: "admin",
loading: <AdminLoadingSkeleton />,
unauthorized: "/login-with-different-user", // redirect to this url
}
What is the right way to implement this using TypeScript?
I found a solution which works, but I am not sure if this is the right way:
export type NextPageWithAuth = NextPage & {
auth: boolean,
role: string
}
type NextPageAuthProps = {
Component: NextPageWithAuth,
pageProps: any
}
The AppProps
type is quite more sophisticated than my own NextPageAuthProps
.
I am trying to follow the next-auth documentation using TypeScript. Using the following code and some code in _app.tsx
from the docs I can protect a page:
AdminDashboard.auth = {
role: "admin",
loading: <AdminLoadingSkeleton />,
unauthorized: "/login-with-different-user", // redirect to this url
}
What is the right way to implement this using TypeScript?
I found a solution which works, but I am not sure if this is the right way:
export type NextPageWithAuth = NextPage & {
auth: boolean,
role: string
}
type NextPageAuthProps = {
Component: NextPageWithAuth,
pageProps: any
}
The AppProps
type is quite more sophisticated than my own NextPageAuthProps
.
2 Answers
Reset to default 13In the page, you can extend the built-in NextPage
type to include the auth
field with the appropriate type.
import type { NextPage } from 'next';
type PageAuth = {
role: string
loading: JSX.Element
unauthorized: string
};
export type NextPageWithAuth<P = {}, IP = P> = NextPage<P, IP> & {
auth: PageAuth
};
const AdminDashboard: NextPageWithAuth = () => {
// Your `AdminDashboard` code here
};
AdminDashboard.auth = {
role: "admin",
loading: <AdminLoadingSkeleton />,
unauthorized: "/login-with-different-user"
};
export default AdminDashboard;
Then, in the custom _app
, you can extend AppProps
so that the Component
prop extends the type you declared in the page.
import type { NextComponentType, NextPageContext } from 'next';
import type { NextPageWithAuth } from '<path-to>/AdminDashboard';
type NextComponentWithAuth = NextComponentType<NextPageContext, any, {}> & Partial<NextPageWithAuth>
type ExtendedAppProps<P = {}> = AppProps<P> & {
Component: NextComponentWithAuth
};
function MyApp({ Component, pageProps }: ExtendedAppProps) {
//...
}
The accepted answer's type definitions hurt my head, so I solved the issue by calling getStaticProps
with an auth
property on the pages I wanted to protect:
// bottom of page I want to protect..
export const getStaticProps = async () => {
return {
props: { auth: true },
};
};
and then consuming the prop in _app.tsx like so:
const MyApp: AppType<{ session: Session | null, auth: boolean }> = ({
Component,
pageProps: { session, ...pageProps },
}) => {
const { auth } = pageProps
return (
<SessionProvider session={session} refetchOnWindowFocus={false}>
<NavBar />
{auth ? (
<RouteGuard>
<Component {...pageProps} />
</RouteGuard>
) : (
<Component {...pageProps} />
)}
<ReactQueryDevtools />
<MobileFooter />
</SessionProvider>
);
};
本文标签: javascriptHow to extend NextPage type to add custom field to page componentStack Overflow
版权声明:本文标题:javascript - How to extend NextPage type to add custom field to page component? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741219653a2360722.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论