Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"@testing-library/react": "^13.2.0",
"@testing-library/user-event": "^13.5.0",
"axios": "^0.27.2",
"bcryptjs": "^3.0.2",
"react": "^18.1.0",
"react-dom": "^18.1.0",
"react-redux": "^8.0.2",
Expand Down
34 changes: 19 additions & 15 deletions client/src/componets/Login.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Box, Button, TextField, Typography } from "@mui/material";
import React, { useEffect, useState } from "react";
import axios from "axios";
import bcrypt from 'bcryptjs';
import { useDispatch } from "react-redux";
import { authActions } from "../store";
import { useNavigate, useLocation } from "react-router-dom";
Expand All @@ -27,33 +28,36 @@ const Login = () => {
useEffect(() => {
setIsSignup(isSignupButtonPressed);
}, [isSignupButtonPressed]);
const sendRequest = async (type = "login") => {
console.log("inside send req");
console.log(`${config.BASE_URL}/api/users/${type}`);
const res = await axios
.post(`${config.BASE_URL}/api/users/${type}`, {
name: inputs.name,
email: inputs.email,
password: inputs.password,
})
.catch((err) => console.log(err));

const sendRequest = async (type = "login",dataToSend) => {

let res;
try {
res = await axios.post(`${config.BASE_URL}/api/users/${type}`, dataToSend)
} catch (err) {
console.error("Signup error:", err);
return;
}

const data = await res.data;
console.log("return");
console.log(data);
return data;
};

const handleSubmit = (e) => {
e.preventDefault();
console.log(inputs);
const salt = bcrypt.genSaltSync(10);
const hashedPassword = bcrypt.hashSync(inputs.password, salt);
const dataToSend = {
...inputs,
password: hashedPassword,
};
if (isSignup) {
sendRequest("signup")
sendRequest("signup", dataToSend)
.then((data) => localStorage.setItem("userId", data.user._id))
.then(() => dispath(authActions.login()))
.then(() => naviagte("/blogs"));
} else {
sendRequest()
sendRequest("login", dataToSend)
.then((data) => localStorage.setItem("userId", data.user._id))
.then(() => dispath(authActions.login()))
.then(() => naviagte("/blogs"));
Expand Down
2 changes: 2 additions & 0 deletions server/config/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ mongoose.set('strictQuery', false);
mongoose.connect("mongodb://127.0.0.1:27017/BlogApp").then(()=>{
console.log("connected!");
}).catch((err)=>{
console.log("error in mongodb connection");

console.log(err);
})
15 changes: 10 additions & 5 deletions server/controller/user-contoller.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,36 @@ const getAllUser = async(req,res,next) =>{
}

const signUp = async(req,res,next) =>{
console.log("Received signup data:", req.body);
const { name , email , password } = req.body;

let existingUser;

try{
existingUser = await User.findOne({email})
}catch(e){
console.log(err);
console.log(e);
return res.status(500).json({ message: "Error checking existing user" });
}

if(existingUser){
if(existingUser){ console.log("here");
return res.status(400).json({message : "User is already exists!"})
}
const hashedPassword = bcrypt.hashSync(password);

const user = new User({
name,email,
password: hashedPassword,
blogs: []
});

try{
user.save();
await user.save();
return res.status(201).json({ user })
}
catch(e){console.log(e);}
catch(e){console.log(e);
return res.status(500).json({ message: "Signup failed!" });
}
}

const logIn = async(req,res,next) => {
Expand Down