|
@@ -7,6 +7,7 @@ const { graphqlHTTP } = require("express-graphql");
|
|
const { buildSchema } = require("graphql");
|
|
const { buildSchema } = require("graphql");
|
|
const cors = require("cors");
|
|
const cors = require("cors");
|
|
const expressJwt = require("express-jwt");
|
|
const expressJwt = require("express-jwt");
|
|
|
|
+const sha1 = require("sha1");
|
|
// const mailer = require("./smtpGmail");
|
|
// const mailer = require("./smtpGmail");
|
|
const app = express();
|
|
const app = express();
|
|
|
|
|
|
@@ -42,8 +43,14 @@ class User extends Sequelize.Model {
|
|
User.init(
|
|
User.init(
|
|
{
|
|
{
|
|
email: Sequelize.STRING,
|
|
email: Sequelize.STRING,
|
|
- password: Sequelize.STRING,
|
|
|
|
|
|
+ // password: Sequelize.STRING,
|
|
login: Sequelize.STRING,
|
|
login: Sequelize.STRING,
|
|
|
|
+ password: {
|
|
|
|
+ type: Sequelize.STRING,
|
|
|
|
+ set(value) {
|
|
|
|
+ this.setDataValue("password", sha1(value));
|
|
|
|
+ },
|
|
|
|
+ },
|
|
},
|
|
},
|
|
{ sequelize, modelName: "user" }
|
|
{ sequelize, modelName: "user" }
|
|
);
|
|
);
|
|
@@ -56,7 +63,18 @@ class Message extends Sequelize.Model {
|
|
|
|
|
|
Message.init(
|
|
Message.init(
|
|
{ message: Sequelize.STRING },
|
|
{ message: Sequelize.STRING },
|
|
- { sequelize, modelName: "message" }
|
|
|
|
|
|
+ {
|
|
|
|
+ scopes: {
|
|
|
|
+ deleted: {
|
|
|
|
+ where: {
|
|
|
|
+ id: 1
|
|
|
|
+ },
|
|
|
|
+ },
|
|
|
|
+ },
|
|
|
|
+
|
|
|
|
+ sequelize,
|
|
|
|
+ modelName: "message",
|
|
|
|
+ }
|
|
);
|
|
);
|
|
|
|
|
|
class ChatGroup extends Sequelize.Model {}
|
|
class ChatGroup extends Sequelize.Model {}
|
|
@@ -76,7 +94,6 @@ ChatGroup.belongsTo(User, { as: "autor", sourceKey: "autorId" });
|
|
ChatGroup.belongsTo(User, { as: "partner", sourceKey: "partnerId" });
|
|
ChatGroup.belongsTo(User, { as: "partner", sourceKey: "partnerId" });
|
|
|
|
|
|
const secret = `7.!BMB?Y+Bc2vZE-Hb5YuCT6QvE^FN,JWN6M?_VtFXeC5dLtB!`;
|
|
const secret = `7.!BMB?Y+Bc2vZE-Hb5YuCT6QvE^FN,JWN6M?_VtFXeC5dLtB!`;
|
|
-const secretPass = "JcFWhuLkpaK9aB3Gtbvo2Y0BApdw5q1tUyAyJeD8fJXs78d7zR";
|
|
|
|
|
|
|
|
const authenticate = async ({ login, password }) => {
|
|
const authenticate = async ({ login, password }) => {
|
|
const user = await User.findOne({ where: { login, password } });
|
|
const user = await User.findOne({ where: { login, password } });
|
|
@@ -101,7 +118,8 @@ var schema = buildSchema(`
|
|
getAllChatGroupsOneUser(id: ID!): [ChatGroup]
|
|
getAllChatGroupsOneUser(id: ID!): [ChatGroup]
|
|
}
|
|
}
|
|
type Mutation {
|
|
type Mutation {
|
|
- createUser(email: String, password: String, login: String): User
|
|
|
|
|
|
+ deleteUser(id: ID!): String
|
|
|
|
+ createUser(email: String, login: String, password: String): User
|
|
changePassword(email: String, password: String): User
|
|
changePassword(email: String, password: String): User
|
|
createMessage(message: String, autorId: String, partnerId: String): Message
|
|
createMessage(message: String, autorId: String, partnerId: String): Message
|
|
changeMessage(id: ID!, message: String): Message
|
|
changeMessage(id: ID!, message: String): Message
|
|
@@ -133,26 +151,40 @@ var schema = buildSchema(`
|
|
const getUser = async ({ email }) => await User.findOne({ where: { email } });
|
|
const getUser = async ({ email }) => await User.findOne({ where: { email } });
|
|
|
|
|
|
const getLogin = async ({ login, password }) => {
|
|
const getLogin = async ({ login, password }) => {
|
|
- const userFind = await User.findOne({ where: { login, password } });
|
|
|
|
|
|
+ const passwordSha1 = sha1(password);
|
|
|
|
+ const userFind = await User.findOne({
|
|
|
|
+ where: { login, password: passwordSha1 },
|
|
|
|
+ });
|
|
return authenticate(userFind);
|
|
return authenticate(userFind);
|
|
};
|
|
};
|
|
|
|
|
|
-const createUser = async ({ email, password, login }) => {
|
|
|
|
|
|
+const createUser = async ({ email, login, password }) => {
|
|
const wasUserCreated = await User.findOne({ where: { email } });
|
|
const wasUserCreated = await User.findOne({ where: { email } });
|
|
if (!wasUserCreated) {
|
|
if (!wasUserCreated) {
|
|
- // const passwordModification = jwt.sign(password + secretPass, secret);
|
|
|
|
- const user = { email, password, login };
|
|
|
|
|
|
+ const user = { email, login, password };
|
|
const newUser = new User(user);
|
|
const newUser = new User(user);
|
|
await newUser.save();
|
|
await newUser.save();
|
|
return await User.findOne({ where: { login } });
|
|
return await User.findOne({ where: { login } });
|
|
} else console.error("error");
|
|
} else console.error("error");
|
|
};
|
|
};
|
|
|
|
|
|
|
|
+const deleteUser = async ({ id }) => {
|
|
|
|
+ const userFind = await User.findByPk(id);
|
|
|
|
+ if (userFind) {
|
|
|
|
+ await User.destroy({ where: { id } });
|
|
|
|
+ return "User deleted";
|
|
|
|
+ } else return "User not find";
|
|
|
|
+};
|
|
|
|
+
|
|
const changePassword = async ({ email, password }) => {
|
|
const changePassword = async ({ email, password }) => {
|
|
var userFind = await User.findOne({ where: { email } });
|
|
var userFind = await User.findOne({ where: { email } });
|
|
if (userFind) {
|
|
if (userFind) {
|
|
- await User.update({ password }, { where: { email } });
|
|
|
|
|
|
+ const a = await User.update(
|
|
|
|
+ { password, login: userFind.login },
|
|
|
|
+ { where: { email } }
|
|
|
|
+ );
|
|
userFind.email = "The password has been change";
|
|
userFind.email = "The password has been change";
|
|
|
|
+ console.log(a);
|
|
return userFind;
|
|
return userFind;
|
|
} else {
|
|
} else {
|
|
userFind = { email: "Email not found" };
|
|
userFind = { email: "Email not found" };
|
|
@@ -243,6 +275,7 @@ const changeMessage = async ({ id, message }) => {
|
|
};
|
|
};
|
|
|
|
|
|
const deleteMessage = async ({ id }) => {
|
|
const deleteMessage = async ({ id }) => {
|
|
|
|
+ // await Message.scope("deleted").findByPk(id);
|
|
const messFind = await Message.findByPk(id);
|
|
const messFind = await Message.findByPk(id);
|
|
if (messFind) {
|
|
if (messFind) {
|
|
await Message.destroy({ where: { id } });
|
|
await Message.destroy({ where: { id } });
|
|
@@ -337,6 +370,7 @@ var root = {
|
|
getUser,
|
|
getUser,
|
|
getLogin,
|
|
getLogin,
|
|
createUser,
|
|
createUser,
|
|
|
|
+ deleteUser,
|
|
getAllUsers,
|
|
getAllUsers,
|
|
getMessage,
|
|
getMessage,
|
|
changePassword,
|
|
changePassword,
|
|
@@ -364,31 +398,6 @@ app.get("/users", async (req, res) => res.send(await User.findAll()));
|
|
app.get("/message", async (req, res) => res.send(await Message.findAll()));
|
|
app.get("/message", async (req, res) => res.send(await Message.findAll()));
|
|
app.get("/chat-group", async (req, res) => res.send(await ChatGroup.findAll()));
|
|
app.get("/chat-group", async (req, res) => res.send(await ChatGroup.findAll()));
|
|
|
|
|
|
-// app.post("/users", async (req, res) => {
|
|
|
|
-// // const twoUsers = async () => {
|
|
|
|
-// // const userEmail = await User.findOne({ where: { email: req.body.email } });
|
|
|
|
-// // if (userEmail !== null) console.log(err);
|
|
|
|
-// // else {
|
|
|
|
-// // console.log("hi");
|
|
|
|
-// var newUser = new User(req.body);
|
|
|
|
-// // const message = {
|
|
|
|
-// // to: req.body.email,
|
|
|
|
-// // subject: "Registered",
|
|
|
|
-// // text: `Отлично. Вот ваши данные:
|
|
|
|
-// // login: ${req.body.email}
|
|
|
|
-// // password: ${req.body.password}
|
|
|
|
-
|
|
|
|
-// // Перейдите по ссылке, чтобы войти в свой аккаунт
|
|
|
|
-// // url: http://localhost:3335/sign_in`,
|
|
|
|
-// // };
|
|
|
|
-// // mailer(message);
|
|
|
|
-// await newUser.save();
|
|
|
|
-// res.status(201).send(newUser);
|
|
|
|
-// // }
|
|
|
|
-// // };
|
|
|
|
-// // twoUsers();
|
|
|
|
-// });
|
|
|
|
-
|
|
|
|
// function errorHandler(err, req, res, next) {
|
|
// function errorHandler(err, req, res, next) {
|
|
// if (typeof err === "string") {
|
|
// if (typeof err === "string") {
|
|
// return res.status(400).json({ message: err });
|
|
// return res.status(400).json({ message: err });
|
|
@@ -415,22 +424,6 @@ app.post("/users/authenticate", async (req, res, next) => {
|
|
|
|
|
|
// app.use(jwtWare());
|
|
// app.use(jwtWare());
|
|
|
|
|
|
-app.get("/a", (req, res, next) => {
|
|
|
|
- console.log(req.headers.authorization);
|
|
|
|
- const token1 = req.headers.authorization;
|
|
|
|
- console.log(token1);
|
|
|
|
- // if (token) {
|
|
|
|
- const data = jwt.verify(token1, secret);
|
|
|
|
- console.log(data);
|
|
|
|
- // if (data) {
|
|
|
|
- console.log(data.sub.login);
|
|
|
|
- res.send(`<h1>Hello ${data.sub.login}</h1>`);
|
|
|
|
- // } else {
|
|
|
|
- // res.send(`<h1>Hello haker</h1>`);
|
|
|
|
- // }
|
|
|
|
- // }
|
|
|
|
-});
|
|
|
|
-
|
|
|
|
// (async () => {
|
|
// (async () => {
|
|
// let persone =
|
|
// let persone =
|
|
// // await User.findOne({ where: { login: "B" } }) ||
|
|
// // await User.findOne({ where: { login: "B" } }) ||
|