feat: loading and error feed back on login form
This commit is contained in:
parent
a2bc9d0d5f
commit
4ca5e0a299
@ -8,6 +8,8 @@ import { Label } from '@/components/ui/label'
|
|||||||
import { useRouter, useSearchParams } from 'next/navigation'
|
import { useRouter, useSearchParams } from 'next/navigation'
|
||||||
import { useState } from 'react'
|
import { useState } from 'react'
|
||||||
import { useGlobal } from '@/providers/GlobalProvider'
|
import { useGlobal } from '@/providers/GlobalProvider'
|
||||||
|
import { Loader2 } from 'lucide-react'
|
||||||
|
import { toast } from 'sonner'
|
||||||
|
|
||||||
export function LoginForm({ className, ...props }: React.ComponentProps<'div'>) {
|
export function LoginForm({ className, ...props }: React.ComponentProps<'div'>) {
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
@ -39,14 +41,19 @@ export function LoginForm({ className, ...props }: React.ComponentProps<'div'>)
|
|||||||
password,
|
password,
|
||||||
}),
|
}),
|
||||||
})
|
})
|
||||||
const user = await loginReq.json()
|
const loginResponse = await loginReq.json()
|
||||||
|
|
||||||
setUser(user.user)
|
if (loginResponse.errors?.length) {
|
||||||
|
loginResponse.errors.forEach((e: Error) => {
|
||||||
|
toast(e.message)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
if (user.token) router.push(searchParams.get('redirectUrl') || '/')
|
if (loginResponse.user) setUser(loginResponse.user)
|
||||||
|
|
||||||
|
if (loginResponse.token) router.push(searchParams.get('redirectUrl') || '/')
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Login failed:', error)
|
toast('Unknown issue while authenticating. Try again')
|
||||||
} finally {
|
|
||||||
setIsLoading(false)
|
setIsLoading(false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -85,16 +92,11 @@ export function LoginForm({ className, ...props }: React.ComponentProps<'div'>)
|
|||||||
</div>
|
</div>
|
||||||
<Input id="password" name="password" type="password" required />
|
<Input id="password" name="password" type="password" required />
|
||||||
</div>
|
</div>
|
||||||
<Button type="submit" className="w-full">
|
<Button disabled={isLoading} type="submit" className="w-full">
|
||||||
Login
|
{isLoading && <Loader2 />}
|
||||||
|
<span>Login</span>
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
<div className="text-center text-sm">
|
|
||||||
Don't have an account?{' '}
|
|
||||||
<a href="/requestAccess" className="underline underline-offset-4">
|
|
||||||
Request Access
|
|
||||||
</a>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
</CardContent>
|
</CardContent>
|
||||||
|
@ -8,7 +8,7 @@ type GlobalProps = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type GlobalState = {
|
type GlobalState = {
|
||||||
setUser: (users?: User) => void
|
setUser: (user?: User) => void
|
||||||
} & GlobalProps
|
} & GlobalProps
|
||||||
|
|
||||||
const defaultState = {
|
const defaultState = {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user