Skip to content

Commit

Permalink
fix(redis_auth): completed auth using redis
Browse files Browse the repository at this point in the history
fixes #67
  • Loading branch information
roman-ojha committed Jun 8, 2022
1 parent 4d87383 commit e7bce17
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 170 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
| React | Client Side |
| Node/Express | Backend API |
| MongoDB | No-SQL Database |
| Redis | Caching |
| Firebase | For Storing Images |

## Features:
Expand Down Expand Up @@ -61,6 +62,4 @@
### Mobile View:

[<img src="assets/UI/Home_Page_Mobile_View.png" width="49.5%" alt="Mobile_View"></img>](assets/UI/Home_Page_Mobile_View.png)
[<img src="assets/UI/ProfilePage_Mobile_View.png" width="49.5%" alt="Mobile_View"></img>](assets/UI/ProfilePage_Mobile_View.png)

<br/>
[<img src="assets/UI/ProfilePage_Mobile_View.png" width="49.5%" alt="Mobile_View"></img>](assets/UI/ProfilePage_Mobile_View.png)
285 changes: 133 additions & 152 deletions controllers/storage.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,161 +4,148 @@ import userDetail from "../models/userDetail_model.js";
import bcrypt from "bcryptjs";
import crypto from "crypto";
import compressFile from "../funcs/compressFile.js";
import varifyUser from "../funcs/varifyUser.js";
import storage from "../db/userStorageConnection.js";
import ResponseObject from "../interface/responseObject";
import { Request, Response } from "express";
const bucket = storage.bucket();

