todo auth

This commit is contained in:
Primakov Alexandr Alexandrovich 2025-01-18 15:44:14 +03:00
parent 8b7f43d15a
commit d88e680413
6 changed files with 164 additions and 1 deletions

View File

@ -80,7 +80,7 @@ app.use(require("./root"))
*/
app.use("/kfu-m-24-1", require("./routers/kfu-m-24-1"))
app.use("/epja-2024-1", require("./routers/epja-2024-1"))
app.use("/todo", require("./routers/todo/routes"))
app.use("/v1/todo", require("./routers/todo"))
app.use("/dogsitters-finder", require("./routers/dogsitters-finder"))
app.use("/kazan-explore", require("./routers/kazan-explore"))
app.use("/edateam", require("./routers/edateam-legacy"))

View File

@ -0,0 +1,90 @@
const { Router } = require("express");
const hash = require("pbkdf2-password")();
const { promisify } = require("node:util");
const jwt = require('jsonwebtoken')
const { AuthModel } = require("./model/todo/auth");
const { UserModel } = require("./model/todo/user");
const { getAnswer } = require("../../utils/common");
const router = Router();
const TOKEN_KEY = process.env.TOKEN_KEY || "asdfhoa-podh829438132 iahda98gauj dj2i3-111"
const requiredValidate =
(...fields) =>
(req, res, next) => {
const errors = []
fields.forEach((field) => {
if (!req.body[field]) {
errors.push(field);
}
});
if (errors.length) {
throw new Error(`Не все поля заполнены: ${errors.join(", ")}`);
} else {
next();
}
};
router.post(
"/signup",
requiredValidate("login", "password", "email"),
async (req, res, next) => {
const { login, password, email } = req.body;
const user = await AuthModel.findOne({ login });
if (user) {
throw new Error("Пользователь с таким логином уже существует");
}
hash({ password }, async function (err, pass, salt, hash) {
if (err) return next(err);
const user = await UserModel.create({ login, email });
await AuthModel.create({ login, hash, salt, userId: user.id });
res.json(getAnswer(null, { ok: true }));
});
}
);
function authenticate(login, pass, cb) {
AuthModel.findOne({ login }).populate('userId').exec().then((user) => {
if (!user) return cb(null, null)
hash({ password: pass, salt: user.salt }, function (err, pass, salt, hash) {
if (err) return cb(err)
if (hash === user.hash) return cb(null, user)
cb(null, null)
})
})
}
const auth = promisify(authenticate);
router.post('/signin', async (req, res) => {
const { login, password } = req.body;
const user = await auth(login, password)
if (!user) {
throw new Error("Неверный логин или пароль")
}
const accessToken = jwt.sign({
...user.userId
}, TOKEN_KEY, {
expiresIn: '12h'
})
res.json(getAnswer(null, {
user: user.userId,
token: accessToken
}))
})
module.exports = router

View File

@ -1,2 +1,6 @@
exports.TODO_LIST_MODEL_NAME = 'TODO_LIST'
exports.TODO_ITEM_MODEL_NAME = 'TODO_ITEM'
exports.TODO_AUTH_PASSWD_MODEL_NAME = 'TODO_AUTH_PASSWD'
exports.TODO_AUTH_USER_MODEL_NAME = 'TODO_AUTH_USER'
exports.TODO_AUTH_CHAT_MODEL_NAME = 'TODO_AUTH_CHAT'

View File

@ -0,0 +1,11 @@
const { Router } = require('express')
const router = Router()
const todoRouter = require('./routes')
const authRouter = require('./auth')
router.use(todoRouter)
router.use('/auth', authRouter)
module.exports = router

View File

@ -0,0 +1,31 @@
const { Schema, model } = require("mongoose");
const {
TODO_AUTH_PASSWD_MODEL_NAME,
TODO_AUTH_USER_MODEL_NAME,
} = require("../../const");
const schema = new Schema({
login: { type: String, required: true, unique: true },
hash: { type: String, required: true },
salt: { type: String, required: true },
userId: { type: Schema.Types.ObjectId, ref: TODO_AUTH_USER_MODEL_NAME },
created: {
type: Date,
default: () => new Date().toISOString(),
},
});
schema.set("toJSON", {
virtuals: true,
versionKey: false,
transform: function (doc, ret) {
delete ret._id;
},
});
schema.virtual("id").get(function () {
return this._id.toHexString();
});
exports.AuthModel = model(TODO_AUTH_PASSWD_MODEL_NAME, schema);

View File

@ -0,0 +1,27 @@
const { Schema, model } = require("mongoose");
const { TODO_AUTH_USER_MODEL_NAME } = require("../../const");
const schema = new Schema({
login: { type: String, required: true, unique: true },
email: { type: String, required: true, unique: true },
role: { type: String, default: "user" },
created: {
type: Date,
default: () => new Date().toISOString(),
},
})
schema.set("toJSON", {
virtuals: true,
versionKey: false,
transform: function (doc, ret) {
delete ret._id
},
})
schema.virtual("id").get(function () {
return this._id.toHexString()
})
exports.UserModel = model(TODO_AUTH_USER_MODEL_NAME, schema);