Skip to content

Commit

Permalink
Global State Added, Logout Fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
Alberto-Guerra committed Mar 6, 2024
1 parent 17a0131 commit 80b10b3
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 26 deletions.
38 changes: 37 additions & 1 deletion webapp/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion webapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^3.5.1"
"web-vitals": "^3.5.1",
"zustand": "^4.5.2"
},
"scripts": {
"start": "react-scripts start",
Expand Down
24 changes: 15 additions & 9 deletions webapp/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import React, { useState } from 'react';
import React, { useEffect } from 'react';
import Game from './components/Game/Game';
import { isLogged } from './services/auth-service';
import { loginWithToken } from './services/auth-service';
import Authentication from './components/auth/Authentication';



import { useUserStore } from './stores/user-store';

function App() {
const [isLoggedState, setIsLoggedState] = useState(isLogged());

const user = useUserStore(state => state.user);

useEffect(() => {
loginWithToken();
}, []);

if (!isLoggedState) {
return <Authentication setIsLoggedState={setIsLoggedState}/>
if (user == null) {
return <Authentication/>
}
else {
return <Game />
}
return <Game />

}

export default App;
7 changes: 5 additions & 2 deletions webapp/src/components/Game/Game.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useState } from "react";
import Question from "./Question";
import NextQuestion from "./NextQuestion";
import AnswerPanel from "./AnswerPanel";
import { logout } from "../../services/auth-service";
import { logout, getUsername } from "../../services/auth-service";

export default function Game() {
const [answered, setAnswered] = useState(false);
Expand Down Expand Up @@ -30,7 +30,10 @@ export default function Game() {

return (
<div id='mainContainer' className='flex flex-col h-screen '>
<div> <button onClick={() => logout()}>logout</button></div>
<div className="flex justify-between">
<h1> {getUsername()}</h1>
<button onClick={() => logout()}>logout</button>
</div>
<div id='pregunta' className='bg-purple-400 h-1/2 border-4 border-purple-700 flex-1'>
<div className="flex justify-between">
<text className='text-white text-2xl p-8'> Score: {score} </text>
Expand Down
7 changes: 2 additions & 5 deletions webapp/src/components/auth/Authentication.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ import React, { useState } from "react";
import AddUser from "./AddUser";
import Login from "./Login";

type props = {
setIsLoggedState: (isLogged: boolean) => void;
}

const Authentication = (props : props) => {
const Authentication = () => {
const [showLogin, setShowLogin] = useState(true);


Expand All @@ -18,7 +15,7 @@ const Authentication = (props : props) => {

<div className="container mx-auto">

{showLogin ? <Login setIsLoggedState={props.setIsLoggedState} /> : <AddUser />}
{showLogin ? <Login /> : <AddUser />}
<div className="text-center mt-8">
{showLogin ? (
<button
Expand Down
7 changes: 1 addition & 6 deletions webapp/src/components/auth/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@
import React, { useState } from 'react';
import {login} from '../../services/auth-service';

type props = {
setIsLoggedState: (isLogged: boolean) => void;
}


const Login = (props:props) => {
const Login = () => {
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
const [error, setError] = useState('');
Expand All @@ -17,8 +14,6 @@ const Login = (props:props) => {
const response = await login(username, password);
if (!response) {
setError("No funca el login");
} else {
props.setIsLoggedState(true);
}

}
Expand Down
14 changes: 12 additions & 2 deletions webapp/src/services/auth-service.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
import axios from 'axios';
import { jwtDecode } from "jwt-decode";
import { useUserStore } from '../stores/user-store';

const API_URL = 'http://localhost:8002';

type JwtPayload = {
export type JwtPayload = {
username: string;
userEmail: string;
questions_answered: number;
correctly_answered_questions: number;
};

export const loginWithToken = () => {
const tokenInfo = getTokenInfo();
if(tokenInfo) {
useUserStore.getState().setUser(tokenInfo);
}
}

export const login = async (username: string, password: string)=> {
try {
const response = await axios.post(`${API_URL}/login`, { username, password });
const token = response.data.token;
console.log('token:', token);
localStorage.setItem('token', token); // store the token in local storage
loginWithToken();
return true;
} catch (error) {
console.error('Error during login:', error);
Expand All @@ -41,7 +50,8 @@ export const isLogged = () => {
};

export const logout = () => {
localStorage.removeItem('token');
localStorage.removeItem('token');
useUserStore.getState().logout();
};

export const getTokenInfo = (): JwtPayload | null => {
Expand Down
16 changes: 16 additions & 0 deletions webapp/src/stores/user-store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

import {create} from 'zustand';
import { JwtPayload } from '../services/auth-service';

interface UserState {
user: JwtPayload | null,
setUser: (user : JwtPayload) => void,
logout: () => void,
}


export const useUserStore = create<UserState>((set) => ({
user: null,
setUser: (user) => set({user}),
logout: () => set({user:null})
}));

0 comments on commit 80b10b3

Please sign in to comment.