From e031fa3f7edcecd94d6cb97f802b743e048ff8dd Mon Sep 17 00:00:00 2001 From: Kitty-001 Date: Tue, 18 Jun 2024 16:54:23 +0800 Subject: [PATCH] fixing invalid account login bug --- .../0002_alter_transaction_update_wallet.py | 18 ++++++++++++++++++ backend/budget_tracking/models.py | 1 + backend/users/urls.py | 1 + backend/users/views.py | 12 +++++++++++- frontend/src/App.js | 18 ++++++++++++++++++ frontend/src/components/AppNavBar.js | 4 +++- frontend/src/contexts/AuthContext.js | 1 - frontend/src/pages/Login.js | 3 ++- 8 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 backend/budget_tracking/migrations/0002_alter_transaction_update_wallet.py diff --git a/backend/budget_tracking/migrations/0002_alter_transaction_update_wallet.py b/backend/budget_tracking/migrations/0002_alter_transaction_update_wallet.py new file mode 100644 index 0000000..1527bdc --- /dev/null +++ b/backend/budget_tracking/migrations/0002_alter_transaction_update_wallet.py @@ -0,0 +1,18 @@ +# Generated by Django 5.0.6 on 2024-06-18 08:42 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('budget_tracking', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='transaction', + name='update_wallet', + field=models.BooleanField(default=True), + ), + ] diff --git a/backend/budget_tracking/models.py b/backend/budget_tracking/models.py index b562f58..c2c1064 100644 --- a/backend/budget_tracking/models.py +++ b/backend/budget_tracking/models.py @@ -231,6 +231,7 @@ def create_from_json(data: dict, user_pk: int) -> 'Transaction': if data.get("type"): transaction.type = data.get("type") + if data.get("date"): date_string = data.get("date") if isinstance(date_string, datetime): diff --git a/backend/users/urls.py b/backend/users/urls.py index cd260e8..e2bd18b 100644 --- a/backend/users/urls.py +++ b/backend/users/urls.py @@ -6,4 +6,5 @@ urlpatterns = [ path('signup/', views.signup, name="signup"), + path('verify/', views.verification, name="verification"), ] \ No newline at end of file diff --git a/backend/users/views.py b/backend/users/views.py index 3acd7fc..6ea1b20 100644 --- a/backend/users/views.py +++ b/backend/users/views.py @@ -6,7 +6,7 @@ from django.contrib.auth import get_user_model import random from rest_framework.decorators import permission_classes, api_view -from rest_framework.permissions import AllowAny +from rest_framework.permissions import AllowAny, IsAuthenticated from rest_framework_simplejwt.tokens import RefreshToken @@ -37,3 +37,13 @@ def signup(request): return JsonResponse({'error': str(e)}, status=405) # Create your views here. + + +@api_view(['GET']) +@permission_classes([IsAuthenticated]) +def verification(request): + if request.method == 'GET': + return JsonResponse({ + 'success': "valid account, persist loggedin", + }) + \ No newline at end of file diff --git a/frontend/src/App.js b/frontend/src/App.js index c1d0500..34f6973 100644 --- a/frontend/src/App.js +++ b/frontend/src/App.js @@ -10,8 +10,26 @@ import "react-toastify/dist/ReactToastify.css"; import ResetPassword from "./pages/ResetPassword"; import Profile from "./pages/Profile"; import Protected from "./pages/Protected"; +import { useEffect } from "react"; +import useAxiosPrivate from "./hooks/useAxiosPrivate"; +import { useAuth } from "./contexts/AuthContext"; const App = () => { + const apiPrivate = useAxiosPrivate(); + const { Logout } = useAuth(); + + useEffect(() => { + const verify = async () => { + try { + const response = await apiPrivate.get('/users/verify/'); + } catch (error) { + console.log("Invalid credential: " + error); + Logout(); + } + }; + + verify(); + },[]); return (
diff --git a/frontend/src/components/AppNavBar.js b/frontend/src/components/AppNavBar.js index 307c83a..2084284 100644 --- a/frontend/src/components/AppNavBar.js +++ b/frontend/src/components/AppNavBar.js @@ -3,6 +3,7 @@ import UserIcon from "../images/user.png"; import { useNavigate } from "react-router-dom"; import CashFlowIcon from "../images/cashflow.png" import { useAuth } from "../contexts/AuthContext"; +import { toast } from "react-toastify"; const AppNavBar = () => { let navigate = useNavigate(); @@ -10,7 +11,8 @@ const AppNavBar = () => { const { isLoggedIn, user, Logout } = useAuth(); const handleLogout = () => { - Logout(localStorage.getItem("id")); + Logout(); + toast.success("Logout Successfully!"); navigate("/"); }; diff --git a/frontend/src/contexts/AuthContext.js b/frontend/src/contexts/AuthContext.js index dcc63ac..32a6022 100644 --- a/frontend/src/contexts/AuthContext.js +++ b/frontend/src/contexts/AuthContext.js @@ -64,7 +64,6 @@ const AuthContextProvider = ({ children }) => { localStorage.setItem("isLoggedIn", "true"); setUser(decodedToken); setIsLoggedIn(true); - console.log(authTokens.access); }; diff --git a/frontend/src/pages/Login.js b/frontend/src/pages/Login.js index bfbbc2a..e322e9d 100644 --- a/frontend/src/pages/Login.js +++ b/frontend/src/pages/Login.js @@ -27,9 +27,10 @@ const Login = () => { console.log("Login response get!"); const { access, refresh } = response.data; Login({ access, refresh }); + toast.success("Login Successfully!"); } catch (error) { console.error('Login failed: ', error); - toast.error("Login failed: Invalid email or password!") + toast.error("Login failed: Invalid email or password!"); } };