Skip to content

Commit

Permalink
jwtTokenExpiration
Browse files Browse the repository at this point in the history
  • Loading branch information
imdeveshshukla committed Sep 4, 2024
1 parent 6cfc6fb commit cff0b11
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 29 deletions.
29 changes: 13 additions & 16 deletions backend/src/controller/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const usersSignInSchema = z.object({

const sendEmailVarification = async ({ userID, username, email }, res) => {
try {
console.log(userID, email);
// console.log(userID, email);

const otp = `${Math.floor(1000 + Math.random() * 9000)}`;
const hashOtp = await bcrypt.hash(otp, 10);
Expand Down Expand Up @@ -55,7 +55,7 @@ const sendEmailVarification = async ({ userID, username, email }, res) => {
userID: userID,
},
});
console.log(isUser);
// console.log(isUser);

if (isUser) {
const updateUser = await prisma.userVarify.update({
Expand All @@ -68,7 +68,7 @@ const sendEmailVarification = async ({ userID, username, email }, res) => {
expiresAt: Date.now() + 180000,
},
});
console.log(updateUser);
// console.log(updateUser);
} else {
const userdata = await prisma.userVarify.create({
data: {
Expand All @@ -78,7 +78,7 @@ const sendEmailVarification = async ({ userID, username, email }, res) => {
expiresAt: Date.now() + 180000,
},
});
console.log(userdata);
// console.log(userdata);
}

res
Expand All @@ -103,10 +103,7 @@ const signup = async (req, res) => {
res.json({ message: "Credentials cannot be empty" });
}
const hashPass = bcrypt.hashSync(password, 10);
//here checks --------------
var token = jwt.sign({ email: email }, process.env.SECRET_KEY, {
expiresIn: "1d",
});


const isuser = await prisma.user.findFirst({
where: {
Expand Down Expand Up @@ -145,7 +142,7 @@ const signup = async (req, res) => {
},
});
}
console.log(newUser);
// console.log(newUser);
await sendEmailVarification(newUser, res);
} catch (error) {
console.log(error);
Expand Down Expand Up @@ -195,7 +192,7 @@ const varifyOtp = async (req, res) => {
{ userId: userID, email: email },
process.env.SECRET_KEY,
{
expiresIn: 24 * 60 * 60,
expiresIn: 24 * 60 * 60 * 7,
}
);
const updateUser = await prisma.user.update({
Expand Down Expand Up @@ -229,7 +226,7 @@ const varifyOtp = async (req, res) => {
};

const resendOtp = async (req, res) => {
console.log(req.body);
// console.log(req.body);
try {
await sendEmailVarification(req.body, res);
} catch (error) {
Expand All @@ -239,7 +236,7 @@ const resendOtp = async (req, res) => {

const resetPassword = async (req, res) => {
const { email } = req.body;
console.log(req.body);
// console.log(req.body);

const user = await prisma.user.findUnique({
where: {
Expand Down Expand Up @@ -301,7 +298,7 @@ const signin = async (req, res) => {
],
},
});
console.log(user);
// console.log(user);

if (!user) {
return res
Expand All @@ -313,15 +310,15 @@ const signin = async (req, res) => {
{ userId: user.userID, email: user.email },
process.env.SECRET_KEY,
{
expiresIn: 24 * 60 * 60,
expiresIn: 24 * 60 * 60 * 7,
}
);
console.log(token);
// console.log(token);

const hashPass = user.password;
const isVarified = user.isVarified;
const validPass = bcrypt.compareSync(password, hashPass);
console.log(validPass);
// console.log(validPass);
if (!isVarified) {
res.status(404).send({ msg: "User is not varified!" });
}
Expand Down
14 changes: 7 additions & 7 deletions backend/src/controller/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ export const createPost = async (req, res) => {
if (req.file) {
try {
url = await uploadOnCloudinary(req.file.path);
console.log("file Object = " + url);
// console.log("file Object = " + url);
} catch (err) {
console.log("Failed To Upload Image\n",err);
const message = err.message.split(".")[0]
console.log(message)
// console.log(message)
return res.status(405).json({
msg:message
})
}
}
const parsedBody = post.safeParse(postbody);
console.log(parsedBody);
// console.log(parsedBody);

if (parsedBody.error)
res.status(405).json({
Expand Down Expand Up @@ -87,7 +87,7 @@ export const getPost = async (req, res) => {
const offset = (page - 1) * limit;
const roomTitle = req.query?.title;
// console.log(offset, limit);
console.log(roomTitle);
// console.log(roomTitle);

try {
const posts = await prisma.post.findMany({
Expand Down Expand Up @@ -192,7 +192,7 @@ export const getHotPost = async (req, res) => {
export const deletePost = async(req,res) =>{
const userId = req.userId;
const id = req.body.id;
console.log(id);
// console.log(id);
try{

const post = await prisma.post.findFirst({
Expand Down Expand Up @@ -278,8 +278,8 @@ let posts = JSON.parse(stringify(result));
return (p.privateRoom == null || p.privateRoom!=true)
})
const postIds = posts.map(post => post.id);
console.log("Inside Poular")
console.log(posts);
// console.log("Inside Poular")
// console.log(posts);
const upvotes = await prisma.upvote.findMany({
where: {
postId: {
Expand Down
8 changes: 4 additions & 4 deletions backend/src/controller/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export const getUserPosts = async (req, res) => {
const offset = parseInt(req.query.offset) || (page - 1) * limit;
// console.log(page, offset);
const { userID, username } = req.query;
console.log(userID, username);
// console.log(userID, username);


try {
Expand Down Expand Up @@ -132,9 +132,9 @@ export const getUserComments = async (req, res) => {
const page = parseInt(req.query.page) || 1;
const limit = parseInt(req.query.limit) || 10;
const offset = parseInt(req.query.offset) || (page - 1) * limit;
console.log(page, offset);
// console.log(page, offset);
const { userID, username } = req.query;
console.log(userID);
// console.log(userID);


try {
Expand Down Expand Up @@ -166,7 +166,7 @@ export const getUserComments = async (req, res) => {
const altcomments = comments.filter((cmt)=>{
return (cmt.post.room == null || cmt.post.room.privateRoom == false);
})
console.log(altcomments);
// console.log(altcomments);

res.status(200).send(altcomments);

Expand Down
3 changes: 1 addition & 2 deletions backend/src/middlewares/verifytoken.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ export const verifyToken = (req, res, next) => {
if (payload.email) {
req.userId = payload.userId;
req.email = payload.email;
console.log("Token varified");
console.log(req?.body || "Request Not found");
// console.log("Token varified");
next();
} else res.status(401).send({ msg: "Invalid Token" });

Expand Down

0 comments on commit cff0b11

Please sign in to comment.