estatesfunding/ef-api/controllers/admin.js

50 lines
1.2 KiB
JavaScript
Raw Permalink Normal View History

2024-11-05 11:31:20 +00:00
import dotenv from "dotenv";
import mongoose from 'mongoose';
import bcrypt from "bcryptjs";
import jwt from "jsonwebtoken";
import adminModal from "../models/admin.js"
dotenv.config();
const secret = process.env.SECRET_KEY;
export const login = async (req, res) => {
const { username, password } = req.body;
try {
const user = await adminModal.findOne({ username });
if (!user) return res.status(400).json({ message: "User not found" });
const isMatch = await bcrypt.compare(password, user.password);
if (!isMatch) return res.status(400).json({ message: "Invalid password" });
const token = jwt.sign({ userId: user._id }, secret, { expiresIn: "1h" });
res.json({ token });
} catch (error) {
res.status(500).json({ message: "Server error" });
}
};
2024-11-07 15:29:39 +00:00
//To show Admin user
export const adminUser = async (req, res) => {
try {
const user = await adminModal.findOne({ username: "admin" });
if (!user) {
return res.status(404).json({ message: 'User not found' });
}
return res.json(user);
} catch (error) {
return res.status(500).json({ message: 'Server Error' });
}
};
2024-11-05 11:31:20 +00:00