User registration completed

This commit is contained in:
2024-09-10 20:28:24 +02:00
parent 628d1e7bee
commit ee07a5a5af
14 changed files with 203 additions and 56 deletions

View File

@@ -1,12 +1,36 @@
import { Router, Request, Response, NextFunction } from "express";
import { Account } from "../models/account.model";
import { validateString } from "../scripts/validateHelper";
export const account = Router()
account.get("/", (req: Request, res: Response, next: NextFunction)=> {
// Request all user from the database
account.get("/", (req: Request, res: Response, next: NextFunction) => {
Account.findAll()
.then(accounts => {
res.json(accounts)
})
.catch(next)
})
// Creating a new user
account.post("/", (req: Request, res: Response, next: NextFunction) => {
if (!validateString(req.body.username, 4))
{
res.status(400).send({ error: "Username too short!" })
}
else if (!validateString(req.body.password, 8))
{
res.status(400).send({ error: "Password too short!" })
}
else
{
Account.create(req.body)
.then(account => {
res.json(account)
res.status(200).send()
}).catch(reason => {
res.status(400).send({ error: reason })
})
}
})