使用Typescript使用getInitialProps将prop传递到Next.js中的每个页面


10

我遇到一种情况,我需要在页面呈现到服务器端并使用Next.js发送回我之前,先了解用户是否已登录,以避免UI中出现闪烁变化。

我能够弄清楚如果用户已经使用此HOC组件登录,如何防止用户访问某些页面...

export const noAuthenticatedAllowed = (WrappedComponent: NextPage) => {
    const Wrapper = (props: any) => {
        return <WrappedComponent {...props} />;
    };

    Wrapper.getInitialProps = async (ctx: NextPageContext) => {
        let context = {};
        const { AppToken } = nextCookie(ctx);
        if (AppToken) {
            const decodedToken: MetadataObj = jwt_decode(AppToken);
            const isExpired = () => {
                if (decodedToken.exp < Date.now() / 1000) {
                    return true;
                } else {
                    return false;
                }
            };

            if (ctx.req) {
                if (!isExpired()) {
                    ctx.res && ctx.res.writeHead(302, { Location: "/" });
                    ctx.res && ctx.res.end();
                }
            }

            if (!isExpired()) {
                context = { ...ctx };
                Router.push("/");
            }
        }

        const componentProps =
            WrappedComponent.getInitialProps &&
            (await WrappedComponent.getInitialProps(ctx));

        return { ...componentProps, context };
    };

    return Wrapper;
};

这很棒。

现在,我如何构建类似的HOC组件,将其包装起来,比如说“ _app.tsx”,这样我就可以通过获取令牌并确定令牌是否过期并基于它来将“ userAuthenticated”属性传递给每个页面该道具我可以向用户显示适当的UI,而不会产生烦人的闪烁效果?

希望您能为我提供帮助,我尝试按照构建上述HOC的方式进行操作,但是我无法做到这一点,尤其是Typescript不会因其怪异的错误而变得更加轻松:(


Edit == =========================================

我能够创建这样的HOC组件,并userAuthenticated像下面这样将pro传递到每个页面...

export const isAuthenticated = (WrappedComponent: NextPage) => {
    const Wrapper = (props: any) => {
        return <WrappedComponent {...props} />;
    };

    Wrapper.getInitialProps = async (ctx: NextPageContext) => {
        let userAuthenticated = false;

        const { AppToken} = nextCookie(ctx);
        if (AppToken) {
            const decodedToken: MetadataObj = jwt_decode(AppToken);
            const isExpired = () => {
                if (decodedToken.exp < Date.now() / 1000) {
                    return true;
                } else {
                    return false;
                }
            };

            if (ctx.req) {
                if (!isExpired()) {
                    // ctx.res && ctx.res.writeHead(302, { Location: "/" });
                    // ctx.res && ctx.res.end();
                    userAuthenticated = true;
                }
            }

            if (!isExpired()) {
                userAuthenticated = true;
            }
        }

        const componentProps =
            WrappedComponent.getInitialProps &&
            (await WrappedComponent.getInitialProps(ctx));

        return { ...componentProps, userAuthenticated };
    };

    return Wrapper;
};

但是,我必须用此HOC包装每个页面,以便将userAuthenticated属性传递到我拥有的全局布局,因为我不能用它包装“ _app.tsx”类组件,它总是给我一个Error。 ..

这有效...

export default isAuthenticated(Home);
export default isAuthenticated(about);

但这不是...

export default withRedux(configureStore)(isAuthenticated(MyApp));

因此,必须对每个页面执行此操作,然后将属性传递到每个页面的全局布局中,而不是仅在“ _app.tsx”中执行一次,这有点烦人。

我猜原因可能是因为“ _app.tsx”是类组件而不是像其余页面一样的功能组件?我不知道,我只是在猜测。

有什么帮助吗?

Answers:


5

对于那些可能遇到相同问题的人,我可以通过以下方式解决此问题...

import React from "react";
import App from "next/app";
import { Store } from "redux";
import { Provider } from "react-redux";
import withRedux from "next-redux-wrapper";
import { ThemeProvider } from "styled-components";
import GlobalLayout from "../components/layout/GlobalLayout";
import { configureStore } from "../store/configureStore";
import { GlobalStyle } from "../styles/global";
import { ToastifyStyle } from "../styles/toastify";
import nextCookie from "next-cookies";
import jwt_decode from "jwt-decode";

 export interface MetadataObj {
   [key: string]: any;
 }

const theme = {
    color1: "#00CC99",
    color2: "#CC0000"
};

export type ThemeType = typeof theme;

interface Iprops {
    store: Store;
    userAuthenticated: boolean;
}

class MyApp extends App<Iprops> {
    // Only uncomment this method if you have blocking data requirements for
    // every single page in your application. This disables the ability to
    // perform automatic static optimization, causing every page in your app to
    // be server-side rendered.

    static async getInitialProps({ Component, ctx }: any) {
        let userAuthenticated = false;

        const { AppToken } = nextCookie(ctx);
        if (AppToken) {
            const decodedToken: MetadataObj = jwt_decode(AppToken);
            const isExpired = () => {
                if (decodedToken.exp < Date.now() / 1000) {
                    return true;
                } else {
                    return false;
                }
            };

            if (ctx.isServer) {
                if (!isExpired()) {
                    userAuthenticated = true;
                }
            }

            if (!isExpired()) {
                userAuthenticated = true;
            }
        }

        return {
            pageProps: Component.getInitialProps
                ? await Component.getInitialProps(ctx)
                : {},
            userAuthenticated: userAuthenticated
        };
    }

    render() {
        const { Component, pageProps, store, userAuthenticated } = this.props;
        return (
            <Provider store={store}>
                <ThemeProvider theme={theme}>
                    <>
                        <GlobalStyle />
                        <ToastifyStyle />
                        <GlobalLayout userAuthenticated={userAuthenticated}>
                            <Component {...pageProps} />
                        </GlobalLayout>
                    </>
                </ThemeProvider>
            </Provider>
        );
    }
}

export default withRedux(configureStore)(MyApp);

如您所见,我发布了整个_app.tsx组件,以便您可以看到我正在使用的软件包。

我使用next-redux-wrapperstyled-components用打字稿。

我必须将appContextin gitInitialPropsas设置为type any,否则它将不起作用。因此,如果您有更好的type建议,请告诉我。我尝试使用type NextPageContext,但是由于某种原因在这种情况下不起作用。

借助该解决方案,我能够了解用户是否通过了身份验证,并将属性传递给全局布局,这样我就可以在每个页面中使用它,而不必在每个页面中都使用它,以防万一。您不希望每次都必须依赖userAuthenticatedprop 来渲染页眉和页脚,因为现在您只需将页眉和页脚放在GlobalLayout组件中,仍然可以使用userAuthenticatedprop:D


我的评论与您的​​帖子和答案无关。我想说的是,ReactJS遵循函数式编程或OOP,但是我看到了具有OOP思想的代码在后端开发中的心态。从另一个组件继承一个新类!before我以前没看过。
AmerllicA

1
@AmerllicA我明白您的意思,我只是尝试了一切。问题在于SSR,如果我正在使用“创建React App”并进行“客户端渲染”,那么我根本不会这样做,但是我无法使其与SSR一起以其他方式工作,甚至是Next.js推荐的方法。
Ruby
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.