Skip to content
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

enchance the encrytion level and store the user data in database with… #41

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion models/User.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import mongoose from 'mongoose';
const mongoose = require("mongoose");

const UserSchema = new mongoose.Schema({
name: { type: String, required: true },
Expand Down
70 changes: 70 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"dependencies": {
"@next/font": "13.2.3",
"@reduxjs/toolkit": "^1.9.3",
"argon2": "^0.41.1",
"bcryptjs": "^2.4.3",
"crypto": "^1.0.1",
"eslint": "8.35.0",
Expand Down
19 changes: 8 additions & 11 deletions pages/api/auth/login.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import ConnectDB from '@/DB/connectDB';
import User from '@/models/User';
import Joi from 'joi';
import { compare } from 'bcryptjs';
import argon2 from 'argon2'; // Import Argon2 for password hashing
import jwt from 'jsonwebtoken';


const schema = Joi.object({
email: Joi.string().email().required(),
password: Joi.string().required(),
});




export default async (req, res) => {
await ConnectDB();

Expand All @@ -25,15 +21,16 @@ export default async (req, res) => {
const checkUser = await User.findOne({ email });
if (!checkUser) return res.status(401).json({ success: false, message: "Account not Found" });

const isMatch = await compare(password, checkUser.password);
// Use Argon2 to verify the password
const isMatch = await argon2.verify(checkUser.password, password);
if (!isMatch) return res.status(401).json({ success: false, message: "Incorrect Password" });

const token = jwt.sign({ id: checkUser._id, email: checkUser.email }, process.env.JWT_SECREAT, { expiresIn: '1d' });
const finalData = {token , user : checkUser}
return res.status(200).json({ success: true, message: "Login Successfull", finalData})
const token = jwt.sign({ id: checkUser._id, email: checkUser.email }, process.env.JWT_SECRET, { expiresIn: '1d' });
const finalData = { token, user: checkUser };
return res.status(200).json({ success: true, message: "Login Successful", finalData });

} catch (error) {
console.log('Error in register (server) => ', error);
return res.status(500).json({ success: false, message: "Something Went Wrong Please Retry Later !" })
console.error('Error in login (server) => ', error); // Log the error
return res.status(500).json({ success: false, message: "Something Went Wrong Please Retry Later !" });
}
}
14 changes: 11 additions & 3 deletions pages/api/auth/register.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import ConnectDB from '@/DB/connectDB';
import User from '@/models/User';
import Joi from 'joi';
import { hash } from 'bcryptjs';
import argon2 from 'argon2';


const schema = Joi.object({
Expand All @@ -27,13 +27,21 @@ export default async (req, res) => {
}

else {
const hashedPassword = await hash(password, 12)
// Use Argon2 to hash the password
const hashedPassword = await argon2.hash(password, {
type: argon2.argon2id,
memoryCost: 2 ** 16, // 64MB
timeCost: 5, // iterations
parallelism: 1 // Number of threads (1 in this case)
});

const createUser = await User.create({ email, name, password: hashedPassword });
return res.status(201).json({ success: true, message: "Account created successfully" });
}
} catch (error) {
console.log('Error in register (server) => ', error);
return res.status(500).json({ success: false, message: "Something Went Wrong Please Retry Later !" })
return res.status(500).json({ success: false, message: "Something Went Wrong Please Retry Later !" });
}
}