@@ -26,17 +26,24 @@ import { useRouter } from "next/navigation";
2626import { useToast } from "@/hooks/use-toast" ;
2727import { Separator } from "../ui/separator" ;
2828import { SiGitlab } from "react-icons/si" ;
29+ import useSWR from "swr" ;
30+ import { AuthStatus } from "@/lib/types" ;
31+ import { Skeleton } from "../ui/skeleton" ;
2932
3033const formSchema = z . object ( {
3134 username : z . string ( ) . min ( 1 , "Username is required" ) ,
3235 password : z . string ( ) . min ( 1 , "Password is required" ) ,
3336} ) ;
3437
38+ const fetcher = ( url : string ) => api . get ( url ) . then ( res => res . data . data ) ;
39+
3540export function LoginForm ( ) {
3641 const { login } = useAuth ( ) ;
3742 const router = useRouter ( ) ;
3843 const { toast } = useToast ( ) ;
3944 const gitlabLoginUrl = `/api/v1/auth/gitlab/login` ;
45+
46+ const { data : authStatus , isLoading } = useSWR < AuthStatus > ( '/auth/status' , fetcher ) ;
4047
4148 const form = useForm < z . infer < typeof formSchema > > ( {
4249 resolver : zodResolver ( formSchema ) ,
@@ -64,78 +71,109 @@ export function LoginForm() {
6471 } ) ;
6572 }
6673 } ;
74+
75+ if ( isLoading ) {
76+ return (
77+ < Card >
78+ < CardHeader >
79+ < CardTitle > Login to CSOJ</ CardTitle >
80+ < CardDescription >
81+ Checking available login methods...
82+ </ CardDescription >
83+ </ CardHeader >
84+ < CardContent className = "space-y-4" >
85+ < Skeleton className = "h-10 w-full" />
86+ < Skeleton className = "h-10 w-full" />
87+ < div className = "relative my-4" >
88+ < div className = "absolute inset-0 flex items-center" >
89+ < Separator />
90+ </ div >
91+ </ div >
92+ < Skeleton className = "h-10 w-full" />
93+ </ CardContent >
94+ </ Card >
95+ ) ;
96+ }
6797
6898 return (
6999 < Card >
70100 < CardHeader >
71101 < CardTitle > Login to CSOJ</ CardTitle >
72102 < CardDescription >
73- Enter your credentials to access your account.
103+ { authStatus ?. local_auth_enabled
104+ ? "Enter your credentials or use an alternative login method."
105+ : "Please use an available login method." }
74106 </ CardDescription >
75107 </ CardHeader >
76108 < CardContent >
77- < Form { ...form } >
78- < form onSubmit = { form . handleSubmit ( onSubmit ) } className = "space-y-4" >
79- < FormField
80- control = { form . control }
81- name = "username"
82- render = { ( { field } ) => (
83- < FormItem >
84- < FormLabel > Username</ FormLabel >
85- < FormControl >
86- < Input placeholder = "your_username" { ...field } />
87- </ FormControl >
88- < FormMessage />
89- </ FormItem >
90- ) }
91- />
92- < FormField
93- control = { form . control }
94- name = "password"
95- render = { ( { field } ) => (
96- < FormItem >
97- < FormLabel > Password</ FormLabel >
98- < FormControl >
99- < Input type = "password" placeholder = "********" { ...field } />
100- </ FormControl >
101- < FormMessage />
102- </ FormItem >
103- ) }
104- />
105- < Button
106- type = "submit"
107- className = "w-full"
108- disabled = { form . formState . isSubmitting }
109- >
110- { form . formState . isSubmitting ? "Logging in..." : "Login" }
111- </ Button >
112- </ form >
113- </ Form >
109+ { authStatus ?. local_auth_enabled && (
110+ < >
111+ < Form { ...form } >
112+ < form onSubmit = { form . handleSubmit ( onSubmit ) } className = "space-y-4" >
113+ < FormField
114+ control = { form . control }
115+ name = "username"
116+ render = { ( { field } ) => (
117+ < FormItem >
118+ < FormLabel > Username</ FormLabel >
119+ < FormControl >
120+ < Input placeholder = "your_username" { ...field } />
121+ </ FormControl >
122+ < FormMessage />
123+ </ FormItem >
124+ ) }
125+ />
126+ < FormField
127+ control = { form . control }
128+ name = "password"
129+ render = { ( { field } ) => (
130+ < FormItem >
131+ < FormLabel > Password</ FormLabel >
132+ < FormControl >
133+ < Input type = "password" placeholder = "********" { ...field } />
134+ </ FormControl >
135+ < FormMessage />
136+ </ FormItem >
137+ ) }
138+ />
139+ < Button
140+ type = "submit"
141+ className = "w-full"
142+ disabled = { form . formState . isSubmitting }
143+ >
144+ { form . formState . isSubmitting ? "Logging in..." : "Login" }
145+ </ Button >
146+ </ form >
147+ </ Form >
114148
115- < div className = "relative my-4" >
116- < div className = "absolute inset-0 flex items-center" >
149+ < div className = "relative my-4" >
150+ < div className = "absolute inset-0 flex items-center" >
117151 < Separator />
118- </ div >
119- < div className = "relative flex justify-center text-xs uppercase" >
152+ </ div >
153+ < div className = "relative flex justify-center text-xs uppercase" >
120154 < span className = "bg-card px-2 text-muted-foreground" >
121- Or continue with
155+ Or continue with
122156 </ span >
157+ </ div >
123158 </ div >
124- </ div >
159+ </ >
160+ ) }
125161
126162 < Button variant = "outline" className = "w-full" asChild >
127- < a href = { gitlabLoginUrl } >
128- < SiGitlab className = "mr-2 h-4 w-4" />
129- Login with GitLab
130- </ a >
163+ < a href = { gitlabLoginUrl } >
164+ < SiGitlab className = "mr-2 h-4 w-4" />
165+ Login with GitLab
166+ </ a >
131167 </ Button >
132168
133- < div className = "mt-4 text-center text-sm" >
134- Don't have an account?{ " " }
135- < Link href = "/register" className = "underline" >
136- Register
137- </ Link >
138- </ div >
169+ { authStatus ?. local_auth_enabled && (
170+ < div className = "mt-4 text-center text-sm" >
171+ Don't have an account?{ " " }
172+ < Link href = "/register" className = "underline" >
173+ Register
174+ </ Link >
175+ </ div >
176+ ) }
139177 </ CardContent >
140178 </ Card >
141179 ) ;
0 commit comments