Skip to content

Commit

Permalink
Feat: Added button to validate user's token, added better logout system
Browse files Browse the repository at this point in the history
  • Loading branch information
Virus288 committed Dec 2, 2024
1 parent 9a6056e commit 57b2686
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 6 deletions.
2 changes: 1 addition & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ export default [...fixupConfigRules(compat.extends(
"no-constant-binary-expression": 0,
"no-constant-condition": 2,

"no-console": [2, {
"no-console": [0, {
allow: ["info", "trace"],
}],

Expand Down
1 change: 1 addition & 0 deletions src/Router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const Router: React.FC = () => {
<Routes location={location} key={location.pathname}>
<Route path="/" element={<Components.Home />} />
<Route path="/login" element={<Components.Login />} />
<Route path="/logout" element={<Components.Logout />} />
<Route path="/register" element={<Components.Register />} />
<Route path="*" element={<Components.FourOhFour />} />
</Routes>
Expand Down
21 changes: 17 additions & 4 deletions src/components/generic/views/Home.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
import React from 'react';
import React, { useState } from 'react';
import { HomeBody, HomeRow } from '../styled';
import { Button } from '../../customs/buttons';
import { sendToLoginPage, sendToRegisterPage, sendToLogoutPage } from '../../../controllers';
import { sendToLoginPage, sendToRegisterPage, sendToLogoutPage, getUserinfo } from '../../../controllers';

const Home: React.FC = () => {
return (
const [userinfo, setUserinfo] = useState<string | null>(null)

const handleGetUserInfo = () => {
getUserinfo().then((login) => {
setUserinfo(login)
}).catch(err => {
console.log("Got err while fetching user data", err)
setUserinfo((err as Error).message)
})
}

return (
<HomeBody>
<header>
This is super simple frontend client created for testing purposes of my authorizations service. This is intended
to be modified and only used as a template
to be modified and only used as a template.
</header>
{userinfo ? `Logged in as ${userinfo}` : null}
<HomeRow>
<Button onClick={() => sendToRegisterPage()}>Register</Button>
<Button onClick={() => sendToLoginPage()}>Log in</Button>
<Button onClick={() => sendToLogoutPage()}>Log out</Button>
<Button onClick={() => handleGetUserInfo()}>Check if logged in</Button>
</HomeRow>
</HomeBody>
);
Expand Down
3 changes: 2 additions & 1 deletion src/components/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ import FourOhFour from './generic/views/FourOhFour.tsx';
import Home from './generic/views/Home';
import Login from './login/views/Login.tsx';
import Register from './register/views/Register.tsx';
import Logout from './logout/views/Logout.tsx'

export { FourOhFour, Home, Login, Register };
export { FourOhFour, Home, Login, Register, Logout };
21 changes: 21 additions & 0 deletions src/controllers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,24 @@ export const sendToLogoutPage = (): void => {

window.location.href = `${server}/user/logout/start?${queryParams}`;
};

export const getUserinfo = async (): Promise<string> => {
const server = import.meta.env.VITE_API_BACKEND as string;
const homeUrl = import.meta.env.VITE_API_HOME as string;

const res = await fetch(`${server}/user/validate`, {
method: 'GET',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': homeUrl,
},
});

if (res.ok) {
return ((await res.json()) as { data: { login: string } }).data.login;
}

console.log('Got error', await res.json());
return 'None. User is not logged in, or something is wrong';
};

0 comments on commit 57b2686

Please sign in to comment.