mirror of
https://github.com/dec0dOS/zero-ui.git
synced 2025-07-05 20:41:43 -07:00
feat: login-limiter
This commit is contained in:
parent
60777f5165
commit
7dd6f3729b
4 changed files with 14 additions and 8 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -161,4 +161,5 @@ sketch
|
||||||
# and uncomment the following lines
|
# and uncomment the following lines
|
||||||
# .pnp.*
|
# .pnp.*
|
||||||
|
|
||||||
# End of https://www.toptal.com/developers/gitignore/api/vscode,yarn,react,node
|
# End of https://www.toptal.com/developers/gitignore/api/vscode,yarn,react,node
|
||||||
|
.yarn/cache/*
|
|
@ -1,5 +1,5 @@
|
||||||
import express from "express";
|
import express from "express";
|
||||||
import rateLimit from "express-rate-limit"
|
import rateLimit from "express-rate-limit";
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
import * as auth from "../services/auth.js";
|
import * as auth from "../services/auth.js";
|
||||||
|
@ -7,7 +7,10 @@ import * as auth from "../services/auth.js";
|
||||||
const loginLimiter = rateLimit({
|
const loginLimiter = rateLimit({
|
||||||
windowMs: 15 * 60 * 1000, // 15 minutes
|
windowMs: 15 * 60 * 1000, // 15 minutes
|
||||||
max: 5, // limit each IP to 5 requests per windowMs
|
max: 5, // limit each IP to 5 requests per windowMs
|
||||||
message: "Too many login attempts, please try again in 15 minutes.",
|
message: {
|
||||||
|
status: 429,
|
||||||
|
error: "Too many login attempts, please try again in 15 minutes.",
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get("/login", async function (req, res) {
|
router.get("/login", async function (req, res) {
|
||||||
|
@ -21,7 +24,6 @@ router.get("/login", async function (req, res) {
|
||||||
router.post("/login", loginLimiter, async function (req, res) {
|
router.post("/login", loginLimiter, async function (req, res) {
|
||||||
if (req.body.username && req.body.password) {
|
if (req.body.username && req.body.password) {
|
||||||
auth.authorize(req.body.username, req.body.password, function (err, user) {
|
auth.authorize(req.body.username, req.body.password, function (err, user) {
|
||||||
console.log(err.message)
|
|
||||||
if (user) {
|
if (user) {
|
||||||
res.send({ token: user["token"] });
|
res.send({ token: user["token"] });
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -8,12 +8,12 @@ export async function authorize(username, password, callback) {
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
const user = users.find({ username: username });
|
const user = users.find({ username: username });
|
||||||
if (!user.value()) return callback(new Error("Cannot find user"));
|
if (!user.value()) return callback(new Error("Invalid username or password")); // If return "user not found" someone can do a user listing
|
||||||
const verified = await verifyHash(password, user.value()["password_hash"]);
|
const verified = await verifyHash(password, user.value()["password_hash"]);
|
||||||
if (verified) {
|
if (verified) {
|
||||||
return callback(null, user.value());
|
return callback(null, user.value());
|
||||||
} else {
|
} else {
|
||||||
return callback(new Error("Invalid password"));
|
return callback(new Error("Invalid username or password"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,8 @@ function LogInUser() {
|
||||||
const [open, setOpen] = useState(false);
|
const [open, setOpen] = useState(false);
|
||||||
const [snackbarOpen, setSnackbarOpen] = useState(false);
|
const [snackbarOpen, setSnackbarOpen] = useState(false);
|
||||||
|
|
||||||
|
const [error, setError] = useState("");
|
||||||
|
|
||||||
const [username, setUsername] = useState("");
|
const [username, setUsername] = useState("");
|
||||||
const [password, setPassword] = useState("");
|
const [password, setPassword] = useState("");
|
||||||
|
|
||||||
|
@ -65,7 +67,8 @@ function LogInUser() {
|
||||||
.catch(function (error) {
|
.catch(function (error) {
|
||||||
setPassword("");
|
setPassword("");
|
||||||
setSnackbarOpen(true);
|
setSnackbarOpen(true);
|
||||||
console.error(error);
|
setError(error.response.data.error);
|
||||||
|
// console.error(error.response.data.error);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -114,7 +117,7 @@ function LogInUser() {
|
||||||
vertical: "top",
|
vertical: "top",
|
||||||
horizontal: "center",
|
horizontal: "center",
|
||||||
}}
|
}}
|
||||||
message="Invalid username or password"
|
message={error}
|
||||||
/>
|
/>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue