Skip to content

Fixes #38: Password hashed and encrypted #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
32 changes: 20 additions & 12 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,17 +28,18 @@ const Login = () => {
useEffect(() => {
setIsSignup(isSignupButtonPressed);
}, [isSignupButtonPressed]);
const sendRequest = async (type = "login") => {

const sendRequest = async (type = "login",dataToSend) => {
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));

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);
Expand All @@ -46,14 +48,20 @@ const Login = () => {

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,
};
console.log(dataToSend);
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