Commit 9dff0019 authored by Poojasri's avatar Poojasri
Browse files

login api implement and validation added

parent 66c3d61e
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -6,3 +6,6 @@ node_modules

# .env
*.env

#dist
dist
 No newline at end of file
+1302 −21

File changed.

Preview size limit exceeded, changes collapsed.

+6 −0
Original line number Diff line number Diff line
@@ -15,14 +15,20 @@
  "license": "ISC",
  "dependencies": {
    "@hono/node-server": "^1.12.2",
    "bcrypt": "^5.1.1",
    "dotenv": "^16.4.5",
    "drizzle-kit": "^0.24.2",
    "drizzle-orm": "^0.33.0",
    "hono": "^4.5.11",
    "passport": "^0.7.0",
    "passport-local": "^1.0.0",
    "pg": "^8.12.0",
    "zod": "^3.23.8"
  },
  "devDependencies": {
    "@types/bcrypt": "^5.0.2",
    "@types/passport": "^1.0.16",
    "@types/passport-local": "^1.0.38",
    "nodemon": "^3.1.4",
    "ts-node": "^10.9.2",
    "typescript": "^5.5.4"
+12 −0
Original line number Diff line number Diff line
import bcrypt from "bcrypt";

const verifyPassword = async (inputPassword: string, storedHash: string) => {
  return await bcrypt.compare(inputPassword, storedHash);
};

const hashPassword = async (password: string) => {
  const saltRounds = 10;
  return await bcrypt.hash(password, saltRounds);
};

export { verifyPassword, hashPassword };
+37 −1
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@ import { Context } from "hono";
import db from "../../db/db.connect";
import { users } from "./user.model";
import { eq } from "drizzle-orm";
import { hashPassword, verifyPassword } from "./password.utill";

export const registerUser = async (c: Context) => {
  const body = await c.req.json();
@@ -15,12 +16,14 @@ export const registerUser = async (c: Context) => {
    return c.json({ error: "Email already exists" }, 409);
  }

  const hashedPassword = await hashPassword(body.password);

  const newUser = {
    firstName: body.firstName,
    lastName: body.lastName,
    email: body.email,
    phoneNumber: body.phoneNumber,
    password: body.password, 
    password: hashedPassword, 
  };

  await db.insert(users).values(newUser);
@@ -28,4 +31,37 @@ export const registerUser = async (c: Context) => {
  return c.json({ message: "User registered successfully!" });
};

export const loginUser = async (c: Context) => {
  const { email, password } = await c.req.json();

  const existingUser = await db
    .select()
    .from(users)
    .where(eq(users.email, email));

  // If the user is not found, return a 404 response
  if (existingUser.length === 0) {
    return c.json({ error: "User not found" }, 404);
  }

  const user = existingUser[0];

  // Verify the provided password against the stored hashed password
  const isPasswordValid = await verifyPassword(password, user.password);
  if (!isPasswordValid) {
    return c.json({ error: "Invalid credentials" }, 401);
  }

  return c.json({
    status: "SUCCESS",
    message: "Login successful!",
    user: {
      id: user.id,
      email: user.email,
      firstName: user.firstName,
      lastName: user.lastName
    }
  });
};

Loading