Skip to content

Commit

Permalink
Merge pull request #5 from VGandhi27/master
Browse files Browse the repository at this point in the history
cors commit
  • Loading branch information
thestarsahil authored Mar 19, 2023
2 parents 03aa428 + 4e07065 commit c111028
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 21 deletions.
7 changes: 7 additions & 0 deletions Server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ require('./models/User')

app.use(express.json())
app.use(require('./routes/Auth'))
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', 'http://localhost:4000');
next();
});
// app.use(cors({
// origin:"http://localhost:4000"
// }))

mongoose.connect(MONGOURI,{
useNewUrlParser:true,
Expand Down
8 changes: 4 additions & 4 deletions Server/models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ const userSchema=new mongoose.Schema({
type:String,
required:true
},
sc:{
type:String,
required:true
},
// sc:{
// type:String,
// required:true
// },
gender:{
type:String,
required:true
Expand Down
16 changes: 13 additions & 3 deletions Server/routes/Auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ const jwt=require('jsonwebtoken')
const { JWT_SECRET }=require("../Key")

router.post('/signup',(req,res)=>{
const {email,password,fname,lname,sc,date,gender}=req.body
if(!email||!password||!fname||!lname||!sc||!date||!gender){
const {email,password,fname,lname,date,gender}=req.body
if(!email||!password||!fname||!lname||!date||!gender){
return res.status(422).json({error:"Please add all the Credential"})
}
User.findOne({email:email})
Expand All @@ -23,7 +23,7 @@ router.post('/signup',(req,res)=>{
const user=new User({
email,
password:hashedpassword,
fname,lname,sc,date,gender
fname,lname,date,gender
})
user.save()
.then(user=>{
Expand Down Expand Up @@ -74,3 +74,13 @@ router.post('/signin',(req,res)=>{
})
})
module.exports=router










48 changes: 48 additions & 0 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "counsellingsystem",
"private": true,
"proxy": "http://localhost:4000",
"version": "0.0.0",
"type": "module",
"scripts": {
Expand All @@ -9,6 +10,7 @@
"preview": "vite preview"
},
"dependencies": {
"cors": "^2.8.5",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.9.0"
Expand Down
79 changes: 65 additions & 14 deletions src/components/SignUp.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import React, { useState } from 'react';
import {Link} from 'react-router-dom'
import {useNavigate} from 'react-router-dom'



const SignUpForm = () => {
Expand All @@ -8,17 +11,55 @@ const SignUpForm = () => {
const [password,setPassword]=useState('');
const [dob, setDob] = useState('');
const [gender, setGender] = useState('');
const [sc, setSc] = useState('');

let navigate =useNavigate();

function handleSubmit(event) {
event.preventDefault();
function handleSubmit(e) {
e.preventDefault();
console.log('First name:', firstName);
console.log('Surname:', surname);
console.log('Email address:', email);
console.log('Password:', password);
console.log('DOB:', dob);
console.log('Gender:', gender);
console.log('Gender:', gender);}

const PostData =()=>{
if(!/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(email)){
console.log({html: "Invalid Email", classes:"#d32f2f red darken-2"})
return
}
fetch("http://localhost:4000/signup",{
method:"post",
"Access-Control-Allow-Origin":"http://localhost:4000/signup",
headers:{
"Content-Type":"application/json",
// 'Access-Control-Allow-Origin': '/api/signup',
"Access-Control-Allow-Origin":"http://localhost:4000/signup"
},
body:JSON.stringify({
fname:firstName,
lname:surname,
email,
date:dob,
gender,
sc,
password

})
}).then(res=>res.json())
.then(data=>{
if(data.error){
console.log({html: data.error, classes:"#d32f2f red darken-2"})
}else{
// console.log({html: data.message, classes:"#43a047 green darken-1"})
console.log(data)
navigate("/signin");
}
})

}

return (
<div className="signup-container">
<div className="shadow-box">
Expand All @@ -30,34 +71,36 @@ const SignUpForm = () => {



<form className="form-container" onSubmit={handleSubmit}>
<form className="form-container"
onSubmit={handleSubmit}
>
<input
type="text"
value={firstName}
onChange={(event) => setFirstName(event.target.value)}
onChange={(e) => setFirstName(e.target.value)}
placeholder="First Name"
className='firstname-text'
/>

<input
type="text"
value={surname}
onChange={(event) => setSurname(event.target.value)}
onChange={(e) => setSurname(e.target.value)}
placeholder="Surname"
className='surname-text'
/>

<input
type="email"
value={email}
onChange={(event)=>setEmail(event.target.value)}
onChange={(e)=>setEmail(e.target.value)}
placeholder="Email address"
/>

<input
type="password"
value={password}
onChange={(event)=>setPassword(event.target.value)}
onChange={(e)=>setPassword(e.target.value)}
placeholder="Password"
className='password-text'
/>
Expand All @@ -66,14 +109,14 @@ const SignUpForm = () => {
<input
type="date"
value={dob}
onChange={(event)=>setDob(event.target.value)}
onChange={(e)=>setDob(e.target.value)}
className='dob'
/>

<select
type="gender"
value={gender}
onChange={(event)=>setGender(event.target.value)}
onChange={(e)=>setGender(e.target.value)}
>
<option value="">Select Gender</option>
<option value="Male">Male</option>
Expand All @@ -82,10 +125,18 @@ const SignUpForm = () => {
</select>

<label htmlFor="are-you-student-or-counsellor">Are you Student and Counsellor ? </label>
<label htmlFor="student-option">Student</label>
<label htmlFor="counsellor-option">Counsellor</label>
<label htmlFor="student-option"
// value={sc="stu"}
// onChange={(e)=>setSc(e.target.value)}
// placeholder="SC"
>Student</label>
<label htmlFor="counsellor-option"
// value={sc="cou"}
// onChange={(e)=>setSc(e.target.value)}
// placeholder="SC"
>Counsellor</label>

<button type="submit" className='submit-button'>Sign Up</button>
<button type="submit" className='submit-button' onClick={()=>PostData()}>Sign Up</button>


</form>
Expand Down
4 changes: 4 additions & 0 deletions vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@ import react from '@vitejs/plugin-react'

// https://vitejs.dev/config/
export default defineConfig({
// server:{
// "proxy": {
// "/api":"http://localhost:4000"}
// },
plugins: [react()]
})

0 comments on commit c111028

Please sign in to comment.