This repository has been archived by the owner on Sep 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
6 changed files
with
102 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import {useCookies} from "react-cookie"; | ||
import {useEffect, useState} from "react"; | ||
|
||
const useToken = () => { | ||
const queryParams: URLSearchParams = new URLSearchParams(location.search); | ||
const ticket: string | null = queryParams.get('ticket'); | ||
const [cookies, setCookies] = useCookies(['token']); | ||
const [token, setToken] = useState(cookies['token']); | ||
useEffect(() => { | ||
if (ticket !== null) { | ||
const url: string = 'https://localhost:8080/api/login?ticket=' + ticket; | ||
fetch(url, {headers: {credentials: "include"}}) | ||
.then(async (data) => { | ||
const response: string = await data.text(); | ||
if (data.status == 200) { | ||
setCookies("token", response); | ||
setToken(response) | ||
} | ||
}) | ||
.catch(error => { | ||
// TODO error logging | ||
console.log(error); | ||
|
||
}); | ||
} | ||
}, [setToken, setCookies, ticket]); | ||
return [token] | ||
} | ||
|
||
export default useToken |
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,33 @@ | ||
import {useEffect, useState} from "react"; | ||
|
||
const useUser = () => { | ||
const [user, setUser] = useState(undefined) | ||
useEffect(() => { | ||
if (user == null) { | ||
fetch("https://localhost:8080/api/user", { | ||
headers: { | ||
credentials: "include", | ||
} | ||
}) | ||
.then(async response => { | ||
if (response.status == 200) { | ||
const text: string = await response.text(); | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
const user_json = JSON.parse(text); | ||
if (user_json != null) { | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment,@typescript-eslint/no-unsafe-argument | ||
setUser(user_json) | ||
} | ||
} else if (response.status == 401) { | ||
console.log("Unauthorized Caught!") | ||
} | ||
} | ||
).catch(error => { | ||
// TODO error handling | ||
console.log(error) | ||
}) | ||
} | ||
}, [setUser, user]); | ||
return [user] | ||
} | ||
export default useUser; |
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 |
---|---|---|
@@ -1,7 +1,21 @@ | ||
import {JSX} from "react"; | ||
import useToken from "../../hooks/Token.tsx"; | ||
import {Navigate} from "react-router-dom"; | ||
|
||
export default function LoginScreen(): JSX.Element { | ||
|
||
function LoginScreen(): JSX.Element { | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
const [token] = useToken() | ||
if (token !== undefined){ | ||
return (<Navigate to={"/"}/>) | ||
} | ||
return ( | ||
<>Login screen</> | ||
) | ||
} | ||
<div> | ||
<p> Please log in!</p> | ||
<a href="https://login.ugent.be/login?service=https://localhost:8080/login">Log in</a> | ||
{token && <Navigate to={"/"}/>} | ||
</div> | ||
); | ||
} | ||
|
||
export default LoginScreen |
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,10 @@ | ||
function LogoutScreen(): JSX.Element { | ||
return ( | ||
<div> | ||
<p>Click the link below to logout</p> | ||
<a href="https://login.ugent.be/logout">Log out</a> | ||
</div> | ||
); | ||
} | ||
|
||
export default LogoutScreen |
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