如何使用Firebase身份验证来限制对next.js页面的访问?

火力 React.js

Mandy

2020-03-27

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

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;

第3811篇《如何使用Firebase身份验证来限制对next.js页面的访问?》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

1个回答
Stafan 2020.03.27

嗨经过一番研究在这里似乎有这样做的方法有两种。您可以使用“ 自定义”替换页面的初始化过程,以在其中包括身份验证-在这种情况下,您可以将身份验证状态作为prop传输到下一页-或针对每次加载的页面要求一个新的身份验证状态。

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android