-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e7f4aa3
commit eb6cbfe
Showing
12 changed files
with
260 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
server/src/main/kotlin/com/xebia/functional/xef/server/models/LoginResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.xebia.functional.xef.server.models | ||
|
||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class LoginResponse(val authToken: String) |
16 changes: 16 additions & 0 deletions
16
server/src/main/kotlin/com/xebia/functional/xef/server/models/Requests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.xebia.functional.xef.server.models | ||
|
||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class RegisterRequest( | ||
val name: String, | ||
val email: String, | ||
val password: String | ||
) | ||
|
||
@Serializable | ||
data class LoginRequest( | ||
val email: String, | ||
val password: String | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,9 @@ | |
height: 2rem; | ||
width: auto; | ||
} | ||
|
||
.panel-right { | ||
display: block; | ||
text-align: right; | ||
margin-left: auto; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.center { | ||
position: absolute; | ||
left: 50%; | ||
top: 20%; | ||
transform: translate(-50%, -50%); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { useAuth } from '@/state/Auth'; | ||
import { Box, Button, TextField, Typography } from '@mui/material'; | ||
import { ChangeEvent, useState } from 'react'; | ||
import { Navigate, useLocation, useNavigate } from 'react-router-dom'; | ||
import styles from './Login.module.css'; | ||
|
||
export function RequireAuth({ children }: { children: JSX.Element }) { | ||
let auth = useAuth(); | ||
let location = useLocation(); | ||
|
||
if (!auth.authToken) { | ||
return <Navigate to="/login" state={{ from: location }} replace />; | ||
} | ||
|
||
return children; | ||
} | ||
|
||
export function Login() { | ||
const navigate = useNavigate(); | ||
const location = useLocation(); | ||
const auth = useAuth(); | ||
|
||
const from = location.state?.from?.pathname || '/'; | ||
|
||
const [emailInput, setEmailInput] = useState<string>(''); | ||
const [passwordInput, setPasswordInput] = useState<string>(''); | ||
|
||
const handleSubmit = () => { | ||
auth.signin(emailInput, () => { | ||
navigate(from, { replace: true }); | ||
}); | ||
}; | ||
|
||
const emailHandleChange = (event: ChangeEvent<HTMLInputElement>) => { | ||
setEmailInput(event.target.value); | ||
}; | ||
|
||
const passwordHandleChange = (event: ChangeEvent<HTMLInputElement>) => { | ||
setPasswordInput(event.target.value); | ||
}; | ||
|
||
const disabledButton = passwordInput?.trim() == "" || emailInput?.trim() == ""; | ||
|
||
return ( | ||
<Box | ||
className={styles.center} | ||
> | ||
<Box | ||
sx={{ | ||
my: 1, | ||
}}> | ||
<Typography variant="h4" gutterBottom> | ||
Xef Server | ||
</Typography> | ||
</Box> | ||
<Box | ||
sx={{ | ||
my: 3, | ||
}}> | ||
<TextField | ||
id="email" | ||
label="Email" | ||
value={emailInput} | ||
onChange={emailHandleChange} | ||
size="small" | ||
sx={{ | ||
width: { xs: '100%', sm: 550 }, | ||
}} | ||
/> | ||
</Box> | ||
<Box | ||
sx={{ | ||
my: 3, | ||
}}> | ||
<TextField | ||
id="password" | ||
label="Password" | ||
value={passwordInput} | ||
onChange={passwordHandleChange} | ||
size="small" | ||
sx={{ | ||
width: { xs: '100%', sm: 550 }, | ||
}} | ||
/> | ||
</Box> | ||
<Button | ||
onClick={handleSubmit} | ||
variant="contained" | ||
disableElevation | ||
disabled={disabledButton}> | ||
<Typography variant="button">Login</Typography> | ||
</Button> | ||
</Box> | ||
); | ||
|
||
// return ( | ||
// <div> | ||
// <p>You must log in to view the page at {from}</p> | ||
|
||
// <form onSubmit={handleSubmit}> | ||
// <label> | ||
// Username: <input name="username" type="text" /> | ||
// </label>{' '} | ||
// <button type="submit">Login</button> | ||
// </form> | ||
// </div> | ||
// ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './Login'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export function Root() { | ||
return <>Root</>; | ||
return <>Dashboard</>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import React from 'react'; | ||
import { createContext, useState } from 'react'; | ||
|
||
export function useAuth() { | ||
return React.useContext(AuthContext); | ||
} | ||
|
||
interface AuthContextType { | ||
authToken: string; | ||
signin: (authToken: string, callback: VoidFunction) => void; | ||
signout: (callback: VoidFunction) => void; | ||
} | ||
|
||
const AuthContext = createContext<AuthContextType>(null!); | ||
|
||
function AuthProvider({ children }: { children: React.ReactNode }) { | ||
const [authToken, setAuthToken] = useState<string>(sessionStorage.getItem('authToken') || ''); | ||
|
||
const signin = (newAuthToken: string, callback: VoidFunction) => { | ||
setAuthToken(newAuthToken); | ||
sessionStorage.setItem('authToken', newAuthToken); | ||
callback(); | ||
}; | ||
|
||
const signout = (callback: VoidFunction) => { | ||
setAuthToken(''); | ||
sessionStorage.setItem('authToken', ''); | ||
callback(); | ||
}; | ||
|
||
const value = { authToken, signin, signout }; | ||
|
||
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>; | ||
} | ||
|
||
export { AuthContext, AuthProvider }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './AuthContext'; |