如何使用firebase auth限制对next.js中页面的访问? [英] How to restrict access to pages in next.js using firebase auth?

查看:23
本文介绍了如何使用firebase auth限制对next.js中页面的访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用 firebase 的 next.js 应用程序.我需要使用 firebase auth 包来限制对页面的访问.with-firebase-authentication 示例不显示多个页面的身份验证.

I am working on a next.js app which uses firebase. I need to use firebase auth package to restrict access to pages. The with-firebase-authentication example doesn't show authentication for multiple pages.

import React from 'react';
import Router from 'next/router';

import { firebase } from '../../firebase';
import * as routes from '../../constants/routes';

const withAuthorization = (needsAuthorization) => (Component) => {
  class WithAuthorization extends React.Component {
    componentDidMount() {
      firebase.auth.onAuthStateChanged(authUser => {
        if (!authUser && needsAuthorization) {
          Router.push(routes.SIGN_IN)
        }
      });
    }

    render() {
      return (
        <Component { ...this.props } />
      );
    }
  }

  return WithAuthorization;
}

export default withAuthorization;

推荐答案

也在努力集成 firebase auth,最终使用 nextjs 上的 with-iron-session 示例中详述的方法:https://github.com/hajola/with-firebase-auth-iron-session

Struggled with integrating firebase auth as well, ended up using the approach detailed in the with-iron-session example on nextjs: https://github.com/hajola/with-firebase-auth-iron-session

这篇关于如何使用firebase auth限制对next.js中页面的访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