11"use client" ;
2+
23import styles from "./Login.module.css" ;
34import { url } from "@/globals" ;
45import { Dispatch , SetStateAction , useState } from "react" ;
5- import { isAdmin } from "../utils/credentials" ;
66import { Loader } from "../components/Loader/Loader" ;
77import { useRouter } from "next/navigation" ;
88import { AppRouterInstance } from "next/dist/shared/lib/app-router-context.shared-runtime" ;
99import { clsx } from "clsx" ;
10+ import { useAuth } from "@/app/context/AuthContext" ; // Import useAuth
11+ import { Role } from "../utils/credentials" ;
1012
1113async function handleSubmit (
1214 username : string ,
1315 password : string ,
1416 setMsg : Dispatch < SetStateAction < string > > ,
1517 router : AppRouterInstance ,
18+ login : (
19+ token : string ,
20+ username : string ,
21+ role : Role ,
22+ userId : string ,
23+ ) => void ,
24+ setLoading : ( isLoading : boolean ) => void ,
1625) {
26+ setLoading ( true ) ; // Set loading to true when the submission starts.
27+
1728 try {
1829 const loginUrl = new URL ( url ) ;
1930 loginUrl . pathname = "/login" ;
@@ -51,30 +62,34 @@ async function handleSubmit(
5162 }
5263
5364 const { id, token, username : responseUsername , role } = data . info ;
54- localStorage . setItem ( "id" , id ) ;
55- localStorage . setItem ( "token" , token ) ;
56- localStorage . setItem ( "username" , responseUsername ) ;
57- localStorage . setItem ( "role" , role ) ;
5865
59- window . dispatchEvent ( new Event ( "storage" ) ) ;
66+ // Use context's login function instead of localStorage
67+ login ( token , responseUsername , role , id ) ;
6068
61- if ( isAdmin ( role ) ) router . push ( "/Dashboard" ) ;
62- else router . push ( "/" ) ;
69+ // Redirect based on role
70+ setTimeout (
71+ ( ) => router . push ( role === "admin" ? "/Dashboard" : "/" ) ,
72+ 1000 ,
73+ ) ;
6374 } catch ( error ) {
6475 setMsg (
6576 `Greška prilikom prijave: ${
6677 error instanceof Error ? error . message : "Nepoznata greška"
6778 } `,
6879 ) ;
6980 console . error ( error ) ;
81+ } finally {
82+ setLoading ( false ) ; // Ensure loading is set back to false.
7083 }
7184}
7285
7386function ErrorMessage ( { message } : { message : string } ) {
7487 if ( ! message ) return null ;
7588 return (
7689 < div className = { styles [ "message-container" ] } >
77- < p className = { styles [ "message" ] } > { message } </ p >
90+ < p className = { clsx ( styles [ "message" ] , styles [ "error" ] ) } >
91+ { message }
92+ </ p >
7893 </ div >
7994 ) ;
8095}
@@ -108,6 +123,7 @@ function LoginButton({
108123
109124function LoginForm ( ) {
110125 const router = useRouter ( ) ;
126+ const { login } = useAuth ( ) ; // Get login function from context
111127 const [ message , setMessage ] = useState ( "" ) ;
112128 const [ isLoading , setLoading ] = useState ( false ) ;
113129 const [ username , setUsername ] = useState ( "" ) ;
@@ -118,13 +134,15 @@ function LoginForm() {
118134
119135 const handleFormSubmit = async ( event : React . FormEvent ) => {
120136 event . preventDefault ( ) ;
121- setMessage ( "" ) ;
122- setLoading ( true ) ;
123- try {
124- await handleSubmit ( username , password , setMessage , router ) ;
125- } finally {
126- setLoading ( false ) ;
127- }
137+ setMessage ( "" ) ; // Clear previous message on new submission attempt.
138+ await handleSubmit (
139+ username ,
140+ password ,
141+ setMessage ,
142+ router ,
143+ login ,
144+ setLoading ,
145+ ) ;
128146 } ;
129147
130148 return (
0 commit comments