Skip to content

Commit

Permalink
Added logout button in creator side frontend && beautified signup and…
Browse files Browse the repository at this point in the history
… signin component
  • Loading branch information
Puneet-NJ committed Jan 3, 2025
1 parent 341658a commit d0027aa
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 208 deletions.
2 changes: 1 addition & 1 deletion backend/src/v1/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ router.get(
}
);

router.post("/logout", async (req, res) => {
router.delete("/logout", async (req, res) => {
try {
res.clearCookie("auth");

Expand Down
2 changes: 0 additions & 2 deletions backend/src/v1/utils/aws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ export const deleteImageFromS3 = (prevImageKey: string) => {
console.log(err);
return;
}

console.log(data);
}
);
};
31 changes: 25 additions & 6 deletions frontend/src/Components/Layouts/CreatorLayout.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,39 @@
import { Link } from "react-router-dom";
import { Link, useNavigate } from "react-router-dom";
import Logo from "../../utils/Logo";
import { Profile } from "../../utils/Icons";
import { Button } from "../ui/button";
import axios from "axios";
import { BACKEND_URL } from "@/utils/lib";

const CreatorLayout = ({ children }: { children: React.ReactNode }) => {
const navigate = useNavigate();

const handleLogout = () => {
axios({
method: "DELETE",
url: `${BACKEND_URL}/logout`,
withCredentials: true,
}).then(() => {
navigate("/signin");
});
};

return (
<div>
<nav className="flex justify-between items-center px-[5%] py-2 shadow-lg sticky top-0 bg-white z-20">
<Link to={"/creator/"} className="w-12">
<Logo />
</Link>

<div>
<div className="flex items-center gap-6">
<div className="w-10">
<Profile />
</div>
<div className="flex items-center gap-6">
<div>
<Button variant={"destructive"} onClick={handleLogout}>
Log out
</Button>
</div>

<div className="w-10">
<Profile />
</div>
</div>
</nav>
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/Components/Layouts/UserLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const UserLayout = ({ children }: { children: React.ReactNode }) => {

const handleLogout = () => {
axios({
method: "POST",
method: "DELETE",
url: `${BACKEND_URL}/logout`,
withCredentials: true,
}).then(() => {
Expand Down
159 changes: 73 additions & 86 deletions frontend/src/Components/SigninComp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,112 +2,99 @@ import axios from "axios";
import { useState } from "react";
import { BACKEND_URL } from "../utils/lib";
import { Link, useNavigate } from "react-router-dom";
import { Card, CardContent, CardHeader, CardTitle } from "./ui/card";
import { Input } from "./ui/input";
import { Button } from "./ui/button";

const SigninComp = () => {
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [role, setRole] = useState("student");
const navigate = useNavigate();

const handleEmailChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setEmail(e.target.value);
};

const handlePasswordChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setPassword(e.target.value);
};

const handleRoleChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
setRole(e.target.value);
};

const handleFormSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();

let userKind;
if (role === "student") userKind = "user";
else userKind = "admin";
const userKind = role === "student" ? "user" : "admin";

const response = await axios({
method: "POST",
url: `${BACKEND_URL}/${userKind}/signin`,
withCredentials: true,
data: {
email,
password,
},
});
try {
const response = await axios.post(
`${BACKEND_URL}/${userKind}/signin`,
{
email,
password,
},
{ withCredentials: true }
);

if (response.status === 200) {
if (userKind === "user") navigate("/");
else navigate("/creator/");
if (response.status === 200) {
navigate(userKind === "user" ? "/" : "/creator/");
}
} catch (error) {
console.error("Error signing in:", error);
}
};

return (
<form
onSubmit={handleFormSubmit}
className="w-1/3 px-7 py-10 flex flex-col gap-7 border shadow-xl rounded-lg bg-slate-300"
>
<div className="flex flex-col gap-2">
<label htmlFor="email" className="font-medium">
Email
</label>
<input
id="email"
placeholder="[email protected]"
value={email}
onChange={handleEmailChange}
className="py-2 px-4 border outline-none"
/>
</div>
<Card className="w-full max-w-md mx-auto mt-8 shadow-2xl">
<CardHeader>
<CardTitle className="text-center text-xl font-bold">Sign In</CardTitle>
</CardHeader>
<CardContent>
<form className="space-y-6" onSubmit={handleFormSubmit}>
<div className="space-y-2">
<label htmlFor="email" className="text-sm font-medium">
Email
</label>
<Input
id="email"
placeholder="[email protected]"
value={email}
onChange={(e) => setEmail(e.target.value)}
/>
</div>

<div className="flex flex-col gap-2">
<label htmlFor="pass" className="font-medium">
Password
</label>
<input
id="pass"
placeholder="John Doe"
type="password"
value={password}
onChange={handlePasswordChange}
className="py-2 px-4 border outline-none"
/>
</div>
<div className="space-y-2">
<label htmlFor="password" className="text-sm font-medium">
Password
</label>
<Input
id="password"
type="password"
placeholder="Enter your password"
value={password}
onChange={(e) => setPassword(e.target.value)}
/>
</div>

<div className="flex flex-col gap-2">
<label htmlFor="role" className="font-medium">
Role
</label>
<select
id="role"
name="role"
onChange={handleRoleChange}
value={role}
className="py-2 px-4 outline-none"
>
<option value="student">Student</option>
<option value="creator">Creator</option>
</select>
</div>
<div className="space-y-2">
<label htmlFor="role" className="text-sm font-medium">
Role
</label>
<select
id="role"
value={role}
onChange={(e) => setRole(e.target.value)}
className="w-full px-4 py-2 border rounded-md outline-none focus:ring-2 focus:ring-indigo-500"
>
<option value="student">Student</option>
<option value="creator">Creator</option>
</select>
</div>

<div className="flex flex-col gap-4">
<button
type="submit"
className="w-full py-2 bg-slate-400 hover:bg-slate-600 hover:text-white duration-150 font-medium rounded"
>
Sign in
</button>
<Button type="submit" className="w-full">
Sign In
</Button>

<p className="text-center text-sm font-medium">
Haven't Signed Up?{" "}
<Link to={"/signup"} className="hover:underline">
Sign Up
</Link>
</p>
</div>
</form>
<p className="text-center text-sm font-medium">
Haven't Signed Up?{" "}
<Link to="/signup" className="text-indigo-600 hover:underline">
Sign Up
</Link>
</p>
</form>
</CardContent>
</Card>
);
};

Expand Down
Loading

0 comments on commit d0027aa

Please sign in to comment.