@@ -2,9 +2,8 @@ import Head from 'next/head'
2
2
import Slimste from '../public/images/deslimste.svg'
3
3
import { signIn , signOut , useSession } from 'next-auth/client'
4
4
import React , { ChangeEvent , FormEvent , useEffect , useState } from 'react'
5
- import { router } from 'next/client'
6
-
7
-
5
+ import { useRouter } from 'next/router'
6
+ import User from '../models/User'
8
7
// eslint-disable-next-line @typescript-eslint/no-unused-vars
9
8
export function Index ( ) : JSX . Element {
10
9
@@ -14,9 +13,16 @@ export function Index( ) : JSX.Element{
14
13
15
14
// eslint-disable-next-line @typescript-eslint/no-unused-vars
16
15
const [ session , loading ] = useSession ( ) ;
17
- useEffect ( ( ) => {
18
- setState ( { ...state , auth_error : window . location . href . includes ( "CredentialsSignin" ) } ) ;
19
- } ) ;
16
+ const router = useRouter ( ) ;
17
+
18
+ let init = true ;
19
+
20
+ if ( init ) {
21
+ useEffect ( ( ) => {
22
+ setState ( { ...state , auth_error : window . location . href . includes ( "CredentialsSignin" ) } ) ;
23
+ } , [ ] ) ;
24
+ init = false ;
25
+ }
20
26
21
27
const [ state , setState ] = useState ( {
22
28
username : "" ,
@@ -31,25 +37,32 @@ export function Index( ) : JSX.Element{
31
37
function handlePasswordChange ( event : ChangeEvent < HTMLInputElement > ) {
32
38
setState ( { ...state , password : event . target . value } )
33
39
}
34
- function handleSignInSubmit ( event : FormEvent < HTMLFormElement > ) {
40
+ async function handleSignInSubmit ( event : FormEvent < HTMLFormElement > ) {
35
41
event . preventDefault ( ) ;
36
- // eslint-disable-next-line no-console
37
- signIn ( 'credentials' , {
42
+ await signIn ( 'credentials' , {
38
43
username : state . username ,
39
44
password : state . password
40
45
} )
41
- . then ( authenticated => {
42
- // eslint-disable-next-line no-console
43
- console . log ( "Auth: " , authenticated ) ;
44
- //router.push(`/auth/callback`);
45
- } )
46
- . catch ( ( ) => {
47
- alert ( "Authentication failed." ) ;
48
- } ) ;
49
46
}
50
- return (
47
+
48
+ function toTheGame ( ) {
49
+ if ( 'role' in session . user ) {
50
+ switch ( ( session . user as User ) . role ) {
51
+ case "admin" :
52
+ router . push ( "/admin" )
53
+ break ;
54
+ case "overlay" :
55
+ router . push ( "/stream-overlay" ) ;
56
+ break ;
57
+ case "user" :
58
+ router . push ( "/contestant" ) ;
59
+ break ;
60
+ }
61
+ }
62
+ }
63
+
64
+ return (
51
65
< >
52
- < div >
53
66
< Head >
54
67
< title > De slimste mens | welcome</ title >
55
68
< meta charSet = "utf-8" />
@@ -59,7 +72,6 @@ export function Index( ) : JSX.Element{
59
72
< meta name = "og:title" property = "og:title" content = "De slimste mens | Portal" />
60
73
< meta name = "og:description" property = "og:description" content = "Please login to get to your slimste mens screen" />
61
74
</ Head >
62
- < body >
63
75
< div className = { "flex flex-row justify-center" } >
64
76
< div className = { "ml-20" } >
65
77
< Slimste />
@@ -85,6 +97,7 @@ export function Index( ) : JSX.Element{
85
97
id = "username"
86
98
className = { "mt-3 form-control rounded-full py-3 px-6" }
87
99
onChange = { handleUsernameChange }
100
+ value = { state . username }
88
101
/>
89
102
</ p >
90
103
< p className = { "mt-5" } >
@@ -96,10 +109,11 @@ export function Index( ) : JSX.Element{
96
109
placeholder = ""
97
110
id = "password"
98
111
className = { "mt-3 form-control rounded-full py-3 px-6" }
112
+ value = { state . password }
99
113
onChange = { handlePasswordChange }
100
114
/>
101
- < p className = { "mt-5 text-red-400 text-l" } > { state . auth_error && "Wrong credentials" } </ p >
102
115
</ p >
116
+ < p className = { "mt-5 text-red-400 text-l" } > { state . auth_error && "Wrong credentials" } </ p >
103
117
< button
104
118
id = "submitButton"
105
119
type = "submit"
@@ -112,9 +126,9 @@ export function Index( ) : JSX.Element{
112
126
}
113
127
{ session &&
114
128
< div className = { "mr-20" } >
115
- < h1 className = { "text-white italic text-6xl" } > Hello { Capital ( session . user . username ) } </ h1 >
129
+ < h1 className = { "text-white italic text-6xl" } > Hello { Capital ( ( session . user as User ) . username ) } </ h1 >
116
130
< div className = { "flex flex-col justify-center" } >
117
- < button className = { "mt-10 bg-dsm p-5 rounded-full py-3 px-6 text-black font-bold w-64" } > Get to your
131
+ < button className = { "mt-10 bg-dsm p-5 rounded-full py-3 px-6 text-black font-bold w-64" } onClick = { toTheGame } > Get to your
118
132
game screen
119
133
</ button >
120
134
< button className = { "mt-10 bg-dsm p-5 rounded-full py-3 px-6 w-32 text-black font-bold" }
@@ -126,7 +140,6 @@ export function Index( ) : JSX.Element{
126
140
</ div >
127
141
</ div >
128
142
</ div >
129
- </ body >
130
143
< footer className = { "bottom-0 absolute h-20 w-screen bg-gray-200" } >
131
144
< div className = "text-center flex justify-center flex-col" >
132
145
< p className = { "mt-4" } >
@@ -138,7 +151,6 @@ export function Index( ) : JSX.Element{
138
151
</ p >
139
152
</ div >
140
153
</ footer >
141
- </ div >
142
154
</ >
143
155
)
144
156
}
0 commit comments