-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6162212
commit 48b1670
Showing
6 changed files
with
119 additions
and
634 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
FROM node:18-alpine | ||
FROM node:22-alpine | ||
|
||
# Pass environment variables as build arguments | ||
ARG PORT | ||
ARG MONGO_URI | ||
ARG JWT_KEY | ||
# Pass environment variables as build arguments | ||
ARG PORT | ||
ARG MONGO_URI | ||
ARG JWT_KEY | ||
|
||
# Optionally, you can export these as environment variables | ||
ENV PORT=${PORT} | ||
ENV MONGO_URI=${MONGO_URI} | ||
ENV JWT_KEY=${JWT_KEY} | ||
# Export build arguments as environment variables | ||
ENV PORT=${PORT} | ||
ENV MONGO_URI=${MONGO_URI} | ||
ENV JWT_KEY=${JWT_KEY} | ||
|
||
COPY package*.json ./ | ||
COPY package*.json ./ | ||
|
||
RUN npm install | ||
RUN npm install | ||
|
||
COPY . . | ||
COPY . . | ||
|
||
EXPOSE 5000 | ||
EXPOSE 5000 | ||
|
||
CMD ["npm", "start"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,89 @@ | ||
import { User } from "../models/userModel.js"; | ||
import validator from "validator"; | ||
import bcrypt from "bcrypt"; | ||
import { createToken } from "../utils/util.js"; | ||
import { User } from "../models/userModel.js"; | ||
import validator from "validator"; | ||
import bcrypt from "bcryptjs"; // Change to bcryptjs | ||
import { createToken } from "../utils/util.js"; | ||
|
||
export const createUser = async (req, res) => { | ||
const { name, email, password } = req.body; | ||
if (!name || !email || !password) { | ||
return res.status(400).json({ message: "Please fill all fields" }); | ||
} | ||
if (validator.isEmail(email) === false) { | ||
return res.status(400).json({ message: "Invalid email" }); | ||
} | ||
if (validator.isStrongPassword(password) === false) { | ||
return res.status(400).json({ message: "Password is not strong enough" }); | ||
} | ||
try { | ||
let existingUser = await User.findOne({ email }); | ||
if (existingUser) { | ||
return res.status(400).json({ message: "User already exists" }); | ||
} | ||
export const createUser = async (req, res) => { | ||
const { name, email, password } = req.body; | ||
if (!name || !email || !password) { | ||
return res.status(400).json({ message: "Please fill all fields" }); | ||
} | ||
if (validator.isEmail(email) === false) { | ||
return res.status(400).json({ message: "Invalid email" }); | ||
} | ||
if (validator.isStrongPassword(password) === false) { | ||
return res.status(400).json({ message: "Password is not strong enough" }); | ||
} | ||
try { | ||
let existingUser = await User.findOne({ email }); | ||
if (existingUser) { | ||
return res.status(400).json({ message: "User already exists" }); | ||
} | ||
|
||
const salt = await bcrypt.genSalt(10); | ||
const hashedPassword = await bcrypt.hash(password, salt); | ||
const user = await User.create({ name, email, password: hashedPassword }); | ||
const token = createToken(user._id); | ||
const salt = await bcrypt.genSalt(10); // No change needed here | ||
const hashedPassword = await bcrypt.hash(password, salt); // No change needed here | ||
const user = await User.create({ name, email, password: hashedPassword }); | ||
const token = createToken(user._id); | ||
|
||
res | ||
.status(201) | ||
.json({ id: user._id, name: user.name, email: user.email, token }); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(500).json({ message: "Server Error" }); | ||
} | ||
}; | ||
res | ||
.status(201) | ||
.json({ id: user._id, name: user.name, email: user.email, token }); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(500).json({ message: "Server Error" }); | ||
} | ||
}; | ||
|
||
export const loginUser = async (req, res) => { | ||
const { email, password } = req.body; | ||
if (!email || !password) { | ||
return res.status(400).json({ message: "Please fill all fields" }); | ||
} | ||
if (validator.isEmail(email) === false) { | ||
return res.status(400).json({ message: "Invalid email" }); | ||
} | ||
try { | ||
let user = await User.findOne({ | ||
email, | ||
}); | ||
if (!user) { | ||
return res.status(400).json({ message: "Invalid credentials" }); | ||
} | ||
const isMatch = await bcrypt.compare(password, user.password); | ||
if (!isMatch) { | ||
return res.status(400).json({ message: "Invalid credentials" }); | ||
} | ||
const token = createToken(user._id); | ||
res.json({ id: user._id, name: user.name, email: user.email, token }); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(500).json({ message: "Server Error" }); | ||
} | ||
}; | ||
export const loginUser = async (req, res) => { | ||
const { email, password } = req.body; | ||
if (!email || !password) { | ||
return res.status(400).json({ message: "Please fill all fields" }); | ||
} | ||
if (validator.isEmail(email) === false) { | ||
return res.status(400).json({ message: "Invalid email" }); | ||
} | ||
try { | ||
let user = await User.findOne({ | ||
email, | ||
}); | ||
if (!user) { | ||
return res.status(400).json({ message: "Invalid credentials" }); | ||
} | ||
const isMatch = await bcrypt.compare(password, user.password); // No change needed here | ||
if (!isMatch) { | ||
return res.status(400).json({ message: "Invalid credentials" }); | ||
} | ||
const token = createToken(user._id); | ||
res.json({ id: user._id, name: user.name, email: user.email, token }); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(500).json({ message: "Server Error" }); | ||
} | ||
}; | ||
|
||
export const findOneUser = async (req, res) => { | ||
const id = req.params.userId; | ||
try { | ||
const user = await User.findById(id); | ||
res.json({ | ||
id: user._id, | ||
name: user.name, | ||
email: user.email, | ||
creaatedAt: user.createdAt, | ||
updatedAt: user.updatedAt, | ||
}); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(500).json({ message: "Server Error" }); | ||
} | ||
}; | ||
export const findOneUser = async (req, res) => { | ||
const id = req.params.userId; | ||
try { | ||
const user = await User.findById(id); | ||
res.json({ | ||
id: user._id, | ||
name: user.name, | ||
email: user.email, | ||
createdAt: user.createdAt, | ||
updatedAt: user.updatedAt, | ||
}); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(500).json({ message: "Server Error" }); | ||
} | ||
}; | ||
|
||
export const findAllUsers = async (req, res) => { | ||
try { | ||
const users = await User.find(); | ||
res.json(users); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(500).json({ message: "Server Error" }); | ||
} | ||
export const findAllUsers = async (req, res) => { | ||
try { | ||
const users = await User.find(); | ||
res.json(users); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(500).json({ message: "Server Error" }); | ||
} | ||
}; |
Oops, something went wrong.