Account login possible
This commit is contained in:
@@ -23,7 +23,7 @@ export const sequelize = new Sequelize({
|
||||
|
||||
export function startDatabase() {
|
||||
// Create database and tables
|
||||
sequelize.sync({ force: true })
|
||||
sequelize.sync({ force: false })
|
||||
.then(() => {
|
||||
console.log(`Database & tables created!`)
|
||||
})
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
)
|
||||
})
|
||||
Reference in New Issue
Block a user