domain-watchdog/assets/pages/LoginPage.tsx

71 lines
2.2 KiB
TypeScript
Raw Normal View History

2024-12-30 23:50:15 +01:00
import React, {createContext, useEffect, useState} from 'react'
import {Button, Card} from 'antd'
2024-07-28 15:36:22 +02:00
import {t} from 'ttag'
2024-12-30 23:50:15 +01:00
import TextPage from './TextPage'
import {LoginForm} from '../components/LoginForm'
2024-12-31 13:55:42 +01:00
import type { InstanceConfig} from '../utils/api'
import {getConfiguration} from '../utils/api'
2024-12-30 23:50:15 +01:00
import {RegisterForm} from '../components/RegisterForm'
2025-10-31 23:13:45 +01:00
import useBreakpoint from "../hooks/useBreakpoint"
2024-07-26 18:31:47 +02:00
2024-12-30 23:50:15 +01:00
export const AuthenticatedContext = createContext<
{
authenticated: (authenticated: boolean) => void
setIsAuthenticated: React.Dispatch<React.SetStateAction<boolean>>
}
>({
authenticated: () => {
},
setIsAuthenticated: () => {
}
})
2024-07-23 18:37:59 +02:00
2024-08-02 16:17:55 +02:00
export default function LoginPage() {
2024-08-05 03:11:03 +02:00
const [wantRegister, setWantRegister] = useState<boolean>(false)
2024-08-04 15:49:38 +02:00
const [configuration, setConfiguration] = useState<InstanceConfig>()
2025-10-31 13:46:17 +01:00
const md = useBreakpoint('md')
2024-07-26 18:31:47 +02:00
2024-08-05 03:11:03 +02:00
const toggleWantRegister = () => {
setWantRegister(!wantRegister)
2024-07-26 18:31:47 +02:00
}
2024-08-02 16:17:55 +02:00
useEffect(() => {
getConfiguration().then((configuration) => {
if(!configuration.registerEnabled && configuration.ssoLogin && configuration.ssoAutoRedirect) {
window.location.href = '/login/oauth'
return
}
setConfiguration(configuration)
})
2024-08-02 16:17:55 +02:00
}, [])
2025-10-31 13:46:17 +01:00
const grid = [
2025-10-31 22:52:45 +01:00
<Card.Grid key="form" style={{width: md ? '100%' : '50%', textAlign: 'center'}} hoverable={false}>
2025-10-31 13:46:17 +01:00
{wantRegister ? <RegisterForm/> : <LoginForm ssoLogin={configuration?.ssoLogin}/>}
{
configuration?.registerEnabled &&
<Button
type='link'
block
style={{marginTop: '1em'}}
onClick={toggleWantRegister}
>{wantRegister ? t`Log in` : t`Create an account`}
</Button>
}
</Card.Grid>,
2025-10-31 22:52:45 +01:00
<Card.Grid key="ads" style={{width: md ? '100%' : '50%'}} hoverable={false}>
2025-10-31 13:46:17 +01:00
<TextPage resource='ads.md'/>
</Card.Grid>
2025-10-31 22:52:45 +01:00
]
2025-10-31 13:46:17 +01:00
if (md) {
2025-10-31 22:52:45 +01:00
grid.reverse()
2025-10-31 13:46:17 +01:00
}
2024-12-30 23:50:15 +01:00
return (
<Card title={wantRegister ? t`Register` : t`Log in`} style={{width: '100%'}}>
2025-10-31 13:46:17 +01:00
{grid}
2024-12-30 23:50:15 +01:00
</Card>
)
}