Skip to content

Commit

Permalink
Freeze account
Browse files Browse the repository at this point in the history
  • Loading branch information
burakorkmez committed Sep 28, 2023
1 parent 0ba5d97 commit 3067dfe
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 3 deletions.
32 changes: 31 additions & 1 deletion backend/controllers/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ const loginUser = async (req, res) => {

if (!user || !isPasswordCorrect) return res.status(400).json({ error: "Invalid username or password" });

if (user.isFrozen) {
user.isFrozen = false;
await user.save();
}

generateTokenAndSetCookie(user._id, res);

res.status(200).json({
Expand Down Expand Up @@ -218,4 +223,29 @@ const getSuggestedUsers = async (req, res) => {
}
};

export { signupUser, loginUser, logoutUser, followUnFollowUser, updateUser, getUserProfile, getSuggestedUsers };
const freezeAccount = async (req, res) => {
try {
const user = await User.findById(req.user._id);
if (!user) {
return res.status(400).json({ error: "User not found" });
}

user.isFrozen = true;
await user.save();

res.status(200).json({ success: true });
} catch (error) {
res.status(500).json({ error: error.message });
}
};

export {
signupUser,
loginUser,
logoutUser,
followUnFollowUser,
updateUser,
getUserProfile,
getSuggestedUsers,
freezeAccount,
};
4 changes: 4 additions & 0 deletions backend/models/userModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ const userSchema = mongoose.Schema(
type: String,
default: "",
},
isFrozen: {
type: Boolean,
default: false,
},
},
{
timestamps: true,
Expand Down
2 changes: 2 additions & 0 deletions backend/routes/userRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
signupUser,
updateUser,
getSuggestedUsers,
freezeAccount,
} from "../controllers/userController.js";
import protectRoute from "../middlewares/protectRoute.js";

Expand All @@ -19,5 +20,6 @@ router.post("/login", loginUser);
router.post("/logout", logoutUser);
router.post("/follow/:id", protectRoute, followUnFollowUser); // Toggle state(follow/unfollow)
router.put("/update/:id", protectRoute, updateUser);
router.put("/freeze", protectRoute, freezeAccount);

export default router;
2 changes: 2 additions & 0 deletions frontend/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import userAtom from "./atoms/userAtom";
import UpdateProfilePage from "./pages/UpdateProfilePage";
import CreatePost from "./components/CreatePost";
import ChatPage from "./pages/ChatPage";
import { SettingsPage } from "./pages/SettingsPage";
function App() {
const user = useRecoilValue(userAtom);
const { pathname } = useLocation();
Expand Down Expand Up @@ -37,6 +38,7 @@ function App() {
/>
<Route path='/:username/post/:pid' element={<PostPage />} />
<Route path='/chat' element={user ? <ChatPage /> : <Navigate to={"/auth"} />} />
<Route path='/settings' element={user ? <SettingsPage /> : <Navigate to={"/auth"} />} />
</Routes>
</Container>
</Box>
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/components/Header.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { FiLogOut } from "react-icons/fi";
import useLogout from "../hooks/useLogout";
import authScreenAtom from "../atoms/authAtom";
import { BsFillChatQuoteFill } from "react-icons/bs";
import { MdOutlineSettings } from "react-icons/md";

const Header = () => {
const { colorMode, toggleColorMode } = useColorMode();
Expand Down Expand Up @@ -44,6 +45,9 @@ const Header = () => {
<Link as={RouterLink} to={`/chat`}>
<BsFillChatQuoteFill size={20} />
</Link>
<Link as={RouterLink} to={`/settings`}>
<MdOutlineSettings size={20} />
</Link>
<Button size={"xs"} onClick={logout}>
<FiLogOut size={20} />
</Button>
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/hooks/useGetUserProfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ const useGetUserProfile = () => {
showToast("Error", data.error, "error");
return;
}
if (data.isFrozen) {
setUser(null);
return;
}
setUser(data);
} catch (error) {
showToast("Error", error.message, "error");
Expand Down
42 changes: 42 additions & 0 deletions frontend/src/pages/SettingsPage.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Button, Text } from "@chakra-ui/react";
import useShowToast from "../hooks/useShowToast";
import useLogout from "../hooks/useLogout";

export const SettingsPage = () => {
const showToast = useShowToast();
const logout = useLogout();

const freezeAccount = async () => {
if (!window.confirm("Are you sure you want to freeze your account?")) return;

try {
const res = await fetch("/api/users/freeze", {
method: "PUT",
headers: { "Content-Type": "application/json" },
});
const data = await res.json();

if (data.error) {
return showToast("Error", data.error, "error");
}
if (data.success) {
await logout();
showToast("Success", "Your account has been frozen", "success");
}
} catch (error) {
showToast("Error", error.message, "error");
}
};

return (
<>
<Text my={1} fontWeight={"bold"}>
Freeze Your Account
</Text>
<Text my={1}>You can unfreeze your account anytime by logging in.</Text>
<Button size={"sm"} colorScheme='red' onClick={freezeAccount}>
Freeze
</Button>
</>
);
};
6 changes: 4 additions & 2 deletions frontend/src/pages/UserPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const UserPage = () => {

useEffect(() => {
const getPosts = async () => {
if (!user) return;
setFetchingPosts(true);
try {
const res = await fetch(`/api/posts/user/${username}`);
Expand All @@ -32,15 +33,16 @@ const UserPage = () => {
};

getPosts();
}, [username, showToast, setPosts]);
console.log("posts is here and it is recoil state", posts);
}, [username, showToast, setPosts, user]);

if (!user && loading) {
return (
<Flex justifyContent={"center"}>
<Spinner size={"xl"} />
</Flex>
);
}

if (!user && !loading) return <h1>User not found</h1>;

return (
Expand Down

0 comments on commit 3067dfe

Please sign in to comment.