1
- import React , { useState } from 'react' ;
2
- import { useNavigate } from 'react-router-dom' ;
3
- // @material -ui/core components
1
+ import React , { useState , FormEvent } from 'react' ;
2
+ import { useNavigate , Navigate } from 'react-router-dom' ;
4
3
import FormControl from '@material-ui/core/FormControl' ;
5
4
import InputLabel from '@material-ui/core/InputLabel' ;
6
- // core components
7
5
import GridItem from '../../components/Grid/GridItem' ;
8
6
import GridContainer from '../../components/Grid/GridContainer' ;
9
7
import Input from '@material-ui/core/Input' ;
@@ -12,70 +10,83 @@ import Card from '../../components/Card/Card';
12
10
import CardHeader from '../../components/Card/CardHeader' ;
13
11
import CardBody from '../../components/Card/CardBody' ;
14
12
import CardFooter from '../../components/Card/CardFooter' ;
15
- import axios from 'axios' ;
13
+ import axios , { AxiosError } from 'axios' ;
16
14
import logo from '../../assets/img/git-proxy.png' ;
17
15
import { Badge , CircularProgress , Snackbar } from '@material-ui/core' ;
18
16
import { getCookie } from '../../utils' ;
19
17
import { useAuth } from '../../auth/AuthProvider' ;
20
18
21
- const loginUrl = `${ import . meta. env . VITE_API_URI } /api/auth/login` ;
19
+ interface LoginResponse {
20
+ username : string ;
21
+ password : string ;
22
+ }
22
23
23
- export default function UserProfile ( ) {
24
- const [ username , setUsername ] = useState ( '' ) ;
25
- const [ password , setPassword ] = useState ( '' ) ;
26
- const [ message , setMessage ] = useState ( '' ) ;
27
- const [ , setGitAccountError ] = useState ( false ) ;
28
- const [ isLoading , setIsLoading ] = useState ( false ) ;
24
+ const loginUrl = `${ import . meta. env . VITE_API_URI } /api/auth/login` ;
29
25
26
+ const Login : React . FC = ( ) => {
30
27
const navigate = useNavigate ( ) ;
31
28
const { refreshUser } = useAuth ( ) ;
32
29
33
- function validateForm ( ) {
30
+ const [ username , setUsername ] = useState < string > ( '' ) ;
31
+ const [ password , setPassword ] = useState < string > ( '' ) ;
32
+ const [ message , setMessage ] = useState < string > ( '' ) ;
33
+ const [ success , setSuccess ] = useState < boolean > ( false ) ;
34
+ const [ gitAccountError , setGitAccountError ] = useState < boolean > ( false ) ;
35
+ const [ isLoading , setIsLoading ] = useState < boolean > ( false ) ;
36
+
37
+ function validateForm ( ) : boolean {
34
38
return (
35
39
username . length > 0 && username . length < 100 && password . length > 0 && password . length < 200
36
40
) ;
37
41
}
38
42
39
- function handleOIDCLogin ( ) {
43
+ function handleOIDCLogin ( ) : void {
40
44
window . location . href = `${ import . meta. env . VITE_API_URI } /api/auth/oidc` ;
41
45
}
42
46
43
- function handleSubmit ( event ) {
47
+ function handleSubmit ( event : FormEvent ) : void {
48
+ event . preventDefault ( ) ;
44
49
setIsLoading ( true ) ;
50
+
45
51
axios
46
- . post (
52
+ . post < LoginResponse > (
47
53
loginUrl ,
48
- {
49
- username : username ,
50
- password : password ,
51
- } ,
54
+ { username, password } ,
52
55
{
53
56
withCredentials : true ,
54
57
headers : {
55
58
'Content-Type' : 'application/json' ,
56
- 'X-CSRF-TOKEN' : getCookie ( 'csrf' ) ,
59
+ 'X-CSRF-TOKEN' : getCookie ( 'csrf' ) || '' ,
57
60
} ,
58
61
} ,
59
62
)
60
- . then ( function ( ) {
63
+ . then ( ( ) => {
61
64
window . sessionStorage . setItem ( 'git.proxy.login' , 'success' ) ;
62
65
setMessage ( 'Success!' ) ;
63
- setIsLoading ( false ) ;
64
- refreshUser ( ) . then ( ( ) => navigate ( '/dashboard/repo' ) ) ;
66
+ setSuccess ( true ) ;
67
+ refreshUser ( ) ;
65
68
} )
66
- . catch ( function ( error ) {
67
- if ( error . response . status === 307 ) {
69
+ . catch ( ( error : AxiosError ) => {
70
+ if ( error . response ? .status === 307 ) {
68
71
window . sessionStorage . setItem ( 'git.proxy.login' , 'success' ) ;
69
72
setGitAccountError ( true ) ;
70
- } else if ( error . response . status === 403 ) {
73
+ } else if ( error . response ? .status === 403 ) {
71
74
setMessage ( 'You do not have the correct access permissions...' ) ;
72
75
} else {
73
76
setMessage ( 'You entered an invalid username or password...' ) ;
74
77
}
78
+ } )
79
+ . finally ( ( ) => {
75
80
setIsLoading ( false ) ;
76
81
} ) ;
82
+ }
77
83
78
- event . preventDefault ( ) ;
84
+ if ( gitAccountError ) {
85
+ return < Navigate to = '/dashboard/profile' /> ;
86
+ }
87
+
88
+ if ( success ) {
89
+ return < Navigate to = '/dashboard/repo' /> ;
79
90
}
80
91
81
92
return (
@@ -91,43 +102,36 @@ export default function UserProfile() {
91
102
< GridItem xs = { 12 } sm = { 10 } md = { 6 } lg = { 4 } xl = { 3 } >
92
103
< Card >
93
104
< CardHeader color = 'primary' >
94
- < div
95
- style = { {
96
- textAlign : 'center' ,
97
- marginRight : '10px' ,
98
- marginTop : '12px' ,
99
- marginBottom : '12px' ,
100
- } }
101
- >
105
+ < div style = { { textAlign : 'center' , margin : '12px 10px' } } >
102
106
< img
103
- style = { { verticalAlign : 'middle' , filter : 'brightness(0) invert(1)' } }
104
- width = { '150px' }
105
107
src = { logo }
106
108
alt = 'logo'
109
+ width = { 150 }
110
+ style = { { verticalAlign : 'middle' , filter : 'brightness(0) invert(1)' } }
107
111
data-test = 'git-proxy-logo'
108
112
/>
109
113
</ div >
110
114
</ CardHeader >
111
115
< CardBody >
112
116
< GridContainer >
113
- < GridItem xs = { 6 } sm = { 6 } md = { 6 } >
114
- < FormControl >
115
- < InputLabel > Username</ InputLabel >
117
+ < GridItem xs = { 12 } sm = { 12 } md = { 12 } >
118
+ < FormControl fullWidth >
119
+ < InputLabel htmlFor = 'username' > Username</ InputLabel >
116
120
< Input
117
121
id = 'username'
118
- type = 'username '
122
+ type = 'text '
119
123
value = { username }
120
124
onChange = { ( e ) => setUsername ( e . target . value ) }
121
- autoFocus = { true }
125
+ autoFocus
122
126
data-test = 'username'
123
127
/>
124
128
</ FormControl >
125
129
</ GridItem >
126
130
</ GridContainer >
127
131
< GridContainer >
128
- < GridItem xs = { 6 } sm = { 6 } md = { 6 } >
129
- < FormControl >
130
- < InputLabel > Password</ InputLabel >
132
+ < GridItem xs = { 12 } sm = { 12 } md = { 12 } >
133
+ < FormControl fullWidth >
134
+ < InputLabel htmlFor = 'password' > Password</ InputLabel >
131
135
< Input
132
136
id = 'password'
133
137
type = 'password'
@@ -162,9 +166,9 @@ export default function UserProfile() {
162
166
) }
163
167
</ CardFooter >
164
168
</ Card >
165
- < div style = { { textAlign : 'center' , opacity : 0.9 , fontSize : '12px' } } >
166
- < Badge overlap = 'rectangular' color = 'error' badgeContent = { 'NEW' } /> { ' ' }
167
- < span style = { { paddingLeft : '20px' } } >
169
+ < div style = { { textAlign : 'center' , opacity : 0.9 , fontSize : 12 , marginTop : 20 } } >
170
+ < Badge overlap = 'rectangular' color = 'error' badgeContent = 'NEW' />
171
+ < span style = { { paddingLeft : 20 } } >
168
172
View our < a href = '/dashboard/push' > open source activity feed</ a > or{ ' ' }
169
173
< a href = '/dashboard/repo' > scroll through projects</ a > we contribute to
170
174
</ span >
@@ -173,4 +177,6 @@ export default function UserProfile() {
173
177
</ GridContainer >
174
178
</ form >
175
179
) ;
176
- }
180
+ } ;
181
+
182
+ export default Login ;
0 commit comments