Account login possible

This commit is contained in:
2024-09-11 14:54:33 +02:00
parent fd06b8a9a4
commit 55fd203c7f
10 changed files with 122 additions and 48 deletions

View File

@@ -14,13 +14,15 @@ account.get("/", (req: Request, res: Response, next: NextFunction) => {
})
// Creating a new user
account.post("/", (req: Request, res: Response, next: NextFunction) => {
account.post("/register", (req: Request, res: Response, next: NextFunction) => {
if (!validateString(req.body.username, 4))
{
// Status: 400 Bad request
res.status(400).send({ error: "Username too short!" })
}
else if (!validateString(req.body.password, 8))
{
// Status: 400 Bad request
res.status(400).send({ error: "Password too short!" })
}
else
@@ -28,9 +30,31 @@ account.post("/", (req: Request, res: Response, next: NextFunction) => {
Account.create(req.body)
.then(account => {
res.json(account)
// Status: 200 OK
res.status(200).send()
}).catch(reason => {
// Status: 400 Bad request
res.status(400).send({ error: reason })
})
}
})
account.post("/login", (req: Request, res: Response, next: NextFunction) => {
Account.findOne({ raw: true, where: { username: req.body.username }})
.then(account => {
if (account != null) {
if (account.password == req.body.password) {
// Status: 200 OK
res.status(200).send({ userAccountId: account.id })
} else {
// Status: 401 Unauthorized
res.status(401).send()
}
} else {
// Status: 401 Unauthorized
res.status(401).send()
}
}
)
})