Skip to content
This repository has been archived by the owner on Sep 27, 2024. It is now read-only.

Commit

Permalink
start login
Browse files Browse the repository at this point in the history
  • Loading branch information
cstefc committed Mar 10, 2024
1 parent a2c0492 commit c74fa7e
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 8 deletions.
30 changes: 30 additions & 0 deletions frontend/src/hooks/Token.tsx
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
33 changes: 33 additions & 0 deletions frontend/src/hooks/User.tsx
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;
4 changes: 4 additions & 0 deletions frontend/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ const router = createBrowserRouter([
{
path: "/teacher",
element: <HomeTeacher/>
},
{
path: "/logout",
element: <LogoutScreen/>
}
]);
ReactDOM.createRoot(document.getElementById('root')!).render(
Expand Down
24 changes: 19 additions & 5 deletions frontend/src/pages/login/LoginScreen.tsx
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
10 changes: 10 additions & 0 deletions frontend/src/pages/login/LogoutScreen.tsx
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
9 changes: 6 additions & 3 deletions frontend/src/pages/student/HomeStudent.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import {JSX} from "react";
import {Header} from "../../components/Header.tsx";
import {Sidebar} from "../../components/Sidebar.tsx";
import useUser from "../../hooks/User.tsx";

export default function HomeStudent(): JSX.Element {
const [user] = useUser()

return (
<>
<div className={"main-header"}>
Expand All @@ -12,9 +15,9 @@ export default function HomeStudent(): JSX.Element {
<div className={"side-bar is-flex is-justify-content-center"}>
<Sidebar/>
</div>
<div>
<>Homescreen for a student</>
</div>
{user && <div>
<>Homescreen for a student {user["name"]}</>
</div>}
</div>
</>
)
Expand Down

0 comments on commit c74fa7e

Please sign in to comment.