Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion controllers/auth/login.js
Original file line number Diff line number Diff line change
@@ -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 };
41 changes: 40 additions & 1 deletion controllers/auth/signup.js
Original file line number Diff line number Diff line change
@@ -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 };
22 changes: 21 additions & 1 deletion controllers/books/deleteBook.js
Original file line number Diff line number Diff line change
@@ -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 };

17 changes: 16 additions & 1 deletion controllers/books/getAllBooks.js
Original file line number Diff line number Diff line change
@@ -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 };