Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 36 additions & 24 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"react-dom": "^18.3.1",
"react-hook-form": "^7.50.1",
"react-redux": "^9.2.0",
"react-router-dom": "^6.22.1",
"react-router-dom": "^7.6.1",
"zod": "^3.22.4"
},
"devDependencies": {
Expand Down
26 changes: 0 additions & 26 deletions src/App.jsx

This file was deleted.

44 changes: 24 additions & 20 deletions src/components/Navbar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const Navbar = () => {
<header className="bg-white shadow-sm">
<div className="container mx-auto px-4 py-4">
<nav className="flex items-center justify-between">


<Link
to="/"
className="flex items-center gap-2 text-yellow-600 font-bold text-xl"
Expand All @@ -34,26 +36,27 @@ const Navbar = () => {
</Link>

<div className="flex gap-4">


{isAuthenticated ? (
<>

<Link
to="/"
className={`flex items-center gap-1 px-3 py-2 rounded-md transition-colors ${
pathname === "/"
? "bg-yellow-100 text-yellow-700"
: "hover:bg-gray-100 text-gray-700"
}`}
className={`flex items-center gap-1 px-3 py-2 rounded-md transition-colors ${pathname === "/"
? "bg-yellow-100 text-yellow-700"
: "hover:bg-gray-100 text-gray-700"
}`}
>
<Plus size={18} />
<span>Create</span>
</Link>

<Link
to="/notes"
className={`flex items-center gap-1 px-3 py-2 rounded-md transition-colors ${
pathname === "/notes"
? "bg-yellow-100 text-yellow-700"
: "hover:bg-gray-100 text-gray-700"
}`}
className={`flex items-center gap-1 px-3 py-2 rounded-md transition-colors ${pathname === "/notes"
? "bg-yellow-100 text-yellow-700"
: "hover:bg-gray-100 text-gray-700"
}`}
>
<List size={18} />
<span>View All</span>
Expand All @@ -66,32 +69,33 @@ const Navbar = () => {
<LogOut size={18} />
<span>Logout</span>
</button>


</>
) : (
<>

<Link
to="/login"
className={`flex items-center gap-1 px-3 py-2 rounded-md transition-colors ${
pathname === "/login"
className={`flex items-center gap-1 px-3 py-2 rounded-md transition-colors ${pathname === "/login"
? "bg-yellow-100 text-yellow-700"
: "hover:bg-gray-100 text-gray-700"
}`}
}`}
>
<LogIn size={18} />
<span>Login</span>
</Link>

<Link
to="/register"
className={`flex items-center gap-1 px-3 py-2 rounded-md transition-colors ${
pathname === "/register"
className={`flex items-center gap-1 px-3 py-2 rounded-md transition-colors ${pathname === "/register"
? "bg-yellow-100 text-yellow-700"
: "hover:bg-gray-100 text-gray-700"
}`}
}`}
>
<UserPlus size={18} />
<span>Register</span>
</Link>

</>
)}
</div>
</nav>
</div>
Expand Down
10 changes: 8 additions & 2 deletions src/components/auth/ProtectedRoute.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { Navigate, useLocation } from "react-router-dom";
import { useSelector } from "react-redux";

const ProtectedRoute = ({ children, requireAuth }) => {
const { isAuthenticated, loading } = useSelector((state) => state.auth);
const { isAuthenticated, status} = useSelector((state) => state.auth);

// Show loading state while checking authentication
if (loading) {
if (status === "loading") {
return (
<div className="flex items-center justify-center min-h-[60vh]">
<div className="text-center">
Expand All @@ -17,9 +17,15 @@ const ProtectedRoute = ({ children, requireAuth }) => {
}

// TODO: If route requires authentication and user is not authenticated, redirect to login
if (requireAuth && !isAuthenticated){
<Navigate to ="/login" replace />
}


//TODO: If route requires unauthenticated user and user is authenticated, redirect to notes
if (!requireAuth && isAuthenticated){
<Navigate to ="/notes" replace />
}


// Otherwise, render the children
Expand Down
6 changes: 4 additions & 2 deletions src/main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ import { createRoot } from "react-dom/client";
import { Provider } from "react-redux";
import { BrowserRouter } from "react-router-dom";
import store from "./store";
import App from "./App.jsx";
import App from "./store/App.jsx";
import "./index.css";

createRoot(document.getElementById("root")).render(
<StrictMode>
<Provider store={store}>
<BrowserRouter>
<BrowserRouter>
<App />
</BrowserRouter>
</Provider>
</StrictMode>
);


2 changes: 1 addition & 1 deletion src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
import App from './App.tsx';
import App from './store/App.jsx';
import './index.css';

createRoot(document.getElementById('root')!).render(
Expand Down
53 changes: 51 additions & 2 deletions src/pages/Login.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,47 @@
import React from "react";
import { useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { useDispatch, useSelector } from "react-redux";
import { useNavigate } from "react-router-dom";
import {login} from "../store/slices/authSlice";
import {loginSchema} from "../schema/authSchema";

const Login = () => {
const dispatch = useDispatch();
const navigate = useNavigate();
const { status, error } = useSelector((state) => state.auth);

const {
register,
handleSubmit,
formState: { errors },
} = useForm({
resolver: zodResolver(loginSchema),
});

const onSubmit = async (data) => {
try {
await dispatch(login(data)).unwrap();
navigate("/notes");
} catch (err) {
console.error("Failed to login:", err);
}
};


return (
<div className="min-h-[60vh] flex items-center justify-center">
<div className="bg-white p-8 rounded-lg shadow-md w-full max-w-md">
<h2 className="text-2xl font-bold text-center text-gray-800 mb-6">
Login to Your Account
</h2>
{error && (
<div className="mb-4 rounded-md bg-red-50 p-4 text-red-600">
{error}
</div>
)}

<form className="space-y-4">
<form onSubmit={handleSubmit(onSubmit)} className="space-y-4">
<div>
<label
htmlFor="email"
Expand All @@ -17,9 +52,15 @@ const Login = () => {
<input
type="email"
id="email"
{...register("email")}
className="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-yellow-500 focus:ring-yellow-500"
placeholder="Enter your email"
/>
{errors.email && (
<p className="mt-1 text-sm text-red-600">
{errors.email.message}
</p>
)}
</div>

<div>
Expand All @@ -32,16 +73,24 @@ const Login = () => {
<input
type="password"
id="password"
{...register("password")}
className="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-yellow-500 focus:ring-yellow-500"
placeholder="Enter your password"
/>
{errors.password && (
<p className="mt-1 text-sm text-red-600">
{errors.password.message}
</p>
)}
</div>

<button
type="submit"
disabled={status === "loading"}
className="w-full bg-yellow-500 text-white py-2 px-4 rounded-md hover:bg-yellow-600 focus:outline-none focus:ring-2 focus:ring-yellow-500 focus:ring-offset-2"
>
Login

{status === "loading" ? "Logging in..." : "Login"}
</button>
</form>

Expand Down
Loading