Skip to content

Commit 2703c2b

Browse files
authored
Merge pull request #29 from ADARSHsri2004/feature/auth-flow
feat/Implemented User Registration Endpoint #11 Thanks for contributing!
2 parents 2729bfc + 8795292 commit 2703c2b

File tree

9 files changed

+131
-65
lines changed

9 files changed

+131
-65
lines changed

.env.example

Lines changed: 0 additions & 6 deletions
This file was deleted.

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ node_modules
55
.env.local
66

77
/dist/
8-
/build/
8+
/build/

package-lock.json

Lines changed: 22 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"express": "^5.1.0",
2626
"jsonwebtoken": "^9.0.2",
2727
"mongoose": "^8.19.1",
28-
"nodemon": "^3.1.10"
28+
"nodemon": "^3.1.10",
29+
"readdirp": "^4.1.2"
2930
}
3031
}

src/app.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
import express from "express";
22
import cors from "cors"
33
import cookieparser from "cookie-parser"
4+
import authRoutes from "./routes/authRoutes.js"
45

5-
const app=express()
6+
const app = express()
67

78
app.use(cors({
8-
origin:process.env.CORS_URL,
9-
credentials:true
9+
origin: process.env.CORS_URL,
10+
credentials: true
1011
}))
1112

12-
app.use(express.json({limit:"16kb"}))
13-
app.use(express.urlencoded({extended:true,limit:"16kb"}))
13+
app.use(express.json({ limit: "16kb" }))
14+
app.use(express.urlencoded({ extended: true, limit: "16kb" }))
1415
app.use(express.static("public"))
1516
app.use(cookieparser())
1617

18+
// Routes
19+
app.use("/api/auth", authRoutes)
1720

18-
19-
export {app}
21+
export { app }

src/controllers/authController.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { loginUser } from "../services/authService.js";
2+
3+
export const login = async (req, res) => {
4+
try {
5+
const { email, password } = req.body;
6+
7+
if (!email?.trim() || !password?.trim()) {
8+
return res.status(400).json({ success: false, message: "Email and password are required" });
9+
}
10+
11+
const { token, refreshToken } = await loginUser(email, password);
12+
13+
return res.status(200).json({
14+
success: true,
15+
message: "Login successful",
16+
token,
17+
refreshToken,
18+
});
19+
} catch (err) {
20+
return res.status(401).json({
21+
success: false,
22+
message: err.message || "Authentication failed",
23+
});
24+
}
25+
};

src/models/user.model.js

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,59 @@
1-
import mongoose ,{Schema} from "mongoose";
1+
import mongoose, { Schema } from "mongoose";
22
import jwt from "jsonwebtoken"
33
import bcrypt from "bcryptjs"
44

5-
const userschema=new Schema({
6-
username:{
7-
type:String,
8-
required:true,
9-
unique:true,
10-
lowercase:true,
11-
trim:true,
12-
index:true
5+
const userschema = new Schema({
6+
username: {
7+
type: String,
8+
required: true,
9+
unique: true,
10+
lowercase: true,
11+
trim: true,
12+
index: true
1313
},
14-
email:{
15-
type:String,
16-
required:true,
17-
unique:true,
18-
lowercase:true,
19-
trim:true,
14+
email: {
15+
type: String,
16+
required: true,
17+
unique: true,
18+
lowercase: true,
19+
trim: true,
2020
},
21-
fullname:{
22-
type:String,
23-
required:true,
24-
trim:true,
25-
index:true
21+
fullname: {
22+
type: String,
23+
required: true,
24+
trim: true,
25+
index: true
2626
},
27-
password:{
28-
type:String,
29-
required:[true,"Password is required"]
30-
},
31-
refreshToken:{
32-
type:String
33-
},
34-
role: {
35-
type: mongoose.Schema.Types.ObjectId,
36-
ref: "Role",
37-
default: null
38-
}
39-
},{
40-
timestamps:true
27+
password: {
28+
type: String,
29+
required: [true, "Password is required"]
30+
},
31+
refreshToken: {
32+
type: String
33+
},
34+
role: {
35+
type: mongoose.Schema.Types.ObjectId,
36+
ref: "Role",
37+
default: null
38+
}
39+
}, {
40+
timestamps: true
4141
})
4242

43-
userschema.pre("save", async function (next){
44-
if(!this.isModified("password"))return next();
43+
userschema.pre("save", async function (next) {
44+
if (!this.isModified("password")) return next();
4545

46-
this.password=await bcrypt.hash(this.password,10)
46+
this.password = await bcrypt.hash(this.password, 10)
4747
next()
4848
})
4949

50-
userschema.methods.isPasswordCorrect=async function(password){
51-
return await bcrypt.compare(password,this.password)
50+
userschema.methods.isPasswordCorrect = async function (password) {
51+
return await bcrypt.compare(password, this.password)
5252
}
5353

54-
userschema.methods.genrateAccessToken = function () {
54+
userschema.methods.generateAccessToken = function () {
5555
try {
56-
if (!process.env.JWT_SECRET ) {
56+
if (!process.env.JWT_SECRET) {
5757
throw new Error("Environment variables for token generation are missing");
5858
}
5959

@@ -95,4 +95,4 @@ userschema.methods.refreshAccessToken = function () {
9595
};
9696

9797

98-
export const User=mongoose.model("User",userschema)
98+
export const User = mongoose.model("User", userschema)

src/routes/authRoutes.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import express from "express";
2+
import { login } from "../controllers/authController.js";
3+
4+
const router = express.Router();
5+
6+
router.post("/login", login);
7+
8+
export default router;

src/services/authService.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { User } from "../models/user.model.js";
2+
3+
export const loginUser = async (email, password) => {
4+
const user = await User.findOne({ email });
5+
if (!user) {
6+
throw new Error("Invalid credentials");
7+
}
8+
9+
const isValid = await user.isPasswordCorrect(password);
10+
if (!isValid) {
11+
throw new Error("Invalid credentials");
12+
}
13+
14+
const token = user.generateAccessToken();
15+
const refreshToken = user.refreshAccessToken();
16+
17+
// Update user's refresh token in database
18+
user.refreshToken = refreshToken;
19+
await user.save({ validateBeforeSave: false });
20+
21+
return { token, refreshToken };
22+
};

0 commit comments

Comments
 (0)