MVP frontend+backend
This commit is contained in:
@@ -1,22 +1,34 @@
|
||||
import { useState } from 'react'
|
||||
import { useEffect, useState } from 'react'
|
||||
import { useNavigate } from 'react-router-dom'
|
||||
import { useAuth } from '@/context/AuthContext'
|
||||
import { useWorkspace } from '@/context/WorkspaceContext'
|
||||
|
||||
export function Auth() {
|
||||
const { login, register } = useAuth()
|
||||
const { login, register, isAuthenticated } = useAuth()
|
||||
const { isLoading: wsLoading } = useWorkspace()
|
||||
const navigate = useNavigate()
|
||||
const [isRegister, setIsRegister] = useState(false)
|
||||
const [email, setEmail] = useState('')
|
||||
const [password, setPassword] = useState('')
|
||||
const [displayName, setDisplayName] = useState('')
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
|
||||
useEffect(() => {
|
||||
if (isAuthenticated && !wsLoading) {
|
||||
navigate('/', { replace: true })
|
||||
}
|
||||
}, [isAuthenticated, wsLoading, navigate])
|
||||
|
||||
const handleSubmit = async (e: React.FormEvent) => {
|
||||
e.preventDefault()
|
||||
setError(null)
|
||||
try {
|
||||
if (isRegister) {
|
||||
await register({ email, password, displayName })
|
||||
navigate('/', { replace: true })
|
||||
} else {
|
||||
await login({ email, password })
|
||||
navigate('/', { replace: true })
|
||||
}
|
||||
} catch (err: unknown) {
|
||||
setError(err instanceof Error ? err.message : 'Något gick fel')
|
||||
|
||||
Reference in New Issue
Block a user