Skip to content

Commit

Permalink
add register controller and bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
hiteshchoudhary committed Nov 20, 2023
1 parent acdad8d commit 12e4963
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 6 deletions.
69 changes: 66 additions & 3 deletions src/controllers/user.controller.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,73 @@
import { asyncHandler } from "../utils/asyncHandler.js";

import {ApiError} from "../utils/ApiError.js"
import { User} from "../models/user.model.js"
import {uploadOnCloudinary} from "../utils/cloudinary.js"
import { ApiResponse } from "../utils/ApiResponse.js";

const registerUser = asyncHandler( async (req, res) => {
res.status(500).json({
message: "chai aur code"
// get user details from frontend
// validation - not empty
// check if user already exists: username, email
// check for images, check for avatar
// upload them to cloudinary, avatar
// create user object - create entry in db
// remove password and refresh token field from response
// check for user creation
// return res


const {fullName, email, username, password } = req.body
console.log("email: ", email);

if (
[fullName, email, username, password].some((field) => field?.trim() === "")
) {
throw new ApiError(400, "All fields are required")
}

const existedUser = User.findOne({
$or: [{ username }, { email }]
})

if (existedUser) {
throw new ApiError(409, "User with email or username already exists")
}

const avatarLocalPath = req.files?.avatar[0]?.path;
const coverImageLocalPath = req.files?.coverImage[0]?.path;

if (!avatarLocalPath) {
throw new ApiError(400, "Avatar file is required")
}

const avatar = await uploadOnCloudinary(avatarLocalPath)
const coverImage = await uploadOnCloudinary(coverImageLocalPath)

if (!avatar) {
throw new ApiError(400, "Avatar file is required")
}

const user = await User.create({
fullName,
avatar: avatar.url,
coverImage: coverImage?.url || "",
email,
password,
username: username.toLowerCase()
})

const createdUser = await User.findById(user._id).select(
"-password -refreshToken"
)

if (!createdUser) {
throw new ApiError(500, "Something went wrong while registering the user")
}

return res.status(201).json(
new ApiResponse(200, createdUser, "User registered Successfully")
)

} )


Expand Down
2 changes: 1 addition & 1 deletion src/models/user.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const userSchema = new Schema(
userSchema.pre("save", async function (next) {
if(!this.isModified("password")) return next();

this.password = bcrypt.hash(this.password, 10)
this.password = await bcrypt.hash(this.password, 10)
next()
})

Expand Down
16 changes: 15 additions & 1 deletion src/routes/user.routes.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
import { Router } from "express";
import { registerUser } from "../controllers/user.controller.js";
import {upload} from "../middlewares/multer.middleware.js"


const router = Router()

router.route("/register").post(registerUser)
router.route("/register").post(
upload.fields([
{
name: "avatar",
maxCount: 1
},
{
name: "coverImage",
maxCount: 1
}
]),
registerUser
)


export default router
4 changes: 3 additions & 1 deletion src/utils/ApiResponse.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ class ApiResponse {
this.message = message
this.success = statusCode < 400
}
}
}

export { ApiResponse }

0 comments on commit 12e4963

Please sign in to comment.