chore: file formatting
This commit is contained in:
@@ -1,24 +1,24 @@
|
||||
import prisma from "../prisma/client.js";
|
||||
import {generateToken} from "../utils/jwt.js";
|
||||
import {hashPassword, comparePassword} from "../utils/password.js";
|
||||
import prisma from '../prisma/client.js';
|
||||
import { generateToken } from '../utils/jwt.js';
|
||||
import { hashPassword, comparePassword } from '../utils/password.js';
|
||||
|
||||
/**
|
||||
* REGISTER
|
||||
* POST /api/auth/register
|
||||
*/
|
||||
export async function register(req, res) {
|
||||
const {username, password, role} = req.body;
|
||||
const { username, password, role } = req.body;
|
||||
|
||||
if (!username || !password) {
|
||||
return res.status(400).json({error: "Username and password required"});
|
||||
return res.status(400).json({ error: 'Username and password required' });
|
||||
}
|
||||
|
||||
const existingUser = await prisma.user.findUnique({
|
||||
where: {username},
|
||||
where: { username },
|
||||
});
|
||||
|
||||
if (existingUser) {
|
||||
return res.status(409).json({error: "Username already exists"});
|
||||
return res.status(409).json({ error: 'Username already exists' });
|
||||
}
|
||||
|
||||
const hashedPassword = await hashPassword(password);
|
||||
@@ -27,12 +27,12 @@ export async function register(req, res) {
|
||||
data: {
|
||||
username,
|
||||
password: hashedPassword,
|
||||
role: role || "admin",
|
||||
role: role || 'admin',
|
||||
},
|
||||
});
|
||||
|
||||
res.status(201).json({
|
||||
message: "User registered successfully",
|
||||
message: 'User registered successfully',
|
||||
user: {
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
@@ -46,24 +46,24 @@ export async function register(req, res) {
|
||||
* POST /api/auth/login
|
||||
*/
|
||||
export async function login(req, res) {
|
||||
const {username, password} = req.body;
|
||||
const { username, password } = req.body;
|
||||
|
||||
if (!username || !password) {
|
||||
return res.status(400).json({error: "Username and password required"});
|
||||
return res.status(400).json({ error: 'Username and password required' });
|
||||
}
|
||||
|
||||
const user = await prisma.user.findUnique({
|
||||
where: {username},
|
||||
where: { username },
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
return res.status(401).json({error: "Invalid credentials"});
|
||||
return res.status(401).json({ error: 'Invalid credentials' });
|
||||
}
|
||||
|
||||
const isValid = await comparePassword(password, user.password);
|
||||
|
||||
if (!isValid) {
|
||||
return res.status(401).json({error: "Invalid credentials"});
|
||||
return res.status(401).json({ error: 'Invalid credentials' });
|
||||
}
|
||||
|
||||
const token = generateToken({
|
||||
@@ -72,5 +72,5 @@ export async function login(req, res) {
|
||||
role: user.role,
|
||||
});
|
||||
|
||||
res.json({token});
|
||||
res.json({ token });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user