From ca24f89c6d4750eddab1b411dcc52bac7b13b269 Mon Sep 17 00:00:00 2001 From: sosokker Date: Sat, 28 Oct 2023 13:13:28 +0700 Subject: [PATCH] Add Sign Up --- backend/core/settings.py | 9 ++- frontend/src/App.jsx | 3 + frontend/src/api/axiosapi.jsx | 22 +++++- frontend/src/components/login.jsx | 1 + frontend/src/components/signup.jsx | 121 +++++++++++++++++++++++++++-- 5 files changed, 145 insertions(+), 11 deletions(-) diff --git a/backend/core/settings.py b/backend/core/settings.py index 0bdc71f..fa98958 100644 --- a/backend/core/settings.py +++ b/backend/core/settings.py @@ -32,7 +32,7 @@ ALLOWED_HOSTS = config('ALLOWED_HOSTS', default='*', cast=Csv()) # Application definition -SITE_ID = 3 +SITE_ID = 4 AUTHENTICATION_BACKENDS = ( 'django.contrib.auth.backends.ModelBackend', @@ -70,6 +70,8 @@ REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': [ 'rest_framework_simplejwt.authentication.JWTAuthentication', 'dj_rest_auth.jwt_auth.JWTCookieAuthentication', + 'rest_framework.authentication.BasicAuthentication', + 'rest_framework.authentication.SessionAuthentication', ] } @@ -85,9 +87,12 @@ SOCIALACCOUNT_PROVIDERS = { } } +CORS_ALLOW_CREDENTIALS = True +CORS_ALLOW_ALL_ORIGINS = False CORS_ALLOWED_ORIGINS = [ "http://localhost:8000", - "http://127.0.0.1:8000" + "http://127.0.0.1:8000", + "http://localhost:5173", ] MIDDLEWARE = [ diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx index fd764c6..3577faa 100644 --- a/frontend/src/App.jsx +++ b/frontend/src/App.jsx @@ -26,6 +26,7 @@ import './App.css'; import {BrowserRouter, Route, Routes, Link} from "react-router-dom"; import Login from "./components/login"; import TestAuth from './components/testAuth'; +import Signup from './components/signup'; const App = () => { return ( @@ -34,11 +35,13 @@ const App = () => {

This is Home page!

} /> }/> + }/> }/>
diff --git a/frontend/src/api/axiosapi.jsx b/frontend/src/api/axiosapi.jsx index b069f34..849ce09 100644 --- a/frontend/src/api/axiosapi.jsx +++ b/frontend/src/api/axiosapi.jsx @@ -83,11 +83,31 @@ const getGreeting = () => { }); } +const config = { + headers: { + "Content-Type": "application/json" + } +}; + +// Function to register +const createUser = async (formData) => { + try { + axios.defaults.withCredentials = true + const resposne = axios.post("http://localhost:8000/api/user/create/", formData); + // const response = await axiosInstance.post('/user/create/', formData); + return response.data; + } catch (error) { + throw error; + } +}; + + // Export the functions and Axios instance export default { axiosInstance, apiUserLogin, apiUserLogout, getGreeting: getGreeting, - googleLogin + googleLogin, + createUser }; diff --git a/frontend/src/components/login.jsx b/frontend/src/components/login.jsx index 2437b86..d4ab3fb 100644 --- a/frontend/src/components/login.jsx +++ b/frontend/src/components/login.jsx @@ -73,6 +73,7 @@ export default function Login() { history.push('/testAuth'); }).catch(err => { console.log('Login failed'); // Handle login failure + console.log(err) }); } diff --git a/frontend/src/components/signup.jsx b/frontend/src/components/signup.jsx index ff06b76..5c1bc7b 100644 --- a/frontend/src/components/signup.jsx +++ b/frontend/src/components/signup.jsx @@ -1,11 +1,116 @@ -import React from 'react' +import React, { useState } from 'react'; +import axiosapi from '../api/axiosapi'; +import TextField from '@material-ui/core/TextField'; +import Typography from '@material-ui/core/Typography'; +import CssBaseline from '@material-ui/core/CssBaseline'; +import Container from '@material-ui/core/Container'; +import Button from '@material-ui/core/Button'; +import { makeStyles } from '@material-ui/core/styles'; -function Signup() { - return ( -
+const useStyles = makeStyles((theme) => ({ + // Styles for various elements + paper: { + marginTop: theme.spacing(8), + display: 'flex', + flexDirection: 'column', + alignItems: 'center', + }, + avatar: { + margin: theme.spacing(1), + backgroundColor: theme.palette.secondary.main, + }, + form: { + width: '100%', + marginTop: theme.spacing(1), + }, + submit: { + margin: theme.spacing(3, 0, 2), + }, +})); -
- ) -} +const Signup = () => { + const classes = useStyles(); + const [formData, setFormData] = useState({ + email: '', + username: '', + password: '', + }); + const [error, setError] = useState(null); + const [isSubmitting, setIsSubmitting] = useState(false); -export default Signup \ No newline at end of file + const handleSubmit = async (e) => { + e.preventDefault(); + setIsSubmitting(true); + setError(null); + + try { + axiosapi.createUser(formData); + } catch (error) { + console.error('Error creating user:', error); + setError('Registration failed. Please try again.'); // Set an error message + } finally { + setIsSubmitting(false); + } + }; + + const handleChange = (e) => { + const { name, value } = e.target; + setFormData({ ...formData, [name]: value }); + }; + + return ( + + +
+ + Sign Up + +
+ + + + + + {error && {error}} +
+
+ ); +}; + +export default Signup;