1
- 'use client'
2
- import { Database } from '../../../types/supabase'
3
- import React , { useState } from 'react'
1
+ 'use client' ;
2
+ import { Database } from '../../../types/supabase' ;
3
+ import React , { useState } from 'react' ;
4
4
import { AuthError } from '@supabase/supabase-js' ;
5
- import { createClientComponentClient } from '@supabase/auth-helpers-nextjs'
5
+ import { createClientComponentClient } from '@supabase/auth-helpers-nextjs' ;
6
6
import ErrorAlert from './ErrorAlert' ;
7
7
import { useRouter } from 'next/router' ;
8
8
9
-
10
9
export default function SignInForm ( ) {
11
- const supabase = createClientComponentClient < Database > ( )
12
- const [ error , setError ] = useState < AuthError | null > ( null ) ;
13
- const [ isMainButtonDisabled , setIsMainButtonDisabled ] = useState < boolean > ( false ) ;
14
- const { push } = useRouter ( ) ;
15
-
16
- async function signInWithPassword ( email : string , password : string ) {
17
- setIsMainButtonDisabled ( true ) ;
18
- const { data, error } = await supabase . auth . signInWithPassword ( {
19
- email,
20
- password,
21
- } )
22
- setIsMainButtonDisabled ( false ) ;
23
- if ( error ) {
24
- setError ( error )
25
- } else {
26
- push ( '/home' ) ;
27
- }
28
- }
29
-
30
- async function onFormSubmit ( event : React . FormEvent < HTMLFormElement > ) {
31
- event . preventDefault ( )
32
- const target = event . target as typeof event . target & {
33
- email : { value : string }
34
- password : { value : string }
35
- }
36
- const email = target . email . value
37
- const password = target . password . value
38
- await signInWithPassword ( email , password )
10
+ const supabase = createClientComponentClient < Database > ( ) ;
11
+ const [ error , setError ] = useState < AuthError | null > ( null ) ;
12
+ const [ isMainButtonDisabled , setIsMainButtonDisabled ] =
13
+ useState < boolean > ( false ) ;
14
+ const { push } = useRouter ( ) ;
15
+
16
+ async function signInWithPassword ( email : string , password : string ) {
17
+ setIsMainButtonDisabled ( true ) ;
18
+ const { data, error } = await supabase . auth . signInWithPassword ( {
19
+ email,
20
+ password,
21
+ } ) ;
22
+ setIsMainButtonDisabled ( false ) ;
23
+ if ( error ) {
24
+ setError ( error ) ;
25
+ } else {
26
+ push ( '/home' ) ;
39
27
}
28
+ }
40
29
41
- return (
42
- < >
43
- { error && < ErrorAlert error = { error } /> }
44
- < form className = "space-y-6" action = "#" method = "POST" onSubmit = { onFormSubmit } >
45
- < div >
46
- < label htmlFor = "email" className = "block text-sm font-medium leading-6 text-gray-900" >
47
- Email address
48
- </ label >
49
- < div className = "mt-2" >
50
- < input
51
- id = "email"
52
- name = "email"
53
- type = "email"
54
- autoComplete = "email"
55
- required
56
- className = "block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6"
57
- />
58
- </ div >
30
+ async function onFormSubmit ( event : React . FormEvent < HTMLFormElement > ) {
31
+ event . preventDefault ( ) ;
32
+ const target = event . target as typeof event . target & {
33
+ email : { value : string } ;
34
+ password : { value : string } ;
35
+ } ;
36
+ const email = target . email . value ;
37
+ const password = target . password . value ;
38
+ await signInWithPassword ( email , password ) ;
39
+ }
40
+
41
+ return (
42
+ < >
43
+ { error && < ErrorAlert error = { error } /> }
44
+ < form
45
+ className = "space-y-6"
46
+ action = "#"
47
+ method = "POST"
48
+ onSubmit = { onFormSubmit }
49
+ >
50
+ < div >
51
+ < label
52
+ htmlFor = "email"
53
+ className = "block text-sm font-medium leading-6 text-gray-900"
54
+ >
55
+ Email address
56
+ </ label >
57
+ < div className = "mt-2" >
58
+ < input
59
+ id = "email"
60
+ name = "email"
61
+ type = "email"
62
+ autoComplete = "email"
63
+ required
64
+ className = "block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6"
65
+ />
59
66
</ div >
67
+ </ div >
60
68
61
- < div >
62
- < div className = "flex items-center justify-between" >
63
- < label htmlFor = "password" className = "block text-sm font-medium leading-6 text-gray-900" >
64
- Password
65
- </ label >
66
- < div className = "text-sm" >
67
- < a href = "#" className = "font-semibold text-indigo-600 hover:text-indigo-500" >
68
- Forgot password?
69
- </ a >
70
- </ div >
71
- </ div >
72
- < div className = "mt-2" >
73
- < input
74
- id = "password"
75
- name = "password"
76
- type = "password"
77
- autoComplete = "current-password"
78
- required
79
- className = "block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6"
80
- />
69
+ < div >
70
+ < div className = "flex items-center justify-between" >
71
+ < label
72
+ htmlFor = "password"
73
+ className = "block text-sm font-medium leading-6 text-gray-900"
74
+ >
75
+ Password
76
+ </ label >
77
+ < div className = "text-sm" >
78
+ < a
79
+ href = "#"
80
+ className = "font-semibold text-indigo-600 hover:text-indigo-500"
81
+ >
82
+ Forgot password?
83
+ </ a >
81
84
</ div >
82
85
</ div >
83
- < div >
84
- < button
85
- disabled = { isMainButtonDisabled }
86
- type = "submit"
87
- className = "flex w-full justify-center rounded-md bg-indigo-600 px-3 py-1.5 text-sm font-semibold leading-6 text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"
88
- >
89
- Sign in
90
- </ button >
86
+ < div className = "mt-2" >
87
+ < input
88
+ id = "password"
89
+ name = "password"
90
+ type = "password"
91
+ autoComplete = "current-password"
92
+ required
93
+ className = "block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6"
94
+ />
91
95
</ div >
92
- </ form >
93
- </ >
94
-
95
- )
96
- }
97
-
96
+ </ div >
97
+ < div >
98
+ < button
99
+ disabled = { isMainButtonDisabled }
100
+ type = "submit"
101
+ className = "flex w-full justify-center rounded-md bg-indigo-600 px-3 py-1.5 text-sm font-semibold leading-6 text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"
102
+ >
103
+ Sign in
104
+ </ button >
105
+ </ div >
106
+ </ form >
107
+ </ >
108
+ ) ;
109
+ }
0 commit comments