Skip to content

Commit

Permalink
setting
Browse files Browse the repository at this point in the history
  • Loading branch information
imdeveshshukla committed Aug 20, 2024
1 parent 479365e commit d01220c
Show file tree
Hide file tree
Showing 10 changed files with 398 additions and 205 deletions.
2 changes: 1 addition & 1 deletion backend/.dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ node_modules

# Ignore the bugs directory if it contains debugging or log files
bugs

Details
# Ignore Prisma directory if it contains files that are only needed locally
# prisma

Expand Down
3 changes: 2 additions & 1 deletion backend/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
# Keep environment variables out of version control
.env
bugs
bugs
Details
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- AlterTable
ALTER TABLE "User" ADD COLUMN "codeforces" TEXT,
ADD COLUMN "showCf" BOOLEAN NOT NULL DEFAULT false;
3 changes: 3 additions & 0 deletions backend/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ model User{
leetcode String?
showLC Boolean @default(false)
codeforces String?
showCf Boolean @default(false)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
Expand Down
10 changes: 6 additions & 4 deletions backend/src/controller/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ const refreshSignIn = async (req, res) => {
const logout = async (req, res) => {
try {
const token = req.headers.cookie.split("=")[1];
console.log(token);
// console.log(token);
if (!token) {
res.status(404).send({ msg: "No token found" });
}
Expand All @@ -340,12 +340,11 @@ const logout = async (req, res) => {
const user = await prisma.user.findUnique({
where: { email: payload.email },
});
console.log(user?.email);
// console.log(user?.email);
if(!user) return res.status(400).send("User not Found");
res
.clearCookie(user?.userID, "", {
path: "/",
// expires: new Date(Date.now() + 1000 * 1),
httpOnly: true,
sameSite: "lax",
})
Expand All @@ -354,7 +353,10 @@ const logout = async (req, res) => {
} else res.status(401).send({ msg: "Invalid Token" });
// console.log(req.body.email);
} catch (error) {
throw error;
res.status(500).json({
msg:"Server Issue",
error
})
}
};

Expand Down
83 changes: 67 additions & 16 deletions backend/src/controller/user.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
import prisma from "../../db/db.config.js";
import jwt from "jsonwebtoken";
import bcrypt from "bcrypt";
import uploadOnCloudinary from "../utils/cloudinary.js";

// const varifyToken = (req, res, next) => {
// const token = req.headers.cookie.split("=")[1];

// if (!token) {
// res.status(401).send({ msg: "No token found" });
// }
// let payload = jwt.verify(token, process.env.SECRET_KEY);
// if (payload.email) {
// req.email=payload.email
// next();
// } else res.status(401).send({ msg: "Invalid Token" });
// };

const getUser = async (req, res) => {
const email = req.params?.email;
if (typeof email !== "string")
Expand All @@ -35,6 +23,8 @@ const getUser = async (req, res) => {
username: true,
leetcode:true,
showLC:true,
codeforces:true,
showCf:true,
posts: {
select: {
id: true,
Expand Down Expand Up @@ -98,14 +88,14 @@ const getUser = async (req, res) => {
};

const uploadImg = async (req, res) => {
console.log("in controller");
// console.log("in controller");

let imgurl = null;
const userId = req.userId;

if (req.file) {
imgurl = await uploadOnCloudinary(req.file.path);
console.log("file Object = " + imgurl);
// console.log("file Object = " + imgurl);
}
try {
const user = await prisma.user.update({
Expand Down Expand Up @@ -161,7 +151,7 @@ const getUserPost = async (req, res) => {

const getNotifications = async (req, res) => {
const id = req.userId;
console.log("Notification :For id", req.userId);
// console.log("Notification :For id", req.userId);

try {
const data = await prisma.notification.findMany({
Expand Down Expand Up @@ -286,9 +276,70 @@ const setLcVisibility = async(req,res)=>{
}
}

const update = async(req,res)=>{
const body = req.body;
const userId = req.userId;
var hashPass = undefined;
if(body.password)hashPass = bcrypt.hashSync(body.password, 10);

try {
const user = await prisma.user.update({
where:{
userID:userId,
},
data:{
bio:body.bio?body.bio:undefined,
username:body.username?body.username:undefined,
password:hashPass,
codeforces:body.rank?body.rank:undefined,
showCf:body.showCF?body.showCF:undefined
}
});
res.status(201).json({
msg:"Succesfully Updated",
user
})
} catch (error) {
res.status(500).json({
msg:"Server/Database Error",
error
});
}
}

const deleteAcct = async(req,res)=>{
const userID = req.userId;
try {
console.log(userID);
const user = await prisma.user.delete({
where:{
userID
}
})
console.log(user);
return res
.clearCookie(user?.userID, "", {
path: "/",
httpOnly: true,
sameSite: "lax",
})
.status(200)
.json({
msg:"Success",
user
});
} catch (error) {
return res.status(500).json({
msg:"Server/Database Issue",
error
})
}
}
// const userController={getUser,varifyToken};
const userController = {
getUser,
update,
deleteAcct,
uploadImg,
getUserPost,
getNotifications,
Expand Down
3 changes: 2 additions & 1 deletion backend/src/routes/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ router.post('/markallasread',verifyToken, userController.markAllAsRead );
router.post("/sendnotification",verifyToken,userController.sendNotification);
router.post("/addLeetCode",verifyToken,userController.addLC);
router.post("/setLcVisibility",verifyToken,userController.setLcVisibility);

router.post("/update",verifyToken,userController.update);
router.delete("/deleteAccount",verifyToken,userController.deleteAcct);
router.get('/:email',verifyToken, userController.getUser );


Expand Down
3 changes: 2 additions & 1 deletion frontend/src/components/ProfileLayout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ const ProfileLayout = ({ isLoading, user }) => {
</div>
<div className='flex flex-col gap-1'>
<div className=' text-lg xxs:text-2xl break-words xs:text-3xl font-bold'>{user.username}</div>
<div className=' text-sm xxs:text-base font-semibold text-gray-700'>u/{user.username}</div>
<div className=' text-xs font-thin xxs:text-base font-semibold text-gray-700'>u/{user.username}</div>
<div className=' text-sm xxs:text-base break-words font-semibold text-gray-800'>{userInfo?.bio}</div>
</div>


Expand Down
Loading

0 comments on commit d01220c

Please sign in to comment.