This repository has been archived by the owner on Jun 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[refactor] simplify Session Box component
- Loading branch information
Showing
3 changed files
with
54 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<HTMLDivElement> { | ||
onSignIn: (user: object) => any; | ||
onError?: (message: any) => any; | ||
} | ||
|
||
export class AuthingGuard extends Component<AuthingGuardProps> { | ||
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 <div {...props} ref={this.init} />; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<SessionBoxProps> { | ||
#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<HTMLDivElement>) => { | ||
event.preventDefault(); | ||
event.stopPropagation(); | ||
|
||
if (this.#started) guard.show(); | ||
else this.openModal(); | ||
}; | ||
|
||
render() { | ||
const { className, autoCover, children } = this.props, | ||
{ session } = userStore; | ||
|
||
return ( | ||
<div | ||
className={className} | ||
onClickCapture={autoCover || session ? undefined : this.captureInput} | ||
> | ||
{(!autoCover || session) && children} | ||
</div> | ||
export const SessionBox: FC<Partial<AuthingGuardProps>> = observer( | ||
({ children, onSignIn, ...props }) => { | ||
const { session, sessionExpired } = userStore; | ||
|
||
return session && !sessionExpired ? ( | ||
children | ||
) : ( | ||
<AuthingGuard | ||
{...props} | ||
onSignIn={async user => { | ||
await userStore.signIn(user); | ||
onSignIn?.(user); | ||
}} | ||
/> | ||
); | ||
} | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fee5879
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Deploy preview for git-pager ready!
✅ Preview
https://git-pager-l5ltgxcku-techquerys-projects.vercel.app
Built with commit fee5879.
This pull request is being automatically deployed with vercel-action