From 8aa485e138220367797b33606b3a545806a960d1 Mon Sep 17 00:00:00 2001 From: Adarsh Pal <111274562+CodingPhionix@users.noreply.github.com> Date: Thu, 22 Feb 2024 16:17:27 +0530 Subject: [PATCH 1/5] feat: Login Authentication Issue : #2 --- controllers/auth/login.js | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/controllers/auth/login.js b/controllers/auth/login.js index 9714115..42771b1 100644 --- a/controllers/auth/login.js +++ b/controllers/auth/login.js @@ -1,5 +1,38 @@ +const jwt = require('jsonwebtoken'); + const loginController = async (req, res) => { - return res.status(200).json({ message: "Login route" }); + // Assuming you have a function to authenticate the user + const { email, password } = req.body; + + // Checking email and password are provided + if (!email || !password) { + return res.status(400).json({ message: "Email and password are required." }); + } + + // Check if email and password are valid (Authenticate the user) + if (email !== "jhon@example.com" || password !== "password123") { + return res.status(401).json({ message: "Invalid email or password." }); + } + + // User is authenticated, generate JWT token + const user = { + firstName: "Jhon", + lastName: "Doe", + email: "jhon@example.com" + }; + + const accessToken = jwt.sign(user, 'your_secret_key_here', { expiresIn: '1h' }); + + // Return the token along with the success message + return res.status(200).json({ + message: "User logged in successfully", + user: { + firstName: user.firstName, + lastName: user.lastName, + email: user.email, + accessToken: accessToken + } + }); }; module.exports = { loginController }; From 4284dbf29e00e111b5ac4c702f68f7a23946198e Mon Sep 17 00:00:00 2001 From: Adarsh Pal <111274562+CodingPhionix@users.noreply.github.com> Date: Thu, 22 Feb 2024 16:35:26 +0530 Subject: [PATCH 2/5] feat: signup auth routs fix --- controllers/auth/signup.js | 41 +++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/controllers/auth/signup.js b/controllers/auth/signup.js index 8eb6d04..8bb8b54 100644 --- a/controllers/auth/signup.js +++ b/controllers/auth/signup.js @@ -1,5 +1,44 @@ +const bcrypt = require('bcryptjs'); +const User = require('./models/User.js'); + const signupController = async (req, res) => { - return res.status(200).json({ message: "Signup route" }); + const { firstName, lastName, email, password } = req.body; + + try { + // Check if user with the email already exists + const existingUser = await User.findOne({ email }); + + if (existingUser) { + return res.status(400).json({ message: "Email already exists." }); + } + + // Hash the password + const hashedPassword = await bcrypt.hash(password, 10); + + // Create a new user instance + const newUser = new User({ + firstName, + lastName, + email, + password: hashedPassword + }); + + // Save the user to the database + await newUser.save(); + + // Return success response + return res.status(200).json({ + message: "User signed up successfully", + user: { + firstName, + lastName, + email + } + }); + } catch (error) { + console.error("Error while signing up user:", error); + return res.status(500).json({ message: "Internal server error" }); + } }; module.exports = { signupController }; From 8fb168576a8e3938f3e7192d6cc2b89d87337ffe Mon Sep 17 00:00:00 2001 From: Adarsh Pal <111274562+CodingPhionix@users.noreply.github.com> Date: Thu, 22 Feb 2024 16:47:15 +0530 Subject: [PATCH 3/5] feat: added the get all books api end point fix: issue #5 - Added a controller to fetch all books from the database. - Used the Book model to retrieve book data from the database. - Returned the fetched books in the response along with a success message. --- controllers/books/getAllBooks.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/controllers/books/getAllBooks.js b/controllers/books/getAllBooks.js index 9cf00bc..113e711 100644 --- a/controllers/books/getAllBooks.js +++ b/controllers/books/getAllBooks.js @@ -1,5 +1,20 @@ +const Book = require('./models/Book'); + const getAllBooksController = async (req, res) => { - return res.status(200).json({ message: "Return all books from database" }); + try { + // Fetch all books from the database + const books = await Book.find(); + + // Return the fetched books in the response + return res.status(200).json({ + status: 200, + message: "Books fetched successfully", + books: books + }); + } catch (error) { + console.error("Error while fetching books:", error); + return res.status(500).json({ message: "Internal server error" }); + } }; module.exports = { getAllBooksController }; From 24388500f9268d080c34b11e59bfff00b2dd916e Mon Sep 17 00:00:00 2001 From: Adarsh Pal <111274562+CodingPhionix@users.noreply.github.com> Date: Thu, 22 Feb 2024 16:58:56 +0530 Subject: [PATCH 4/5] Update deleteBook.js - Added a controller to handle book deletion by ID. - Utilized the Book model to find and remove the book from the database. - Returned a success message upon successful deletion of the book. --- controllers/books/deleteBook.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/controllers/books/deleteBook.js b/controllers/books/deleteBook.js index 1d5a5fd..f7cfc55 100644 --- a/controllers/books/deleteBook.js +++ b/controllers/books/deleteBook.js @@ -1,5 +1,25 @@ +const Book = require('../../models/Book'); // Assuming you have a Book model + const deleteBookController = async (req, res) => { - return res.status(200).json({ message: "Delete a book" }); + try { + // Extract book ID from request parameters + const { id } = req.params; + + // Find the book by ID and delete it + const deletedBook = await Book.findByIdAndDelete(id); + + // Check if the book exists + if (!deletedBook) { + return res.status(404).json({ message: "Book not found" }); + } + + // Return success message + return res.status(200).json({ message: "Book deleted successfully" }); + } catch (error) { + console.error("Error while deleting book:", error); + return res.status(500).json({ message: "Internal server error" }); + } }; module.exports = { deleteBookController }; + From e9fc48ad30519d6cbd46e4e33e55a1ef471d5b8a Mon Sep 17 00:00:00 2001 From: Adarsh Pal <111274562+CodingPhionix@users.noreply.github.com> Date: Thu, 22 Feb 2024 16:59:31 +0530 Subject: [PATCH 5/5] Update deleteBook.js Updated the typos --- controllers/books/deleteBook.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/controllers/books/deleteBook.js b/controllers/books/deleteBook.js index f7cfc55..d4c979a 100644 --- a/controllers/books/deleteBook.js +++ b/controllers/books/deleteBook.js @@ -1,4 +1,4 @@ -const Book = require('../../models/Book'); // Assuming you have a Book model +const Book = require('./models/Book'); // Assuming you have a Book model const deleteBookController = async (req, res) => { try {