Skip to content

Commit

Permalink
included suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Sourabh782 committed Nov 2, 2024
1 parent 51ea0da commit 7c09828
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 16 deletions.
2 changes: 1 addition & 1 deletion backend/controller/admin.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ async function loginAdmin(req, res) {
message: "Login successful",
token,
role: "admin",
admin: { id: admin._id, name: admin.name, email: admin.email, role: "admin" },
admin: { id: admin._id, name: admin.name, email: admin.email, role: admin.role || "admin" },
});
} catch (error) {
logger.error("Error logging in admin:", {
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/Pages/Admin.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ const Admin = () => {
<div className="container grid grid-cols-1 gap-8 px-4 md:grid-cols-2 lg:grid-cols-1 md:px-6">
<div className="event-list">
{error && <p className="text-red-500">{error}</p>}
{events.map((event) => (
{events.length > 0 && events.map((event) => (
<div
key={event._id}
className="grid grid-cols-1 md:grid-cols-2 gap-10 lg:grid-cols-1 xl:grid-cols-2 md:px-6 lg:px-4 xl:px-0"
Expand Down
15 changes: 12 additions & 3 deletions frontend/src/components/Pages/Admin/AdminLogin.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ const AdminLogin = () => {

const handleSubmit = async (e) => {
e.preventDefault();
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(data.email)) {
setError('Please enter a valid email address');
return;
}
if (data.password.length < 8) {
setError('Password must be at least 8 characters long');
return;
}
setIsLoading(true);
setError(null);
try {
Expand Down Expand Up @@ -122,8 +131,8 @@ const AdminLogin = () => {
</Link>
</h3>

<a
href={`${API_URL}/api/user/auth/google`}
<Link
to={`${API_URL}/api/user/auth/google`}
className="w-full"
>
<button
Expand All @@ -132,7 +141,7 @@ const AdminLogin = () => {
>
Sign in with Google
</button>
</a>
</Link>

{error && <p className="text-red-500 mt-2">{error}</p>}

Expand Down
7 changes: 4 additions & 3 deletions frontend/src/components/Pages/Admin/AdminSignup.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ const AdminSignup = () => {
setIsLoading(false);
return;
}
if (!data.email.includes('@')) {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(data.email)) {
setError('Please enter a valid email address');
setIsLoading(false);
return;
Expand Down Expand Up @@ -163,11 +164,11 @@ const AdminSignup = () => {
Login
</Link>
</h3>
<a href={`${API_URL}/api/user/auth/google`} className="w-full">
<Link to={`${API_URL}/api/user/auth/google`} className="w-full">
<button className="button-confirm w-full h-10 rounded-md border-2 border-black bg-beige text-[17px] font-semibold shadow-[4px_4px_0px_0px_black] hover:text-green-300">
Sign up with Google
</button>
</a>
</Link>
<button
className="button-confirm w-full h-10 rounded-md border-2 border-black bg-beige text-[17px] font-semibold shadow-[4px_4px_0px_0px_black] mb-2 hover:text-green-300"
onClick={handleSubmit}
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/Pages/Login.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ const Login = () => {
body: JSON.stringify({ ...data, rememberMe }), // Include rememberMe in the body
});
const result = await response.json();
console.log(result);
// console.log(result);

if (!response) {
throw new Error(result.message || 'Login failed');
}
const res = JSON.stringify(result.user)

Cookies.set("authenticatedUser", res, {expires: 1, secure: true})
Cookies.set("authenticatedUser", res, {expires: 1, secure: true, sameSite: 'strict'})

Cookies.set('authToken', result.token, {
expires: rememberMe ? 7 : 1 / 24, // 7 days if Remember Me is checked, 1 hour otherwise
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/Pages/VerifyOtp.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import React, { useState } from 'react';
import { message } from 'antd';

const VerifyOtp = () => {
const API_URL = process.env.VITE_BACKEND_URL || 'http://localhost:3000';
const API_URL = import.meta.env.VITE_BACKEND_URL || 'http://localhost:3000';
const navigate = useNavigate(); // Use useNavigate for navigation
const [otp, setOtp] = useState("")

Expand Down
23 changes: 19 additions & 4 deletions frontend/src/context/userContext.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import React, { createContext, useState, useContext } from 'react';
import Cookies from 'js-cookie';

const UserContext = createContext();
export const UserContext = createContext({
user: null,
setUser: () => {},
});

export const UserProvider = ({ children }) => {
const data = Cookies.get("authenticatedUser");

const userData = data ? JSON.parse(data) : null;
console.log(userData);
let userData = null;
try {
userData = data ? JSON.parse(data) : null;
} catch (error) {
console.error('Invalid user data in cookie:', error);
Cookies.remove("authenticatedUser");
}

const [user, setUser] = useState(userData);

return (
Expand All @@ -17,4 +26,10 @@ export const UserProvider = ({ children }) => {
);
};

export const useUser = () => useContext(UserContext);
export const useUser = () => {
const context = useContext(UserContext);
if (context === undefined) {
throw new Error('useUser must be used within a UserProvider');
}
return context;
};
6 changes: 5 additions & 1 deletion frontend/src/router/ProtectedRoute.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import { useUser } from '../context/userContext';
const ProtectedRoute = ({ children }) => {
const {user} = useUser();

return user.role == "admin" ? children : <Navigate to="/admin-login" />;
if (!user) {
return <Navigate to="/admin-login" />;
}

return user.role === "admin" ? children : <Navigate to="/admin-login" />;
};

export default ProtectedRoute;

0 comments on commit 7c09828

Please sign in to comment.