首页 > 解决方案 > 如何使用firebase auth限制对next.js中页面的访问?

问题描述

我正在开发一个使用 firebase 的 next.js 应用程序。我需要使用 firebase auth 包来限制对页面的访问。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;

标签: firebasefirebase-authenticationnext.js

解决方案


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


推荐阅读