init project backend-messager
commit
caa7be1fce
|
@ -0,0 +1,22 @@
|
|||
version: "3.7"
|
||||
|
||||
services:
|
||||
mongo:
|
||||
image: mongo
|
||||
restart: always
|
||||
environment:
|
||||
- MONGO_INITDB_ROOT_USERNAME=root
|
||||
- MONGO_INITDB_ROOT_PASSWORD=qwe123
|
||||
ports:
|
||||
- 27017:27017
|
||||
mongo-express:
|
||||
image: mongo-express
|
||||
restart: always
|
||||
ports:
|
||||
- 8081:8081
|
||||
environment:
|
||||
- ME_CONFIG_MONGODB_ADMINUSERNAME=root
|
||||
- ME_CONFIG_MONGODB_ADMINPASSWORD=qwe123
|
||||
- ME_CONFIG_MONGODB_URL=mongodb://root:qwe123@mongo:27017/
|
||||
depends_on:
|
||||
- mongo
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,21 @@
|
|||
{
|
||||
"name": "message-beackend",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "app.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"start": "node ./src/app.js"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"type": "module",
|
||||
"dependencies": {
|
||||
"bcrypt": "^5.1.0",
|
||||
"cookie-parser": "^1.4.6",
|
||||
"dotenv": "^16.0.3",
|
||||
"express": "^4.18.2",
|
||||
"jsonwebtoken": "^9.0.0",
|
||||
"mongoose": "^7.2.0"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
import UserService from "../Services/UserService.js";
|
||||
|
||||
class UserController {
|
||||
async register(req, res) {
|
||||
const user = await UserService.CreateUser(req.body);
|
||||
res.send(user);
|
||||
}
|
||||
async login(req, res) {
|
||||
const user = await UserService.LoginUser(req.body);
|
||||
res.cookie("refresh", user.refreshToken, {
|
||||
maxAge: 30 * 24 * 60 * 60 * 1000,
|
||||
httpOnly: true,
|
||||
});
|
||||
res.send(user);
|
||||
}
|
||||
async logout(req, res) {
|
||||
res.clearCookie("refresh");
|
||||
res.send({ message: "Выход завершен" });
|
||||
}
|
||||
}
|
||||
|
||||
export default new UserController();
|
|
@ -0,0 +1,9 @@
|
|||
import mongoose, { Schema } from "mongoose";
|
||||
|
||||
const users = new Schema({
|
||||
login: { type: String },
|
||||
password: { type: String },
|
||||
email: { type: String },
|
||||
});
|
||||
|
||||
export default mongoose.model("users", users);
|
|
@ -0,0 +1,9 @@
|
|||
import { Router } from "express";
|
||||
import UserController from "../Controllers/UserController.js";
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.post("/register", UserController.register);
|
||||
router.post("/login", UserController.login);
|
||||
router.get("/logout", UserController.logout);
|
||||
export default router;
|
|
@ -0,0 +1,21 @@
|
|||
import jsonwebtoken from "jsonwebtoken";
|
||||
class TokenService {
|
||||
genTokens(payload) {
|
||||
const accessToken = jsonwebtoken.sign(payload, process.env.Access_Jwt_Key, {
|
||||
expiresIn: "1h",
|
||||
});
|
||||
const refreshToken = jsonwebtoken.sign(
|
||||
payload,
|
||||
process.env.Refresh_Jwt_Key,
|
||||
{
|
||||
expiresIn: "30d",
|
||||
}
|
||||
);
|
||||
return {
|
||||
accessToken: accessToken,
|
||||
refreshToken: refreshToken,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export default new TokenService();
|
|
@ -0,0 +1,43 @@
|
|||
import Users from "../DataBase/model/Users.js";
|
||||
import bcrypt from "bcrypt";
|
||||
import jsonwebtoken from "jsonwebtoken";
|
||||
import TokenService from "./TokenService.js";
|
||||
|
||||
class UserService {
|
||||
async CreateUser({ user, password, email }) {
|
||||
try {
|
||||
const CheckUser = await Users.findOne({ login: user });
|
||||
if (CheckUser) {
|
||||
return { message: "Такой пользователь уже существует", result: false };
|
||||
}
|
||||
|
||||
const hashPass = await bcrypt.hash(password, 5);
|
||||
const data = await Users.create({
|
||||
login: user,
|
||||
password: hashPass,
|
||||
email,
|
||||
});
|
||||
return { message: "Пользователь зарегестрирован", result: true };
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
}
|
||||
|
||||
async LoginUser({ user, password }) {
|
||||
const CheckUser = await Users.findOne({ login: user });
|
||||
if (!CheckUser) {
|
||||
return { message: "Пользователь не найден", result: false };
|
||||
}
|
||||
const isValidPass = await bcrypt.compare(password, CheckUser.password);
|
||||
if (!isValidPass) {
|
||||
return { message: "Неверный пользователь или пароль", result: false };
|
||||
}
|
||||
const tokens = TokenService.genTokens({
|
||||
user: CheckUser.login,
|
||||
email: CheckUser.email,
|
||||
});
|
||||
return tokens;
|
||||
}
|
||||
}
|
||||
|
||||
export default new UserService();
|
|
@ -0,0 +1,21 @@
|
|||
import express from "express";
|
||||
import mongoose from "mongoose";
|
||||
import * as dotenv from "dotenv";
|
||||
import { PORT } from "./utils/Constants.js";
|
||||
import router from "./Routers/UserRouter.js";
|
||||
import cookieParser from "cookie-parser";
|
||||
|
||||
dotenv.config();
|
||||
const server = express();
|
||||
server.use(cookieParser());
|
||||
server.use(express.json());
|
||||
server.use(router);
|
||||
|
||||
try {
|
||||
await mongoose.connect(process.env.DB_URL);
|
||||
server.listen(PORT, () => {
|
||||
console.log(`Start server on port${PORT}`);
|
||||
});
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
export const PORT = 5000;
|
Loading…
Reference in New Issue