Frontegg React.js (英语)
Frontegg是一个网络平台,SaaS公司可以在此设置其完全管理,可伸缩和品牌意识的-SaaS功能,并整合到其SaaS门户网站中,最多有5行代码.
安装
使用软件包管理器 无 用于安装foregg React.JS库.
npm install @frontegg/react react-router-dom
配置
用 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')
);
为了获得你的子域和客户端ID,访问我们的门户.
重定向到登录
使用 Frontegg 使用 Auth 钩,您可以确定用户是否被认证 . 如果用户没有被认证,您可以通过LoginWithRedict hook通过如下所示的使用重新引导用户登录.
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;
整合行政门户
打开行政门户可通过以下代码片段获取.
import { AdminPortal } from '@frontegg/react'
const handleClick = () => {
AdminPortal.show();
};
<button onClick={handleClick}>Settings</button>