@frontegg/react

中文文档
7.20.2发布于 2026/11/11MITTypeScriptCommonJS近 30 天下载 72.1万


Frontegg Logo

原始使用文档
文档目录5 节
  1. Frontegg React.js
  2. Installation
  3. Configuration
  4. Redirect to login
  5. Integrate Admin Portal

Frontegg Logo

Frontegg React.js

Frontegg is a web platform where SaaS companies can set up their fully managed, scalable and brand aware - SaaS features and integrate them into their SaaS portals in up to 5 lines of code.

Installation

Use the package manager npm to install frontegg React.JS library.

npm install @frontegg/react react-router-dom

Configuration

Wrap your root component with Frontegg Provider:

import React from 'react';
import ReactDOM from 'react-dom'; // For react 17
// For react 18: import ReactDOM from 'react-dom/client';
import App from './App';
import './index.css';

import { FronteggProvider } from '@frontegg/react';

const contextOptions = {
  baseUrl: 'https://[YOUR_SUBDOMAIN].frontegg.com',
  clientId: '[YOUR-CLIENT-ID]'
};

// For react 18: 
// const root = ReactDOM.createRoot(document.getElementById('root'));
// root.render(
ReactDOM.render(
    <FronteggProvider contextOptions={contextOptions} hostedLoginBox={true}>
        <App />
    </FronteggProvider>,
    document.getElementById('root')
);

In order to get your subDomain and clientId, visit our portal.

Redirect to login

Using the Frontegg useAuth hook, you can determine whether a user is authenticated or not. If the user is not authenticated, you can redirect the user to login via the useLoginWithRedirect hook as shown below.

import './App.css';
// import { useEffect } from 'react';
import { ContextHolder } from '@frontegg/rest-api';
import { useAuth, useLoginWithRedirect } from '@frontegg/react';

function App() {
  const { user, isAuthenticated } = useAuth();
  const loginWithRedirect = useLoginWithRedirect();

  // Uncomment this to redirect to login automatically
  // useEffect(() => {
  //   if (!isAuthenticated) {
  //     loginWithRedirect();
  //   }
  // }, [isAuthenticated, loginWithRedirect]);

  const logout = () => {
    const baseUrl = ContextHolder.getContext().baseUrl;
    window.location.href = `${baseUrl}/oauth/logout?post_logout_redirect_uri=${window.location}`;
  };

  return (
    <div className='App'>
      {isAuthenticated ? (
        <div>
          <div>
            <img src={user?.profilePictureUrl} alt={user?.name} />
          </div>
          <div>
            <span>Logged in as: {user?.name}</span>
          </div>
          <div>
            <button onClick={() => alert(user.accessToken)}>What is my access token?</button>
          </div>
          <div>
            <button onClick={() => logout()}>Click to logout</button>
          </div>
        </div>
      ) : (
        <div>
          <button onClick={() => loginWithRedirect()}>Click me to login</button>
        </div>
      )}
    </div>
  );
}

export default App;

Integrate Admin Portal

Opening the Admin Portal is available via the following code snippet.

import { AdminPortal } from '@frontegg/react'

const handleClick = () => {
  AdminPortal.show();
};

<button onClick={handleClick}>Settings</button>

版本与发布记录

@frontegg/react 共发布 1,590 个版本,首次发布于 2019/09/02;过去 12 个月发布了 127 个版本。

版本发布日期距今
7.20.2latest2026/11/114 天前
7.20.2-alpha.43004088372next2026/11/114 天前
7.20.12026/10/2917 天前
7.20.1-alpha.408829619582026/10/2917 天前
7.20.1-alpha.40841505976alpha2026/10/2917 天前
7.20.02026/10/2521 天前

其他发布通道:beta → 3.0.11-vitejs,vitejs → 3.0.12-vitejs,alpha → 7.20.1-alpha.40841505976,next → 7.20.2-alpha.43004088372。

关于 @frontegg/react 的常见问题

如何安装 @frontegg/react?

在项目目录运行 npm install @frontegg/react,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>16.9.0)、react-dom(>16.9.0)、react-router-dom(>5.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@frontegg/react 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@frontegg/react 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@frontegg/react 可以商用吗?

@frontegg/react 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@frontegg/react 的下载量有多少?

根据 npm 官方下载统计,@frontegg/react 在 2026/10/16 – 2026/11/14 这 30 天内被下载 721,288 次,日均约 24,043 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@frontegg/react 还在维护吗?

最新版本 7.20.2 发布于 2026/11/11(4 天前);至今共发布 1590 个版本,其中过去 12 个月发布了 127 个;首次发布于 2019/09/02。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。