Skip to content

Commit

Permalink
fixed session user thingy
Browse files Browse the repository at this point in the history
  • Loading branch information
Dragon1320 committed Jun 29, 2018
1 parent 9020743 commit 6420caf
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 29 deletions.
25 changes: 22 additions & 3 deletions src/web/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,29 @@ const fetchUser = discord_id => {
.then(user_doc => {
if (user_doc === null) {

return reject("User doc not found");
}
const new_user_doc = new schemas.UserSchema({
discord_id
});

new_user_doc
.save()
.then(new_user_doc => {
if (new_user_doc === null) {

resolve(user_doc);
return reject("Error on user creation");
}

resolve(new_user_doc);
})
.catch(error => {

apiLogger.error(error);
reject(error);
});
} else {

resolve(user_doc);
}
})
.catch(error => {

Expand Down
35 changes: 31 additions & 4 deletions src/web/routes/api/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ router.post("/", authAdmin, async (req, res) => {
});
} catch(error) {

apiLogger.error(error);
return res.json({ status: 500, message: "Internal Server Error", error });
}

Expand All @@ -53,6 +54,7 @@ router.post("/", authAdmin, async (req, res) => {
});
} catch(error) {

apiLogger.error(error);
return res.json({ status: 500, message: "Internal Server Error", error });
}

Expand All @@ -73,6 +75,7 @@ router.post("/", authAdmin, async (req, res) => {
await user.save();
} catch(error) {

apiLogger.error(error);
return res.json({ status: 500, message: "Internal Server Error", error });
}

Expand All @@ -81,26 +84,50 @@ router.post("/", authAdmin, async (req, res) => {

router.route("/@me").get(authUser, (req, res) => {

const user_obj = req.user.toObject();

delete user_obj._id;
delete user_obj.__v;

res.json(user_obj);

}).put(authUser, (req, res) => {


// Currently no fields that users can edit.
res.json({ status: 200, message: "OK", error: null });

}).patch(authUser, (req, res) => {

// Currently no fields that users can edit.
res.json({ status: 200, message: "OK", error: null });


}).delete(authUser, (req, res) => {
}).delete(authUser, async (req, res) => {

let del_user;
try {

del_user = await schemas.UserSchema
.findOneAndRemove({
discord_id: req.user.discord_id
});
} catch(error) {

apiLogger.error(error);
return res.json({ status: 500, message: "Internal Server Error", error });
}

if (del_user === null) {

return res.json({ status: 404, message: "Not Found", error: "The user that you are trying to delete could not be found" });
}

res.json({ status: 200, message: "OK", error: null });
});

router.route("/:discord_id").get(authAdmin, (req, res) => {



}).put(authAdmin, (req, res) => {


Expand Down
22 changes: 0 additions & 22 deletions src/web/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,28 +114,6 @@ router.get("/auth/discord/callback", async (req, res) => {
return res.json({ status: 401, message: "Unauthorized", error });
}

let user_doc;
try {

user_doc = await schemas.UserSchema.findOne({ discord_id: user_res.data.id });
} catch(error) {

apiLogger.error(error);
return res.json({ status: 401, message: "Unauthorized", error });
}

if (user_doc === null) {

try {

user_doc = await new schemas.UserSchema({ discord_id: user_res.data.id }).save();
} catch(error) {

apiLogger.error(error);
return res.json({ status: 401, message: "Unauthorized", error });
}
}

session_doc.discord.access_token = token_res.data.access_token;
session_doc.discord.token_type = token_res.data.token_type;
session_doc.discord.expires_in = token_res.data.expires_in;
Expand Down

0 comments on commit 6420caf

Please sign in to comment.