export default {
post: async (req, res): Promise<object> => {
post: async (req: Request, res: Response): Promise<object> => {
try {
const rootUser = await varifyUser(req.cookies.AuthToken);
if (!req.body.caption && !req.file) {
const rootUser = req.rootUser;
// console.log(rootUser);
const { caption, file } = req.body;
// console.log(caption,file);
if (!caption && !file) {
// if user doesn't fill the any filed
return res.status(400).json(<ResponseObject>{
success: false,
msg: "Please fill the required field",
});
} else if (req.body.caption && !req.file) {
} else if (caption && !file) {
// if user only fill content field
if (rootUser) {
const caption = req.body.caption;
const postID = crypto.randomBytes(16).toString("hex");
const userPostDetail = {
id: postID,
caption: caption,
likes: {
No: 0,
},
comments: {
No: 0,
},
// const caption = req.body.caption;
const postID = crypto.randomBytes(16).toString("hex");
const userPostDetail = {
id: postID,
caption: caption,
likes: {
No: 0,
},
comments: {
No: 0,
},
};
const postSuccessFull = await rootUser.uploadPost(
userPostDetail,
undefined
);
if (postSuccessFull) {
const resData = {
useremail: rootUser.email,
username: rootUser.name,
userID: rootUser.userID,
profilePicture: rootUser.picture,
picture: "",
// id: postRes[0].id,
// caption: postRes[0].caption,
// likes: postRes[0].likes,
// comments: postRes[0].comments,
...userPostDetail,
date: new Date(),
};
const postSuccessFull = await rootUser.uploadPost(
userPostDetail,
undefined
);
if (postSuccessFull) {
const resData = {
useremail: rootUser.email,
username: rootUser.name,
userID: rootUser.userID,
profilePicture: rootUser.picture,
picture: "",
// id: postRes[0].id,
// caption: postRes[0].caption,
// likes: postRes[0].likes,
// comments: postRes[0].comments,
...userPostDetail,
date: new Date(),
};
return res.status(200).json(<ResponseObject>{
success: true,
msg: "Post upload successfully",
data: resData,
});
}
return res.status(500).json(<ResponseObject>{
success: false,
msg: "Server Error!!, Please Try again later",
return res.status(200).json(<ResponseObject>{
success: true,
msg: "Post upload successfully",
data: resData,
});
} else {
return res
.status(401)
.json(<ResponseObject>{ success: false, msg: "UnAuthorized" });
}
return res.status(500).json(<ResponseObject>{
success: false,
msg: "Server Error!!, Please Try again later",
});
} else {
const rootUser = await varifyUser(req.cookies.AuthToken);
if (rootUser) {
// uploading image to firebase Storage
await compressFile(req.file.path);
// deleting uncompressed file after compressed
fs.unlink(`./db/Images/${req.file.filename}`, (err) => {});
const metadata = {
metadata: {
firebaseStorageDownloadTokens: uuid(),
},
cacheControl: "public, max-age=31536000",
};
const uploadRes = await bucket.upload(
`./db/build/${req.file.filename}`,
{
destination: `images/${rootUser.email}/${req.file.filename}`,
gzip: true,
metadata: metadata,
}
);
// here we are again deleting the compressed file after upload to firebase
fs.unlink(`./db/build/${req.file.filename}`, (err) => {});
// console.log(uploadRes);
const caption = req.body.caption;
const picName = req.file.filename;
const picPath = `images/${rootUser.email}/${req.file.filename}`;
const picToken =
uploadRes[0].metadata.metadata.firebaseStorageDownloadTokens;
const picBucket = process.env.FIREBASE_STORAGEBUCKET;
const picUrl = `https://firebasestorage.googleapis.com/v0/b/${picBucket}/o/${encodeURIComponent(
picPath
)}?alt=media&token=${picToken}`;
const postID = crypto.randomBytes(16).toString("hex");
const today = new Date();
const userPostDetail = {
id: postID,
caption: caption,
picture: {
name: picName,
path: picPath,
url: picUrl,
firebaseStorageDownloadToken: picToken,
bucket: picBucket,
},
likes: {
No: 0,
},
comments: {
No: 0,
},
};
const userStoryDetail = {
caption: caption,
picture: picUrl,
date: `${today.toLocaleString("default", {
month: "long",
})} ${today.getDate()}, ${today.getFullYear()}`,
// uploading image to firebase Storage
await compressFile(file.path);
// deleting uncompressed file after compressed
fs.unlink(`./db/Images/${file.filename}`, (err) => {});
const metadata = {
metadata: {
firebaseStorageDownloadTokens: uuid(),
},
cacheControl: "public, max-age=31536000",
};
const uploadRes = await bucket.upload(`./db/build/${file.filename}`, {
destination: `images/${rootUser.email}/${file.filename}`,
gzip: true,
metadata: metadata,
});
// here we are again deleting the compressed file after upload to firebase
fs.unlink(`./db/build/${file.filename}`, (err) => {});
// console.log(uploadRes);
const caption = req.body.caption;
const picName = file.filename;
const picPath = `images/${rootUser.email}/${file.filename}`;
const picToken =
uploadRes[0].metadata.metadata.firebaseStorageDownloadTokens;
const picBucket = process.env.FIREBASE_STORAGEBUCKET;
const picUrl = `https://firebasestorage.googleapis.com/v0/b/${picBucket}/o/${encodeURIComponent(
picPath
)}?alt=media&token=${picToken}`;
const postID = crypto.randomBytes(16).toString("hex");
const today = new Date();
const userPostDetail = {
id: postID,
caption: caption,
picture: {
name: picName,
path: picPath,
url: picUrl,
firebaseStorageDownloadToken: picToken,
bucket: picBucket,
},
likes: {
No: 0,
},
comments: {
No: 0,
},
};
const userStoryDetail = {
caption: caption,
picture: picUrl,
date: `${today.toLocaleString("default", {
month: "long",
})} ${today.getDate()}, ${today.getFullYear()}`,
};
const postSuccessFull = await rootUser.uploadPost(
userPostDetail,
userStoryDetail
);
if (postSuccessFull) {
const resData = {
useremail: rootUser.email,
username: rootUser.name,
userID: rootUser.userID,
profilePicture: rootUser.picture,
// id: postRes[0].id,
// caption: postRes[0].caption,
// picture: postRes[0].picture,
// likes: postRes[0].likes,
// comments: postRes[0].comments,
...userPostDetail,
date: new Date(),
};
const postSuccessFull = await rootUser.uploadPost(
userPostDetail,
userStoryDetail
);
if (postSuccessFull) {
const resData = {
useremail: rootUser.email,
username: rootUser.name,
userID: rootUser.userID,
profilePicture: rootUser.picture,
// id: postRes[0].id,
// caption: postRes[0].caption,
// picture: postRes[0].picture,
// likes: postRes[0].likes,
// comments: postRes[0].comments,
...userPostDetail,
date: new Date(),
};
return res.status(200).json(<ResponseObject>{
success: true,
msg: "Post upload successfully",
data: resData,
});
}

return res
.status(401)
.json(<ResponseObject>{ success: false, msg: "UnAuthorized" });
} else {
return res
.status(401)
.json(<ResponseObject>{ success: false, msg: "UnAuthorized" });
return res.status(200).json(<ResponseObject>{
success: true,
msg: "Post upload successfully",
data: resData,
});
}

return res
.status(401)
.json(<ResponseObject>{ success: false, msg: "UnAuthorized" });
}
} catch (err) {
return res.status(500).json(<ResponseObject>{
Expand Down Expand Up @@ -305,39 +292,33 @@ export default {
});
}
},
changeProfileUsingImgFile: async (req, res) => {
changeProfileUsingImgFile: async (req: Request, res: Response) => {
try {
const rootUser = req.rootUser;
const file = req.file;
if (file === undefined) {
return res.status(400).json({
success: false,
msg: "File/ImgUrl Doesn't exist, Please Send us File/ImgUrl",
});
}
const rootUser = await varifyUser(req.cookies.AuthToken);
if (!rootUser) {
return res.status(401).json({
success: false,
msg: "UnAuthorized user",
});
}
await compressFile(req.file.path);
fs.unlink(`./db/Images/${req.file.filename}`, (err) => {});
await compressFile(file.path);
fs.unlink(`./db/Images/${file.filename}`, (err) => {});
const metadata = {
metadata: {
firebaseStorageDownloadTokens: uuid(),
},
cacheControl: "public, max-age=31536000",
};
const uploadRes = await bucket.upload(`./db/build/${req.file.filename}`, {
destination: `images/${rootUser.email}/${req.file.filename}`,
const uploadRes = await bucket.upload(`./db/build/${file.filename}`, {
destination: `images/${rootUser.email}/${file.filename}`,
gzip: true,
metadata: metadata,
});
fs.unlink(`./db/build/${req.file.filename}`, (err) => {});
fs.unlink(`./db/build/${file.filename}`, (err) => {});
const caption = `${rootUser.userID} Update The Profile Picture`;
const picName = req.file.filename;
const picPath = `images/${rootUser.email}/${req.file.filename}`;
const picName = file.filename;
const picPath = `images/${rootUser.email}/${file.filename}`;
const picToken =
uploadRes[0].metadata.metadata.firebaseStorageDownloadTokens;
const picBucket = process.env.FIREBASE_STORAGEBUCKET;
Expand Down
5 changes: 4 additions & 1 deletion middleware/auth/authUsingRedis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ const authenticate: RequestHandler = async (
});
}
req.token = token;
req.rootUser = parsedUserDetail;
req.rootUser = {
...parsedUserDetail,
tokens: [],
};
req.userID = parsedUserDetail.userID;
next();
} else {
Expand Down
3 changes: 2 additions & 1 deletion routes/message.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import express from "express";
import authenticate from "../middleware/auth/authenticate.js";
// import authenticate from "../middleware/auth/authenticate.js";
import authenticate from "../middleware/auth/authUsingRedis.js";
import messageController from "../controllers/message.controller.js";
const messageRoute = express.Router();

Expand Down
3 changes: 2 additions & 1 deletion routes/post.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import express from "express";
import authenticate from "../middleware/auth/authenticate.js";
// import authenticate from "../middleware/auth/authenticate.js";
import authenticate from "../middleware/auth/authUsingRedis.js";
import postController from "../controllers/post.controller.js";
const postRoute = express.Router();

Expand Down
3 changes: 2 additions & 1 deletion routes/setting.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import express from "express";
import authenticate from "../middleware/auth/authenticate.js";
// import authenticate from "../middleware/auth/authenticate.js";
import authenticate from "../middleware/auth/authUsingRedis.js";
import settingController from "../controllers/setting.controller.js";
const settingRoute = express.Router();

Expand Down
Loading

0 comments on commit e7bce17

Please sign in to comment.