import { Button } from '@affine/admin/components/ui/button'; import { Input } from '@affine/admin/components/ui/input'; import { Label } from '@affine/admin/components/ui/label'; import { FeatureType, getUserFeaturesQuery } from '@affine/graphql'; import type { FormEvent } from 'react'; import { useCallback, useRef } from 'react'; import { Navigate } from 'react-router-dom'; import { toast } from 'sonner'; import { isAdmin, useCurrentUser, useRevalidateCurrentUser } from '../common'; import logo from './logo.svg'; export function Auth() { const currentUser = useCurrentUser(); const revalidate = useRevalidateCurrentUser(); const emailRef = useRef(null); const passwordRef = useRef(null); const login = useCallback( (e: FormEvent) => { e.preventDefault(); e.stopPropagation(); if (!emailRef.current || !passwordRef.current) return; fetch('/api/auth/sign-in', { method: 'POST', body: JSON.stringify({ email: emailRef.current?.value, password: passwordRef.current?.value, }), headers: { 'Content-Type': 'application/json', }, }) .then(async response => { if (!response.ok) { const data = await response.json(); throw new Error(data.message || 'Failed to login'); } return response.json(); }) .then(() => fetch('/graphql', { method: 'POST', body: JSON.stringify({ operationName: getUserFeaturesQuery.operationName, query: getUserFeaturesQuery.query, variables: {}, }), headers: { 'Content-Type': 'application/json', }, }) ) .then(res => res.json()) .then( ({ data: { currentUser: { features }, }, }) => { if (features.includes(FeatureType.Admin)) { toast.success('Logged in successfully'); revalidate(); } else { toast.error('You are not an admin'); } } ) .catch(err => { toast.error(`Failed to login: ${err.message}`); }); }, [revalidate] ); if (currentUser && isAdmin(currentUser)) { return ; } return (

Login

Enter your email below to login to your account

Image
); } export { Auth as Component };