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 }; 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 }; diff --git a/controllers/books/deleteBook.js b/controllers/books/deleteBook.js index 1d5a5fd..d4c979a 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 }; + 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 };