Skip to content

Commit

Permalink
#117 backend
Browse files Browse the repository at this point in the history
  • Loading branch information
fdhhhdjd committed Nov 25, 2022
1 parent e6f2f2f commit ea6173c
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ module.exports = {
handleLogoutAdmin: async ({ user_id, token, session, res }) => {
await del(`cartUserId:${user_id}`);
res.clearCookie("refreshtoken", {
path: "/api/auth/refresh_token",
path: "/v1/api/admin/new/access",
});
session.destroy();
return {
Expand Down
7 changes: 7 additions & 0 deletions backend/src/v1/configs/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,14 @@ module.exports = {
STATUS_LOGIN_FACEBOOK: "facebook",
STATUS_LOGIN_PHONE: "phone",
STATUS_LOGIN_EMAIL: "email",

//! Connect Take Data
STORAGE_GRAPH_FACEBOOK:
"https://graph.facebook.com/v13.0/${userID}/?fields=picture.width(300).height(300),id,name,email&access_token=${accessToken}",

//! Key Redis
REDIS_BLACK_LIST: "black_list",
REDIS_MIN_LIST: 0,
REDIS_MAX_LIST: 9999999,
};

44 changes: 32 additions & 12 deletions backend/src/v1/middlewares/VerifyAcceptToken.middleware.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,41 @@
const HELPER = require("../utils/helper");
const REDIS = require("../db/redis_db")
const CONSTANTS = require("../configs/constants")
const VerifyAcceptToken = async (req, res, next) => {
try {
const token = req.headers.authorization.split(" ")[1];
let now = new Date();
const decoded = HELPER.VerifyAccToken(token);
if (decoded.exp < now.getTime() / 1000) {
return res.status(401).json({
status: 401,
success: false,
element: {
msg: "Expired Token",
},
});

if (token) {
const decoded = HELPER.VerifyAccToken(token);
let auth_user = HELPER.decodeJWT(token)

if (decoded.exp < now.getTime() / 1000) {
return res.status(401).json({
status: 401,
success: false,
element: {
msg: "Expired Token",
},
});
}

REDIS.lrange(CONSTANTS.REDIS_BLACK_LIST + ":" + auth_user.user_id || auth_user.id, CONSTANTS.REDIS_MIN_LIST, CONSTANTS.REDIS_MAX_LIST).then(result => {
if (result.indexOf(token) > -1) {
return res.status(401).json({
status: 401,
success: false,
element: {
message: "Invalid Token",
},
});
} else {
req.user = decoded;
req.token = token;
next();
}
})
}
req.user = decoded;
req.token = token;
next();
} catch (error) {
return res.status(401).json({
status: 401,
Expand Down
1 change: 1 addition & 0 deletions backend/src/v1/user_api/controllers/user.controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const userCtrl = {
GetIPUser,
res,
session,
req
});
return res.status(status).json({
status,
Expand Down
42 changes: 30 additions & 12 deletions backend/src/v1/user_api/services/user.service/user.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ const {
getInfoEveryUser,
getOrderInfoEveryUser,
} = require("./getalluser.service");
const { get, RedisPub, del } = require("../../../utils/limited_redis");
const { get, RedisPub } = require("../../../utils/limited_redis");
const PASSWORD = require("../../../utils/password");
const STORAGE = require("../../../utils/storage");
const CONSTANTS = require("../../../configs/constants");
const CONFIGS = require("../../../configs/config");
const Users = require("../../../models/userModel");
const REDIS = require("../../../db/redis_db")
module.exports = {
//*--------------- Handle Authentication Users ---------------
checkLoginUser: async ({
Expand All @@ -53,6 +54,7 @@ module.exports = {
GetIPUser,
res,
session,
req
}) => {
const { status, _ttl, msg } = UserSpam(GetIPUser);
if (status === 400) {
Expand Down Expand Up @@ -85,7 +87,7 @@ module.exports = {
session.save();
const accessToken = createAccessToken({ id: result_user._id });
const refreshToken = await GenerateRefreshToken({ id: result_user._id });
saveCookies(res, refreshToken);
saveCookies(res, refreshToken, req);
return {
status: 200,
success: true,
Expand Down Expand Up @@ -280,17 +282,33 @@ module.exports = {
};
},
LogoutRemoveAllUser: async ({ user_id, token, session, res }) => {
await del(user_id);
res.clearCookie("refreshtoken", {
path: "/api/user/new/accessToken",
});
session.destroy();
return {
status: 200,
success: true,
element: { msg: "Logged out success" },
};
let redis_multi = REDIS.pipeline()
.lpush(`${CONSTANTS.REDIS_BLACK_LIST}:${user_id}`, token)
.del(user_id)

redis_multi.exec().then(rs => {
if (rs) {
res.clearCookie("refreshtoken", {
path: "/api/user/new/accessToken",
});
session.destroy();
}
return {
status: 200,
success: true,
element: { msg: "Logged out success" },
};
}).catch(err => {
return {
status: 503,
success: false,
element: { msg: "Server busy" },
};
})


},

HandleForgerPasswordUser: async ({ email, req }) => {
const { status, success, element } = await CheckForget({
email,
Expand Down
6 changes: 4 additions & 2 deletions backend/src/v1/utils/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,14 @@ module.exports = {
});
},
//* cookie
saveCookies(res, refreshToken) {
saveCookies(res, refreshToken, req) {
res.cookie("refreshtoken", refreshToken, {
httpOnly: CONFIGS.NODE_ENV === "PRODUCTION" ? true : false,
sameSite: CONFIGS.NODE_ENV === "PRODUCTION" ? true : false,
secure: CONFIGS.NODE_ENV === "PRODUCTION" ? true : false,
path: "/api/user/new/accessToken",
maxAge: CONSTANTS._7_DAY,
domain: req.host.split(':')[0] || ''
});
},
//* cookie Admin
Expand All @@ -194,8 +195,9 @@ module.exports = {
httpOnly: CONFIGS.NODE_ENV === "PRODUCTION" ? true : false,
sameSite: CONFIGS.NODE_ENV === "PRODUCTION" ? true : false,
secure: CONFIGS.NODE_ENV === "PRODUCTION" ? true : false,
path: "http://localhost:8080/v1/api/admin/new/access",
path: "/v1/api/admin/new/access",
maxAge: CONSTANTS._7_DAY,
domain: req.host.split(':')[0] || ''
});
},
//*Handle RefetchToken
Expand Down
Binary file modified dump.rdb
Binary file not shown.

0 comments on commit ea6173c

Please sign in to comment.