Refactor arguments check
This commit is contained in:
parent
536c791f66
commit
6843b76ebf
2 changed files with 18 additions and 17 deletions
|
|
@ -31,7 +31,7 @@ export default {
|
||||||
return this.post("signup", { name })
|
return this.post("signup", { name })
|
||||||
},
|
},
|
||||||
|
|
||||||
async login(password: string) {
|
async login(key: string) {
|
||||||
return this.post("login", { password })
|
return this.post("login", { key })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,19 @@ const protect = (req, res, next) => {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const neededArguments = (args) => {
|
||||||
|
return (req, res, next) => {
|
||||||
|
for (let i = 0; i < args.length; i++) {
|
||||||
|
const arg = args[i];
|
||||||
|
if (!req.body[arg])
|
||||||
|
return res.status(400).json({
|
||||||
|
error: "missing argument " + arg
|
||||||
|
})
|
||||||
|
}
|
||||||
|
next()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const app = express()
|
const app = express()
|
||||||
app.use(morgan('combined'))
|
app.use(morgan('combined'))
|
||||||
app.use(express.json())
|
app.use(express.json())
|
||||||
|
|
@ -40,14 +53,7 @@ app.get("/secured", protect, (req, res) => {
|
||||||
res.json({ message: "You can see it!", payload: req.user })
|
res.json({ message: "You can see it!", payload: req.user })
|
||||||
})
|
})
|
||||||
|
|
||||||
app.post("/signup", async (req, res) => {
|
app.post("/signup", neededArguments(['name']), async (req, res) => {
|
||||||
console.log("No co jest??", req.body)
|
|
||||||
|
|
||||||
if (!req.body.name)
|
|
||||||
return res.status(400).json({
|
|
||||||
error: "missing argument name"
|
|
||||||
})
|
|
||||||
|
|
||||||
const user = await auth.createAccount(req.body.name)
|
const user = await auth.createAccount(req.body.name)
|
||||||
|
|
||||||
if (user.error)
|
if (user.error)
|
||||||
|
|
@ -58,13 +64,8 @@ app.post("/signup", async (req, res) => {
|
||||||
res.json(user)
|
res.json(user)
|
||||||
})
|
})
|
||||||
|
|
||||||
app.post("/login", async (req, res) => {
|
app.post("/login", neededArguments(['key']), async (req, res) => {
|
||||||
if (!req.body.password)
|
const user = auth.login(req.body.key)
|
||||||
return res.status(400).json({
|
|
||||||
error: "missing argument password"
|
|
||||||
})
|
|
||||||
|
|
||||||
const user = auth.login(req.body.password)
|
|
||||||
|
|
||||||
if (user.error)
|
if (user.error)
|
||||||
return res.status(400).json({
|
return res.status(400).json({
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue