From fee5879e1148bf356b8545701ab8b1fbb5421227 Mon Sep 17 00:00:00 2001 From: TechQuery Date: Sun, 26 May 2024 22:16:15 +0800 Subject: [PATCH] [add] Authing Guard component [refactor] simplify Session Box component --- src/component/AuthingGuard.tsx | 35 ++++++++++++++ src/component/SessionBox.tsx | 84 +++++++--------------------------- src/page/index.tsx | 4 +- 3 files changed, 54 insertions(+), 69 deletions(-) create mode 100644 src/component/AuthingGuard.tsx diff --git a/src/component/AuthingGuard.tsx b/src/component/AuthingGuard.tsx new file mode 100644 index 0000000..7c4dea6 --- /dev/null +++ b/src/component/AuthingGuard.tsx @@ -0,0 +1,35 @@ +import { Guard } from '@authing/guard'; +import { Component, HTMLAttributes } from 'react'; + +export const guard = new Guard({ + appId: process.env.AUTHING_APP_ID! +}); + +export interface AuthingGuardProps extends HTMLAttributes { + onSignIn: (user: object) => any; + onError?: (message: any) => any; +} + +export class AuthingGuard extends Component { + init = async (box: HTMLDivElement | null) => { + if (!box) return; + + const { onSignIn, onError } = this.props; + + if (typeof onError === 'function') guard.on('login-error', onError); + + const user = await guard.start(box); + + onSignIn(user); + }; + + componentWillUnmount() { + guard.unmount(); + } + + render() { + const { onSignIn, onError, ...props } = this.props; + + return
; + } +} diff --git a/src/component/SessionBox.tsx b/src/component/SessionBox.tsx index 5134a3d..c6f476f 100644 --- a/src/component/SessionBox.tsx +++ b/src/component/SessionBox.tsx @@ -1,74 +1,24 @@ -import { Guard } from '@authing/guard'; import { observer } from 'mobx-react'; -import { Component, MouseEvent, PropsWithChildren } from 'react'; +import { FC } from 'react'; import userStore from '../model/User'; +import { AuthingGuard, AuthingGuardProps } from './AuthingGuard'; import './SessionBox.less'; -export const guard = new Guard({ - mode: 'modal', - appId: process.env.AUTHING_APP_ID! -}); - -export type SessionBoxProps = PropsWithChildren<{ - className?: string; - autoCover?: boolean; -}>; - -@observer -export default class SessionBox extends Component { - #started = false; - - componentDidMount() { - const { autoCover } = this.props; - - if (autoCover) this.openModal(); - } - - closeModal = () => { - this.#started = false; - - guard.hide(); - - document.scrollingElement?.classList.remove('overflow-hidden'); - }; - - async openModal() { - if (!userStore.sessionExpired) return; - - document.scrollingElement?.classList.add('overflow-hidden'); - - guard.on('close', this.closeModal); - guard.on('login-error', this.closeModal); - - this.#started = true; - - const user = await guard.start(); - - await userStore.signIn(user); - - this.closeModal(); - } - - captureInput = (event: MouseEvent) => { - event.preventDefault(); - event.stopPropagation(); - - if (this.#started) guard.show(); - else this.openModal(); - }; - - render() { - const { className, autoCover, children } = this.props, - { session } = userStore; - - return ( -
- {(!autoCover || session) && children} -
+export const SessionBox: FC> = observer( + ({ children, onSignIn, ...props }) => { + const { session, sessionExpired } = userStore; + + return session && !sessionExpired ? ( + children + ) : ( + { + await userStore.signIn(user); + onSignIn?.(user); + }} + /> ); } -} +); diff --git a/src/page/index.tsx b/src/page/index.tsx index fc4c624..b7163af 100644 --- a/src/page/index.tsx +++ b/src/page/index.tsx @@ -2,11 +2,11 @@ import { observer } from 'mobx-react'; import { FC } from 'react'; import { Container, Nav, Navbar } from 'react-bootstrap'; +import { SessionBox } from '../component/SessionBox'; import { UserBar } from '../component/UserBar'; import ApplicationModel from '../model'; import { Editor } from './Editor'; import { menu, title } from './index.json'; -import SessionBox from '../component/SessionBox'; export interface FrameProps { repository: string; @@ -32,7 +32,7 @@ export const Application: FC = observer(({ repository, store }) => ( - +