mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-17 19:37:02 +00:00
finished user store in typscript
This commit is contained in:
@@ -34,6 +34,7 @@
|
||||
"@types/bluebird": "^3.5.32",
|
||||
"@types/cors": "^2.8.7",
|
||||
"@types/express": "^4.17.8",
|
||||
"@types/jsonwebtoken": "^8.5.0",
|
||||
"@types/multer": "^1.4.4",
|
||||
"@types/node": "^14.10.1",
|
||||
"@types/validator": "^13.1.0",
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
module.exports = {
|
||||
secret: "mysecret",
|
||||
expiresIn: "7d",
|
||||
};
|
||||
4
backend/src/config/auth.ts
Normal file
4
backend/src/config/auth.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export default {
|
||||
secret: "mysecret",
|
||||
expiresIn: "7d"
|
||||
};
|
||||
16
backend/src/controllers/SessionController.ts
Normal file
16
backend/src/controllers/SessionController.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { Request, Response } from "express";
|
||||
|
||||
import AuthUserService from "../services/AuthUserSerice";
|
||||
|
||||
const store = async (req: Request, res: Response): Promise<Response> => {
|
||||
const { email, password } = req.body;
|
||||
|
||||
const { user, token } = await AuthUserService({ email, password });
|
||||
|
||||
return res.status(200).json({
|
||||
user,
|
||||
token
|
||||
});
|
||||
};
|
||||
|
||||
export default store;
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Request, Response } from "express";
|
||||
|
||||
// import CheckSettingsHelper from "../helpers/CheckSettingsHelper";
|
||||
import CheckSettingsHelper from "../helpers/CheckSettingsHelper";
|
||||
import AppError from "../errors/AppError";
|
||||
|
||||
import CreateUserService from "../services/CreateUserService";
|
||||
@@ -9,30 +9,31 @@ import CreateUserService from "../services/CreateUserService";
|
||||
// import FindUserService from "../services/FindUserService";
|
||||
|
||||
export const index = async (req: Request, res: Response): Promise<Response> => {
|
||||
if (req.user.profile !== "admin") {
|
||||
throw new AppError("Only administrators can access this route.", 403); // should be handled better.
|
||||
}
|
||||
const { searchParam, pageNumber } = req.query as any;
|
||||
// if (req.user.profile !== "admin") {
|
||||
// throw new AppError("Only administrators can access this route.", 403); // should be handled better.
|
||||
// }
|
||||
// const { searchParam, pageNumber } = req.query as any;
|
||||
|
||||
const { users, count, hasMore } = await ListUsersService({
|
||||
searchParam,
|
||||
pageNumber
|
||||
});
|
||||
// const { users, count, hasMore } = await ListUsersService({
|
||||
// searchParam,
|
||||
// pageNumber
|
||||
// });
|
||||
|
||||
return res.json({ users, count, hasMore });
|
||||
// return res.json({ users, count, hasMore });
|
||||
return res.json({ ok: "ok" });
|
||||
};
|
||||
|
||||
export const store = async (req: Request, res: Response): Promise<Response> => {
|
||||
const { email, password, name, profile } = req.body;
|
||||
|
||||
// if (
|
||||
// req.url === "/signup" &&
|
||||
// (await CheckSettingsHelper("userCreation")) === "disabled"
|
||||
// ) {
|
||||
// throw new AppError("User creation is disabled by administrator.", 403);
|
||||
// } else if (req.user.profile !== "admin") {
|
||||
// throw new AppError("Only administrators can create users.", 403);
|
||||
// }
|
||||
if (
|
||||
req.url === "/signup" &&
|
||||
(await CheckSettingsHelper("userCreation")) === "disabled"
|
||||
) {
|
||||
throw new AppError("User creation is disabled by administrator.", 403);
|
||||
} else if (req.url !== "/signup" && req.user.profile !== "admin") {
|
||||
throw new AppError("Only administrators can create users.", 403);
|
||||
}
|
||||
|
||||
const user = await CreateUserService({
|
||||
email,
|
||||
@@ -44,26 +45,26 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
|
||||
return res.status(200).json(user);
|
||||
};
|
||||
|
||||
export const show = async (req: Request, res: Response): Promise<Response> => {
|
||||
const { userId } = req.params;
|
||||
// export const show = async (req: Request, res: Response): Promise<Response> => {
|
||||
// const { userId } = req.params;
|
||||
|
||||
const user = await FindUserService(userId);
|
||||
// const user = await FindUserService(userId);
|
||||
|
||||
return res.status(200).json(user);
|
||||
};
|
||||
// return res.status(200).json(user);
|
||||
// };
|
||||
|
||||
export const update = async (
|
||||
req: Request,
|
||||
res: Response
|
||||
): Promise<Response> => {
|
||||
if (req.user.profile !== "admin") {
|
||||
throw new AppError("Only administrators can edit users.", 403);
|
||||
}
|
||||
// export const update = async (
|
||||
// req: Request,
|
||||
// res: Response
|
||||
// ): Promise<Response> => {
|
||||
// if (req.user.profile !== "admin") {
|
||||
// throw new AppError("Only administrators can edit users.", 403);
|
||||
// }
|
||||
|
||||
const { userId } = req.params;
|
||||
const userData = req.body;
|
||||
// const { userId } = req.params;
|
||||
// const userData = req.body;
|
||||
|
||||
const user = await UpdateUserService({ userData, userId });
|
||||
// const user = await UpdateUserService({ userData, userId });
|
||||
|
||||
return res.status(200).json(user);
|
||||
};
|
||||
// return res.status(200).json(user);
|
||||
// };
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { Sequelize } from "sequelize-typescript";
|
||||
import User from "../models/User";
|
||||
import Setting from "../models/Setting";
|
||||
|
||||
// eslint-disable-next-line
|
||||
const dbConfig = require("../config/database");
|
||||
@@ -10,18 +11,17 @@ const dbConfig = require("../config/database");
|
||||
// const Message = require("../models/Message");
|
||||
// const Whatsapp = require("../models/Whatsapp");
|
||||
// const ContactCustomField = require("../models/ContactCustomField");
|
||||
// const Setting = require("../models/Setting");
|
||||
|
||||
const sequelize = new Sequelize(dbConfig);
|
||||
|
||||
const models = [
|
||||
User
|
||||
User,
|
||||
// Contact,
|
||||
// Ticket,
|
||||
// Message,
|
||||
// Whatsapp,
|
||||
// ContactCustomField,
|
||||
// Setting,
|
||||
Setting
|
||||
];
|
||||
|
||||
sequelize.addModels(models);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
class AppError {
|
||||
public readonly message: string;
|
||||
|
||||
public readonly statusCode: number;
|
||||
|
||||
constructor(message: string, statusCode = 400) {
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
import AppError from "../errors/AppError";
|
||||
import Setting from "../models/Setting";
|
||||
import AppError from "../errors/AppError";
|
||||
|
||||
const CheckSettings = async (key: string): Promise<string> => {
|
||||
const settingsRepository = getRepository(Setting);
|
||||
|
||||
const setting = await settingsRepository.findOne({
|
||||
const setting = await Setting.findOne({
|
||||
where: { key }
|
||||
});
|
||||
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
const jwt = require("jsonwebtoken");
|
||||
const util = require("util");
|
||||
|
||||
const User = require("../models/User");
|
||||
const authConfig = require("../config/auth");
|
||||
|
||||
module.exports = async (req, res, next) => {
|
||||
const authHeader = req.headers.authorization;
|
||||
|
||||
if (!authHeader) {
|
||||
return res.status(401).json({ error: "Token not provided" });
|
||||
}
|
||||
|
||||
const [, token] = authHeader.split(" ");
|
||||
|
||||
try {
|
||||
const decoded = await util.promisify(jwt.verify)(token, authConfig.secret);
|
||||
|
||||
const user = await User.findByPk(decoded.userId, {
|
||||
attributes: ["id", "name", "profile", "email"],
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
return res
|
||||
.status(401)
|
||||
.json({ error: "The token corresponding user does not exists." });
|
||||
}
|
||||
|
||||
req.user = user;
|
||||
|
||||
return next();
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
return res.status(401).json({ error: "Invalid Token" });
|
||||
}
|
||||
};
|
||||
35
backend/src/middleware/isAuth.ts
Normal file
35
backend/src/middleware/isAuth.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { verify } from "jsonwebtoken";
|
||||
import { Request, Response, NextFunction } from "express";
|
||||
|
||||
import AppError from "../errors/AppError";
|
||||
import authConfig from "../config/auth";
|
||||
|
||||
interface TokenPayload {
|
||||
id: string;
|
||||
username: string;
|
||||
profile: string;
|
||||
iat: number;
|
||||
exp: number;
|
||||
}
|
||||
|
||||
const isAuth = (req: Request, res: Response, next: NextFunction): void => {
|
||||
const authHeader = req.headers.authorization;
|
||||
|
||||
if (!authHeader) {
|
||||
throw new AppError("Token not provided.", 403);
|
||||
}
|
||||
|
||||
const [, token] = authHeader.split(" ");
|
||||
|
||||
const decoded = verify(token, authConfig.secret);
|
||||
const { id, profile } = decoded as TokenPayload;
|
||||
|
||||
req.user = {
|
||||
id,
|
||||
profile
|
||||
};
|
||||
|
||||
return next();
|
||||
};
|
||||
|
||||
export default isAuth;
|
||||
26
backend/src/models/Setting.ts
Normal file
26
backend/src/models/Setting.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import {
|
||||
Table,
|
||||
Column,
|
||||
CreatedAt,
|
||||
UpdatedAt,
|
||||
Model,
|
||||
PrimaryKey
|
||||
} from "sequelize-typescript";
|
||||
|
||||
@Table
|
||||
class Setting extends Model<Setting> {
|
||||
@PrimaryKey
|
||||
@Column
|
||||
key: string;
|
||||
|
||||
@Column
|
||||
value: string;
|
||||
|
||||
@CreatedAt
|
||||
createdAt: Date;
|
||||
|
||||
@UpdatedAt
|
||||
updatedAt: Date;
|
||||
}
|
||||
|
||||
export default Setting;
|
||||
@@ -15,7 +15,7 @@ class User extends Model<User> {
|
||||
@Column
|
||||
name: string;
|
||||
|
||||
@Column
|
||||
@Column(DataType.STRING)
|
||||
email: string;
|
||||
|
||||
@Column(DataType.VIRTUAL)
|
||||
@@ -43,12 +43,12 @@ class User extends Model<User> {
|
||||
}
|
||||
};
|
||||
|
||||
// static checkPassword = async ( // maybe not work like this.
|
||||
// instance: User,
|
||||
// password: string
|
||||
// ): Promise<boolean> => {
|
||||
// return compare(password, instance.passwordHash);
|
||||
// };
|
||||
public checkPassword = async (
|
||||
// maybe not work like this.
|
||||
password: string
|
||||
): Promise<boolean> => {
|
||||
return compare(password, this.getDataValue("passwordHash"));
|
||||
};
|
||||
}
|
||||
|
||||
export default User;
|
||||
|
||||
11
backend/src/routes/authRoutes.ts
Normal file
11
backend/src/routes/authRoutes.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { Router } from "express";
|
||||
import SessionController from "../controllers/SessionController";
|
||||
import * as UserController from "../controllers/UserController";
|
||||
|
||||
const authRoutes = Router();
|
||||
|
||||
authRoutes.post("/signup", UserController.store);
|
||||
|
||||
authRoutes.post("/login", SessionController);
|
||||
|
||||
export default authRoutes;
|
||||
@@ -1,8 +1,8 @@
|
||||
import { Router } from "express";
|
||||
|
||||
import userRoutes from "./userRoutes";
|
||||
import authRoutes from "./authRoutes";
|
||||
|
||||
// const AuthRoutes = require("./routes/auth");
|
||||
// const TicketsRoutes = require("./routes/tickets");
|
||||
// const MessagesRoutes = require("./routes/messages");
|
||||
// const ContactsRoutes = require("./routes/contacts");
|
||||
@@ -13,7 +13,7 @@ import userRoutes from "./userRoutes";
|
||||
const routes = Router();
|
||||
|
||||
routes.use(userRoutes);
|
||||
// routes.use("/auth", AuthRoutes);
|
||||
routes.use("/auth", authRoutes);
|
||||
// routes.use(TicketsRoutes);
|
||||
// routes.use(MessagesRoutes);
|
||||
// routes.use(ContactsRoutes);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Router } from "express";
|
||||
|
||||
// const isAuth = require("../../middleware/is-auth");
|
||||
import isAuth from "../middleware/isAuth";
|
||||
import * as UserController from "../controllers/UserController";
|
||||
|
||||
const userRoutes = Router();
|
||||
@@ -9,7 +9,7 @@ userRoutes.get("/users", (req, res) =>
|
||||
res.json({ meessage: "lets do some prettier shit here" })
|
||||
);
|
||||
|
||||
userRoutes.post("/users", UserController.store);
|
||||
userRoutes.post("/users", isAuth, UserController.store);
|
||||
|
||||
// userRoutes.put("/users/:userId", isAuth, UserController.update);
|
||||
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import "dotenv/config";
|
||||
import "express-async-errors";
|
||||
import express from "express";
|
||||
import express, { Request, Response, NextFunction } from "express";
|
||||
import cors from "cors";
|
||||
import AppError from "./errors/AppError";
|
||||
|
||||
import routes from "./routes";
|
||||
|
||||
@@ -15,14 +16,21 @@ import "./database";
|
||||
// const wbotMonitor = require("./services/wbotMonitor");
|
||||
// const Whatsapp = require("./models/Whatsapp");
|
||||
|
||||
// const Router = require("./router");
|
||||
|
||||
const app = express();
|
||||
|
||||
app.use(cors());
|
||||
app.use(express.json());
|
||||
app.use(routes);
|
||||
|
||||
app.use(async (err: Error, req: Request, res: Response, next: NextFunction) => {
|
||||
if (err instanceof AppError) {
|
||||
return res.status(err.statusCode).json({ error: err.message });
|
||||
}
|
||||
|
||||
console.error(err);
|
||||
return res.status(500).json({ error: "Internal server error" });
|
||||
});
|
||||
|
||||
const server = app.listen(process.env.PORT, () => {
|
||||
console.log(`Server started on port: ${process.env.PORT}`);
|
||||
});
|
||||
|
||||
48
backend/src/services/AuthUserSerice.ts
Normal file
48
backend/src/services/AuthUserSerice.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { sign } from "jsonwebtoken";
|
||||
|
||||
import User from "../models/User";
|
||||
import AppError from "../errors/AppError";
|
||||
import authConfig from "../config/auth";
|
||||
|
||||
interface Request {
|
||||
email: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
interface Response {
|
||||
user: User;
|
||||
token: string;
|
||||
}
|
||||
|
||||
const AuthUserService = async ({
|
||||
email,
|
||||
password
|
||||
}: Request): Promise<Response> => {
|
||||
const user = await User.findOne({
|
||||
where: { email }
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new AppError("Incorrect user/password combination.", 401);
|
||||
}
|
||||
|
||||
if (!(await user.checkPassword(password))) {
|
||||
throw new AppError("Incorrect user/password combination.", 401);
|
||||
}
|
||||
|
||||
const { secret, expiresIn } = authConfig;
|
||||
const token = sign(
|
||||
{ usarname: user.name, profile: user.profile, id: user.id },
|
||||
secret,
|
||||
{
|
||||
expiresIn
|
||||
}
|
||||
);
|
||||
|
||||
return {
|
||||
user,
|
||||
token
|
||||
};
|
||||
};
|
||||
|
||||
export default AuthUserService;
|
||||
@@ -10,44 +10,59 @@ interface Request {
|
||||
profile?: string;
|
||||
}
|
||||
|
||||
interface Response {
|
||||
email: string;
|
||||
name: string;
|
||||
id: number;
|
||||
profile: string;
|
||||
}
|
||||
|
||||
const CreateUserService = async ({
|
||||
email,
|
||||
password,
|
||||
name,
|
||||
profile = "admin"
|
||||
}: Request): Promise<User> => {
|
||||
// const schema = Yup.object().shape({
|
||||
// name: Yup.string().required().min(2),
|
||||
// email: Yup.string()
|
||||
// .email()
|
||||
// .required()
|
||||
// .test(
|
||||
// "Check-email",
|
||||
// "An user with this email already exists.",
|
||||
// async value => {
|
||||
// const emailExists = await User.findOne({
|
||||
// where: { email: value }
|
||||
// });
|
||||
// return !Boolean(emailExists);
|
||||
// }
|
||||
// ),
|
||||
// password: Yup.string().required().min(5)
|
||||
// });
|
||||
}: Request): Promise<Response> => {
|
||||
const schema = Yup.object().shape({
|
||||
name: Yup.string().required().min(2),
|
||||
email: Yup.string()
|
||||
.email()
|
||||
.required()
|
||||
.test(
|
||||
"Check-email",
|
||||
"An user with this email already exists.",
|
||||
async value => {
|
||||
if (value) {
|
||||
const emailExists = await User.findOne({
|
||||
where: { email: value }
|
||||
});
|
||||
return !emailExists;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
),
|
||||
password: Yup.string().required().min(5)
|
||||
});
|
||||
|
||||
// try {
|
||||
// await schema.validate({ email, password, name });
|
||||
// } catch (err) {
|
||||
// throw new AppError(err.message);
|
||||
// }
|
||||
try {
|
||||
await schema.validate({ email, password, name });
|
||||
} catch (err) {
|
||||
throw new AppError(err.message);
|
||||
}
|
||||
|
||||
const user = User.create({
|
||||
const user = await User.create({
|
||||
email,
|
||||
password,
|
||||
name,
|
||||
profile
|
||||
});
|
||||
|
||||
return user;
|
||||
return {
|
||||
id: user.id,
|
||||
name: user.name,
|
||||
email: user.email,
|
||||
profile: user.profile
|
||||
};
|
||||
};
|
||||
|
||||
export default CreateUserService;
|
||||
|
||||
@@ -1,21 +1,17 @@
|
||||
import { getRepository, Raw } from "typeorm";
|
||||
|
||||
import User from "../models/User";
|
||||
import AppError from "../errors/AppError";
|
||||
|
||||
const FindUserService = async (id: string): Promise<User | undefined> => {
|
||||
const usersRepository = getRepository(User);
|
||||
const user = await User.findOne({
|
||||
where: { id },
|
||||
attributes: ["name", "id", "email", "profile"]
|
||||
});
|
||||
|
||||
const user = await usersRepository.findOne({
|
||||
where: { id },
|
||||
select: ["name", "id", "email", "profile"],
|
||||
});
|
||||
if (!user) {
|
||||
throw new AppError("No user found with this ID.", 404);
|
||||
}
|
||||
|
||||
if (!user) {
|
||||
throw new AppError("No user found with this ID.", 404);
|
||||
}
|
||||
|
||||
return user;
|
||||
return user;
|
||||
};
|
||||
|
||||
export default FindUserService;
|
||||
|
||||
@@ -218,6 +218,13 @@
|
||||
resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee"
|
||||
integrity sha1-7ihweulOEdK4J7y+UnC86n8+ce4=
|
||||
|
||||
"@types/jsonwebtoken@^8.5.0":
|
||||
version "8.5.0"
|
||||
resolved "https://registry.yarnpkg.com/@types/jsonwebtoken/-/jsonwebtoken-8.5.0.tgz#2531d5e300803aa63279b232c014acf780c981c5"
|
||||
integrity sha512-9bVao7LvyorRGZCw0VmH/dr7Og+NdjYSsKAxB43OQoComFbBgsEpoR9JW6+qSq/ogwVBg8GI2MfAlk4SYI4OLg==
|
||||
dependencies:
|
||||
"@types/node" "*"
|
||||
|
||||
"@types/mime@*":
|
||||
version "2.0.3"
|
||||
resolved "https://registry.yarnpkg.com/@types/mime/-/mime-2.0.3.tgz#c893b73721db73699943bfc3653b1deb7faa4a3a"
|
||||
|
||||
Reference in New Issue
Block a user