mirror of
https://github.com/AmanTahiliani/PeerNotes.git
synced 2026-08-07 11:55:12 -04:00
add set cookie
This commit is contained in:
@@ -18,7 +18,9 @@ class LoginView(APIView):
|
|||||||
user = authenticate(username=username, password=password)
|
user = authenticate(username=username, password=password)
|
||||||
if user:
|
if user:
|
||||||
token, _ = Token.objects.get_or_create(user=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:
|
else:
|
||||||
return Response(
|
return Response(
|
||||||
{"error": "Invalid credentials"},
|
{"error": "Invalid credentials"},
|
||||||
|
|||||||
@@ -134,3 +134,7 @@ STATIC_URL = "static/"
|
|||||||
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
|
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
|
||||||
AUTH_USER_MODEL = "api.PeerUser"
|
AUTH_USER_MODEL = "api.PeerUser"
|
||||||
CORS_ALLOW_ALL_ORIGINS = True
|
CORS_ALLOW_ALL_ORIGINS = True
|
||||||
|
# CORS_ALLOWED_ORIGINS = [
|
||||||
|
# "http://localhost:5173"
|
||||||
|
# ]
|
||||||
|
CORS_ALLOW_CREDENTIALS = True
|
||||||
@@ -1,9 +1,28 @@
|
|||||||
import { Link } from "react-router-dom"
|
import { Link } from "react-router-dom"
|
||||||
import "../styles/Login.css"
|
import "../styles/Login.css"
|
||||||
|
import { FormEvent } from "react";
|
||||||
|
|
||||||
export default function Login() {
|
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 (
|
return (
|
||||||
<main>
|
<main>
|
||||||
<form className="login-container">
|
<form className="login-container" onSubmit={handleSubmit}>
|
||||||
<h1>Login</h1>
|
<h1>Login</h1>
|
||||||
<div className="form-input">
|
<div className="form-input">
|
||||||
<label htmlFor="username">Username</label>
|
<label htmlFor="username">Username</label>
|
||||||
|
|||||||
@@ -1,10 +1,36 @@
|
|||||||
import { Link } from "react-router-dom";
|
import { Link } from "react-router-dom";
|
||||||
import "../styles/Login.css"
|
import "../styles/Login.css"
|
||||||
|
import { FormEvent } from "react";
|
||||||
export default function Signup() {
|
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 (
|
return (
|
||||||
<main>
|
<main>
|
||||||
<form className="login-container">
|
<form className="login-container" onSubmit={handleSubmit}>
|
||||||
<h1>Sign Up</h1>
|
<h1>Sign Up</h1>
|
||||||
|
<div className="form-input">
|
||||||
|
<label htmlFor="email">Email</label>
|
||||||
|
<input type="email" name="email" id="email" />
|
||||||
|
</div>
|
||||||
<div className="form-input">
|
<div className="form-input">
|
||||||
<label htmlFor="username">Username</label>
|
<label htmlFor="username">Username</label>
|
||||||
<input type="text" name="username" id="username" />
|
<input type="text" name="username" id="username" />
|
||||||
|
|||||||
Reference in New Issue
Block a user