page.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. "use client"
  2. import { useState, useEffect } from "react"
  3. import { signIn, useSession } from "next-auth/react"
  4. import { useRouter } from "next/navigation"
  5. import Link from "next/link"
  6. import { Button } from "@/components/ui/button"
  7. import { Input } from "@/components/ui/input"
  8. import { Label } from "@/components/ui/label"
  9. import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"
  10. import { User, Lock, AlertCircle } from "lucide-react"
  11. import { notifications } from "@/lib/notifications"
  12. export default function LoginPage() {
  13. const [username, setUsername] = useState("")
  14. const [password, setPassword] = useState("")
  15. const [loading, setLoading] = useState(false)
  16. const router = useRouter()
  17. const { data: session, status } = useSession()
  18. // Redirigir si ya está autenticado
  19. useEffect(() => {
  20. if (session) {
  21. router.push("/dashboard")
  22. }
  23. }, [session, router])
  24. // Mostrar loading mientras se verifica la sesión
  25. if (status === "loading") {
  26. return (
  27. <div className="min-h-screen bg-gradient-to-br from-blue-50 to-indigo-100 flex items-center justify-center p-4">
  28. <div className="w-8 h-8 border-4 border-blue-600 border-t-transparent rounded-full animate-spin"></div>
  29. </div>
  30. )
  31. }
  32. // Si ya está autenticado, mostrar loading mientras redirige
  33. if (session) {
  34. return (
  35. <div className="min-h-screen bg-gradient-to-br from-blue-50 to-indigo-100 flex items-center justify-center p-4">
  36. <div className="text-center">
  37. <div className="w-8 h-8 border-4 border-blue-600 border-t-transparent rounded-full animate-spin mx-auto mb-4"></div>
  38. <p className="text-gray-600">Redirigiendo...</p>
  39. </div>
  40. </div>
  41. )
  42. }
  43. const handleSubmit = async (e: React.FormEvent) => {
  44. e.preventDefault()
  45. setLoading(true)
  46. try {
  47. const result = await signIn("credentials", {
  48. username,
  49. password,
  50. redirect: false,
  51. })
  52. if (result?.error) {
  53. notifications.auth.loginError()
  54. } else {
  55. notifications.auth.loginSuccess()
  56. router.push("/dashboard")
  57. }
  58. } catch (error) {
  59. console.error("Error en login:", error)
  60. notifications.auth.loginGeneralError()
  61. } finally {
  62. setLoading(false)
  63. }
  64. }
  65. return (
  66. <div className="min-h-screen bg-gradient-to-br from-blue-50 to-indigo-100 flex items-center justify-center p-4">
  67. <Card className="w-full max-w-md">
  68. <CardHeader className="text-center">
  69. <div className="flex items-center justify-center mb-4">
  70. <div className="w-12 h-12 bg-blue-600 rounded-lg flex items-center justify-center mr-3">
  71. <User className="w-6 h-6 text-white" />
  72. </div>
  73. <CardTitle className="text-2xl">Ani Assistant</CardTitle>
  74. </div>
  75. <p className="text-gray-600">Inicia sesión en tu cuenta</p>
  76. </CardHeader>
  77. <CardContent>
  78. <form onSubmit={handleSubmit} className="space-y-4">
  79. <div className="space-y-2">
  80. <Label htmlFor="username">Usuario</Label>
  81. <Input
  82. id="username"
  83. type="text"
  84. placeholder="1206706838-EST"
  85. value={username}
  86. onChange={(e) => setUsername(e.target.value)}
  87. required
  88. />
  89. </div>
  90. <div className="space-y-2">
  91. <Label htmlFor="password">Contraseña</Label>
  92. <Input
  93. id="password"
  94. type="password"
  95. placeholder="••••••••"
  96. value={password}
  97. onChange={(e) => setPassword(e.target.value)}
  98. required
  99. />
  100. </div>
  101. <div className="space-y-2">
  102. <Button
  103. type="submit"
  104. className="w-full"
  105. disabled={loading}
  106. >
  107. {loading ? (
  108. <div className="w-4 h-4 border-2 border-white border-t-transparent rounded-full animate-spin mr-2"></div>
  109. ) : (
  110. <Lock className="w-4 h-4 mr-2" />
  111. )}
  112. Iniciar Sesión
  113. </Button>
  114. <Button
  115. type="button"
  116. variant="outline"
  117. className="w-full"
  118. onClick={() => router.push("/")}
  119. disabled={loading}
  120. >
  121. Cancelar
  122. </Button>
  123. </div>
  124. </form>
  125. <div className="mt-6 p-4 bg-blue-50 rounded-lg">
  126. <div className="flex items-center">
  127. <AlertCircle className="w-4 h-4 text-blue-600 mr-2" />
  128. <p className="text-sm text-blue-800">
  129. <strong>Acceso UTB:</strong> Usa tus credenciales institucionales
  130. (ejemplo: 1206706838-EST)
  131. </p>
  132. </div>
  133. </div>
  134. </CardContent>
  135. </Card>
  136. </div>
  137. )
  138. }