【问题标题】:redux-next-wrapper with supabase v2 ssr带有 supabase v2 ssr 的 redux-next-wrapper
【发布时间】:2022-10-24 06:25:46
【问题描述】:

在supabase V2 之前,我得到了我的数据并使用 redux 存储,如下所示:

supabase 不幸的是,V2 破坏了 redux-next-wrapper 的一些功能

export const getServerSideProps = wrapper.getServerSideProps(
  (store) => async ({ req }) => {
    const { user } = await supabase.auth.api.getUserByCookie(req);
    if (user === null) {
      return {
        redirect: {
          permanent: false,
          destination: "/auth",
        },
        props: {},
      };
    }
    if (user) {
      async function getData() {
        let { data, error, status } = await supabase
          .from("table")
          .select(`id`)
          .eq("id", user.id);
        store.dispatch(writeUserData(data));
        return data;
      }

      return {
        props: {
          data: await getData(),
        },
      };
    }
  }
);

任何想法如何使用withPageAuth() 实现相同的功能?

export const getServerSideProps = withPageAuth({
  redirectTo: '/foo',
  async getServerSideProps (ctx, supabase) {
    // Access the user object
    const {
      data: { user }
    } = await supabase.auth.getUser()

    return { props: { id: user?.id } }
  }
})

【问题讨论】:

    标签: reactjs redux next.js supabase


    【解决方案1】:

    这与您已经拥有的类似,只是重定向已经由 withPageAuth 处理。

    export const getServerSideProps = withPageAuth({
      redirectTo: '/auth',
      async getServerSideProps (ctx, supabase) {
        // Access the user object
        const {
          data: { user }
        } = await supabase.auth.getUser()
    
        let { data, error, status } = await supabase
          .from("table")
          .select(`id`)
          .eq("id", user.id);
        store.dispatch(writeUserData(data));
    
        return { props: { data } }
      }
    })
    

    【讨论】:

      猜你喜欢
      • 2022-01-26
      • 1970-01-01
      • 2022-01-22
      • 2021-11-18
      • 2020-10-01
      • 2021-11-06
      • 1970-01-01
      • 2021-01-16
      • 2021-09-01
      相关资源
      最近更新 更多