From 04d8c8504ccee8ffef6920eb99ece37d4035b69b Mon Sep 17 00:00:00 2001 From: afazio1 Date: Thu, 11 Apr 2024 02:34:32 -0400 Subject: [PATCH] add set cookie --- backend/api/user_login.py | 4 +++- backend/peer_notes/settings.py | 4 ++++ frontend/src/screens/Login.tsx | 21 ++++++++++++++++++++- frontend/src/screens/Signup.tsx | 28 +++++++++++++++++++++++++++- 4 files changed, 54 insertions(+), 3 deletions(-) diff --git a/backend/api/user_login.py b/backend/api/user_login.py index 1a8becb..4cd8ff5 100644 --- a/backend/api/user_login.py +++ b/backend/api/user_login.py @@ -18,7 +18,9 @@ class LoginView(APIView): user = authenticate(username=username, password=password) if user: token, _ = Token.objects.get_or_create(user=user) - return Response({"token": token.key}) + response = Response({"token": token.key}, status=status.HTTP_200_OK) + response.set_cookie("token", token.key) + return response else: return Response( {"error": "Invalid credentials"}, diff --git a/backend/peer_notes/settings.py b/backend/peer_notes/settings.py index 474b470..2418f69 100644 --- a/backend/peer_notes/settings.py +++ b/backend/peer_notes/settings.py @@ -134,3 +134,7 @@ STATIC_URL = "static/" DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" AUTH_USER_MODEL = "api.PeerUser" CORS_ALLOW_ALL_ORIGINS = True +# CORS_ALLOWED_ORIGINS = [ +# "http://localhost:5173" +# ] +CORS_ALLOW_CREDENTIALS = True \ No newline at end of file diff --git a/frontend/src/screens/Login.tsx b/frontend/src/screens/Login.tsx index 3fa1142..928ef90 100644 --- a/frontend/src/screens/Login.tsx +++ b/frontend/src/screens/Login.tsx @@ -1,9 +1,28 @@ import { Link } from "react-router-dom" import "../styles/Login.css" +import { FormEvent } from "react"; + export default function Login() { + function handleSubmit(e: FormEvent) { + e.preventDefault(); + const formData = new FormData(e.target as HTMLFormElement); + const username = formData.get('username'); + const password = formData.get('password'); + fetch('http://localhost:8000/api/login/', { + method: 'POST', + credentials: 'include', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ username, password }), + }) + .catch((error) => { + console.error('Error:', error); + }); + } return (
-
+

Login

diff --git a/frontend/src/screens/Signup.tsx b/frontend/src/screens/Signup.tsx index 1f1fbb2..c207581 100644 --- a/frontend/src/screens/Signup.tsx +++ b/frontend/src/screens/Signup.tsx @@ -1,10 +1,36 @@ import { Link } from "react-router-dom"; import "../styles/Login.css" +import { FormEvent } from "react"; export default function Signup() { + function handleSubmit(e: FormEvent) { + e.preventDefault(); + const formData = new FormData(e.target as HTMLFormElement); + const email = formData.get('email'); + const username = formData.get('username'); + const password = formData.get('password'); + fetch('http://localhost:8000/api/signup/', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ email, username, password }), + }) + .then(response => response.json()) + .then(data => { + console.log('Success:', data); + }) + .catch((error) => { + console.error('Error:', error); + }); + } return (
- +

Sign Up

+
+ + +