Skip to content

Commit

Permalink
MILESTONE: Committing auth routes.
Browse files Browse the repository at this point in the history
MAINT: Adding features.
  • Loading branch information
AhmedMaherElSaeidi committed Jul 27, 2024
1 parent 5bd6775 commit 02b3452
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 2 deletions.
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ app.use(express.static("public"));
if (app.get("env") === "development") app.use(morgan("tiny"));

// Importing routes (APIs)
const auth_routes = require("./routes/auth");
const user_routes = require("./routes/users");
const carts_routes = require("./routes/carts");
const default_routes = require("./routes/default");
Expand All @@ -19,6 +20,7 @@ const cartItems_routes = require("./routes/cartItems");
const categories_routes = require("./routes/categories");

app.use("/", default_routes);
app.use("/api/auth", auth_routes);
app.use("/api/users", user_routes);
app.use("/api/carts", carts_routes);
app.use("/api/products", products_routes);
Expand Down
94 changes: 94 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 @@ -16,6 +16,7 @@
"cors": "^2.8.5",
"express": "^4.19.2",
"joi": "^17.13.3",
"jsonwebtoken": "^9.0.2",
"morgan": "^1.10.0",
"mysql2": "^3.10.3",
"nodemon": "^3.1.4",
Expand Down
54 changes: 54 additions & 0 deletions routes/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const express = require("express");
const router = express.Router();
const Joi = require("joi");
const { User } = require("../models/index");
const Password = require("../services/Password");
const JWT = require("../services/JWT");

// Table schema (for validating post, and put request)
const schema = Joi.object({
username: Joi.string().required(),
password: Joi.string().required(),
});

// Authentication
router.post("/login", async (req, res) => {
try {
const { username, password } = req.body;

// Validate data
const { error } = schema.validate(req.body);
if (error)
return res.status(400).json({ message: error.details[0].message });

// Fetching user data
const user = await User.findOne({
where: { username },
});
if (!user)
return res.status(404).json({ message: "Username doesn't exist" });

// Validating password
const validPassword = await Password.comparePassword(
password,
user.password
);
if (!validPassword)
return res.status(404).json({ message: "Invalid password." });

const token = JWT.sign({
id: user.id,
name: `${user.fname} ${user.lname}`,
username: user.username,
telephone: user.telephone,
image: user.image,
gender: user.gender,
admin: user.role === "admin" ? true : false,
});
res.status(201).json({ data: token });
} catch (error) {
res.status(400).json({ message: error });
}
});

module.exports = router;
14 changes: 14 additions & 0 deletions services/JWT.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const jwt = require("jsonwebtoken");

class JWT {
static _key = "5BD24DCB1483578373DD86A7AD35F";
static _ms = "3600s";

static sign(object) {
return jwt.sign(object, this._key, { expiresIn: this._ms });
}

static verify(token) {}
}

module.exports = JWT;
6 changes: 4 additions & 2 deletions services/Password.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ class Password {
return await bcrypt.hash(password, salt);
}

static comparePassword(password, hashedPassword) {}
static async comparePassword(password, hashedPassword) {
return await bcrypt.compare(password, hashedPassword);
}
}

module.exports = Password;
module.exports = Password;

0 comments on commit 02b3452

Please sign in to comment.