@anmol0493/fullstack-app 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +24 -0
  3. package/backend-express/.env.example +2 -0
  4. package/backend-express/index.js +2 -0
  5. package/backend-express/package-lock.json +1939 -0
  6. package/backend-express/package.json +25 -0
  7. package/backend-express/src/config/db.js +20 -0
  8. package/backend-express/src/controller/auth.js +71 -0
  9. package/backend-express/src/middleware/auth.js +36 -0
  10. package/backend-express/src/middleware/validator.js +16 -0
  11. package/backend-express/src/models/user.js +26 -0
  12. package/backend-express/src/routes/auth.js +25 -0
  13. package/backend-express/src/server.js +28 -0
  14. package/backend-express/src/utils/constants.js +14 -0
  15. package/backend-express/src/utils/helper.js +30 -0
  16. package/backend-express/src/utils/schema/auth.js +34 -0
  17. package/backend-nestjs/.env.example +3 -0
  18. package/backend-nestjs/.prettierrc +4 -0
  19. package/backend-nestjs/README.md +99 -0
  20. package/backend-nestjs/eslint.config.mjs +35 -0
  21. package/backend-nestjs/nest-cli.json +8 -0
  22. package/backend-nestjs/package.json +99 -0
  23. package/backend-nestjs/pnpm-lock.yaml +7848 -0
  24. package/backend-nestjs/src/app.controller.ts +12 -0
  25. package/backend-nestjs/src/app.module.ts +13 -0
  26. package/backend-nestjs/src/app.service.ts +8 -0
  27. package/backend-nestjs/src/common/decorators/user.decorator.ts +8 -0
  28. package/backend-nestjs/src/common/dtos/common.dto.ts +87 -0
  29. package/backend-nestjs/src/common/enum/index.ts +0 -0
  30. package/backend-nestjs/src/common/exceptions/custom.exception.ts +28 -0
  31. package/backend-nestjs/src/common/filters/http-exception.filter.ts +46 -0
  32. package/backend-nestjs/src/common/guard/permission.guard.ts +18 -0
  33. package/backend-nestjs/src/common/middleware/auth.middleware.ts +20 -0
  34. package/backend-nestjs/src/common/pipes/validation.pipe.ts +61 -0
  35. package/backend-nestjs/src/common/utils/constants.ts +36 -0
  36. package/backend-nestjs/src/common/utils/helper.ts +43 -0
  37. package/backend-nestjs/src/core/core.module.ts +8 -0
  38. package/backend-nestjs/src/core/jwt/jwt.module.ts +10 -0
  39. package/backend-nestjs/src/core/jwt/jwt.service.ts +45 -0
  40. package/backend-nestjs/src/core/prisma/prisma.module.ts +9 -0
  41. package/backend-nestjs/src/core/prisma/prisma.service.ts +17 -0
  42. package/backend-nestjs/src/core/prisma/schema.prisma +27 -0
  43. package/backend-nestjs/src/main.ts +26 -0
  44. package/backend-nestjs/src/module/auth/auth.controller.ts +30 -0
  45. package/backend-nestjs/src/module/auth/auth.module.ts +10 -0
  46. package/backend-nestjs/src/module/auth/auth.service.ts +83 -0
  47. package/backend-nestjs/src/module/auth/dto/index.ts +28 -0
  48. package/backend-nestjs/src/module/index.module.ts +17 -0
  49. package/backend-nestjs/src/scripts/migrate.js +40 -0
  50. package/backend-nestjs/tsconfig.build.json +4 -0
  51. package/backend-nestjs/tsconfig.json +22 -0
  52. package/frontend/.env.example +1 -0
  53. package/frontend/README.md +54 -0
  54. package/frontend/eslint.config.js +28 -0
  55. package/frontend/index.html +13 -0
  56. package/frontend/package-lock.json +3813 -0
  57. package/frontend/package.json +43 -0
  58. package/frontend/public/vite.svg +1 -0
  59. package/frontend/src/App.tsx +24 -0
  60. package/frontend/src/assets/react.svg +1 -0
  61. package/frontend/src/components/Layout.tsx +59 -0
  62. package/frontend/src/components/ui/AlertDialog.tsx +47 -0
  63. package/frontend/src/components/ui/Button.tsx +61 -0
  64. package/frontend/src/components/ui/CommonAlertDialog.tsx +57 -0
  65. package/frontend/src/components/ui/FormInput.tsx +73 -0
  66. package/frontend/src/components/ui/Loader.tsx +7 -0
  67. package/frontend/src/hook/useFetchUser.ts +38 -0
  68. package/frontend/src/index.css +1 -0
  69. package/frontend/src/lib/constants.ts +24 -0
  70. package/frontend/src/lib/schema.ts +12 -0
  71. package/frontend/src/lib/utils.ts +71 -0
  72. package/frontend/src/main.tsx +11 -0
  73. package/frontend/src/pages/Home.tsx +5 -0
  74. package/frontend/src/pages/Login.tsx +67 -0
  75. package/frontend/src/pages/Signup.tsx +67 -0
  76. package/frontend/src/redux/api/auth.ts +19 -0
  77. package/frontend/src/redux/slice/auth.ts +39 -0
  78. package/frontend/src/redux/store.ts +30 -0
  79. package/frontend/src/routes/index.tsx +20 -0
  80. package/frontend/src/routes/middleware.ts +18 -0
  81. package/frontend/src/types/index.ts +12 -0
  82. package/frontend/src/vite-env.d.ts +1 -0
  83. package/frontend/tsconfig.app.json +26 -0
  84. package/frontend/tsconfig.json +7 -0
  85. package/frontend/tsconfig.node.json +24 -0
  86. package/frontend/vite.config.ts +19 -0
  87. package/package.json +34 -0
  88. package/scripts/setup.js +73 -0
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "task",
3
+ "private": true,
4
+ "version": "0.0.0",
5
+ "type": "module",
6
+ "scripts": {
7
+ "dev": "vite",
8
+ "build": "tsc -b && vite build",
9
+ "lint": "eslint .",
10
+ "preview": "vite preview"
11
+ },
12
+ "dependencies": {
13
+ "@hookform/resolvers": "^4.1.3",
14
+ "@radix-ui/react-dialog": "^1.1.6",
15
+ "@reduxjs/toolkit": "^2.6.1",
16
+ "@tailwindcss/vite": "^4.0.12",
17
+ "clsx": "^2.1.1",
18
+ "lucide-react": "^0.479.0",
19
+ "react": "^19.0.0",
20
+ "react-dom": "^19.0.0",
21
+ "react-hook-form": "^7.55.0",
22
+ "react-hot-toast": "^2.5.2",
23
+ "react-redux": "^9.2.0",
24
+ "react-router-dom": "^7.3.0",
25
+ "tailwind-merge": "^3.0.2",
26
+ "tailwindcss": "^4.0.12",
27
+ "zod": "^3.24.2"
28
+ },
29
+ "devDependencies": {
30
+ "@eslint/js": "^9.21.0",
31
+ "@types/node": "^22.13.14",
32
+ "@types/react": "^19.0.10",
33
+ "@types/react-dom": "^19.0.4",
34
+ "@vitejs/plugin-react-swc": "^3.8.0",
35
+ "eslint": "^9.21.0",
36
+ "eslint-plugin-react-hooks": "^5.1.0",
37
+ "eslint-plugin-react-refresh": "^0.4.19",
38
+ "globals": "^15.15.0",
39
+ "typescript": "~5.7.2",
40
+ "typescript-eslint": "^8.24.1",
41
+ "vite": "^6.2.0"
42
+ }
43
+ }
@@ -0,0 +1 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>
@@ -0,0 +1,24 @@
1
+ import { BrowserRouter as Router } from "react-router-dom";
2
+ import { Layout } from "./components/Layout";
3
+ import { Toaster } from "react-hot-toast";
4
+ import { AuthHandler } from "./routes/middleware";
5
+ import PageRoutes from "./routes";
6
+ import { useFetchProfile } from "./hook/useFetchUser";
7
+
8
+ function App() {
9
+ const isLoading = useFetchProfile();
10
+
11
+ if (isLoading) return <div></div>;
12
+
13
+ return (
14
+ <Router>
15
+ <AuthHandler />
16
+ <Layout>
17
+ <PageRoutes />
18
+ </Layout>
19
+ <Toaster position="bottom-right" />
20
+ </Router>
21
+ );
22
+ }
23
+
24
+ export default App;
@@ -0,0 +1 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="35.93" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 228"><path fill="#00D8FF" d="M210.483 73.824a171.49 171.49 0 0 0-8.24-2.597c.465-1.9.893-3.777 1.273-5.621c6.238-30.281 2.16-54.676-11.769-62.708c-13.355-7.7-35.196.329-57.254 19.526a171.23 171.23 0 0 0-6.375 5.848a155.866 155.866 0 0 0-4.241-3.917C100.759 3.829 77.587-4.822 63.673 3.233C50.33 10.957 46.379 33.89 51.995 62.588a170.974 170.974 0 0 0 1.892 8.48c-3.28.932-6.445 1.924-9.474 2.98C17.309 83.498 0 98.307 0 113.668c0 15.865 18.582 31.778 46.812 41.427a145.52 145.52 0 0 0 6.921 2.165a167.467 167.467 0 0 0-2.01 9.138c-5.354 28.2-1.173 50.591 12.134 58.266c13.744 7.926 36.812-.22 59.273-19.855a145.567 145.567 0 0 0 5.342-4.923a168.064 168.064 0 0 0 6.92 6.314c21.758 18.722 43.246 26.282 56.54 18.586c13.731-7.949 18.194-32.003 12.4-61.268a145.016 145.016 0 0 0-1.535-6.842c1.62-.48 3.21-.974 4.76-1.488c29.348-9.723 48.443-25.443 48.443-41.52c0-15.417-17.868-30.326-45.517-39.844Zm-6.365 70.984c-1.4.463-2.836.91-4.3 1.345c-3.24-10.257-7.612-21.163-12.963-32.432c5.106-11 9.31-21.767 12.459-31.957c2.619.758 5.16 1.557 7.61 2.4c23.69 8.156 38.14 20.213 38.14 29.504c0 9.896-15.606 22.743-40.946 31.14Zm-10.514 20.834c2.562 12.94 2.927 24.64 1.23 33.787c-1.524 8.219-4.59 13.698-8.382 15.893c-8.067 4.67-25.32-1.4-43.927-17.412a156.726 156.726 0 0 1-6.437-5.87c7.214-7.889 14.423-17.06 21.459-27.246c12.376-1.098 24.068-2.894 34.671-5.345a134.17 134.17 0 0 1 1.386 6.193ZM87.276 214.515c-7.882 2.783-14.16 2.863-17.955.675c-8.075-4.657-11.432-22.636-6.853-46.752a156.923 156.923 0 0 1 1.869-8.499c10.486 2.32 22.093 3.988 34.498 4.994c7.084 9.967 14.501 19.128 21.976 27.15a134.668 134.668 0 0 1-4.877 4.492c-9.933 8.682-19.886 14.842-28.658 17.94ZM50.35 144.747c-12.483-4.267-22.792-9.812-29.858-15.863c-6.35-5.437-9.555-10.836-9.555-15.216c0-9.322 13.897-21.212 37.076-29.293c2.813-.98 5.757-1.905 8.812-2.773c3.204 10.42 7.406 21.315 12.477 32.332c-5.137 11.18-9.399 22.249-12.634 32.792a134.718 134.718 0 0 1-6.318-1.979Zm12.378-84.26c-4.811-24.587-1.616-43.134 6.425-47.789c8.564-4.958 27.502 2.111 47.463 19.835a144.318 144.318 0 0 1 3.841 3.545c-7.438 7.987-14.787 17.08-21.808 26.988c-12.04 1.116-23.565 2.908-34.161 5.309a160.342 160.342 0 0 1-1.76-7.887Zm110.427 27.268a347.8 347.8 0 0 0-7.785-12.803c8.168 1.033 15.994 2.404 23.343 4.08c-2.206 7.072-4.956 14.465-8.193 22.045a381.151 381.151 0 0 0-7.365-13.322Zm-45.032-43.861c5.044 5.465 10.096 11.566 15.065 18.186a322.04 322.04 0 0 0-30.257-.006c4.974-6.559 10.069-12.652 15.192-18.18ZM82.802 87.83a323.167 323.167 0 0 0-7.227 13.238c-3.184-7.553-5.909-14.98-8.134-22.152c7.304-1.634 15.093-2.97 23.209-3.984a321.524 321.524 0 0 0-7.848 12.897Zm8.081 65.352c-8.385-.936-16.291-2.203-23.593-3.793c2.26-7.3 5.045-14.885 8.298-22.6a321.187 321.187 0 0 0 7.257 13.246c2.594 4.48 5.28 8.868 8.038 13.147Zm37.542 31.03c-5.184-5.592-10.354-11.779-15.403-18.433c4.902.192 9.899.29 14.978.29c5.218 0 10.376-.117 15.453-.343c-4.985 6.774-10.018 12.97-15.028 18.486Zm52.198-57.817c3.422 7.8 6.306 15.345 8.596 22.52c-7.422 1.694-15.436 3.058-23.88 4.071a382.417 382.417 0 0 0 7.859-13.026a347.403 347.403 0 0 0 7.425-13.565Zm-16.898 8.101a358.557 358.557 0 0 1-12.281 19.815a329.4 329.4 0 0 1-23.444.823c-7.967 0-15.716-.248-23.178-.732a310.202 310.202 0 0 1-12.513-19.846h.001a307.41 307.41 0 0 1-10.923-20.627a310.278 310.278 0 0 1 10.89-20.637l-.001.001a307.318 307.318 0 0 1 12.413-19.761c7.613-.576 15.42-.876 23.31-.876H128c7.926 0 15.743.303 23.354.883a329.357 329.357 0 0 1 12.335 19.695a358.489 358.489 0 0 1 11.036 20.54a329.472 329.472 0 0 1-11 20.722Zm22.56-122.124c8.572 4.944 11.906 24.881 6.52 51.026c-.344 1.668-.73 3.367-1.15 5.09c-10.622-2.452-22.155-4.275-34.23-5.408c-7.034-10.017-14.323-19.124-21.64-27.008a160.789 160.789 0 0 1 5.888-5.4c18.9-16.447 36.564-22.941 44.612-18.3ZM128 90.808c12.625 0 22.86 10.235 22.86 22.86s-10.235 22.86-22.86 22.86s-22.86-10.235-22.86-22.86s10.235-22.86 22.86-22.86Z"></path></svg>
@@ -0,0 +1,59 @@
1
+ import { useSelector } from "react-redux";
2
+ import { actions, RootState } from "../redux/store";
3
+ import { Button } from "./ui/Button";
4
+ import { LogOut, TriangleAlert } from "lucide-react";
5
+ import { cn } from "../lib/utils";
6
+ import toast from "react-hot-toast";
7
+ import { useState } from "react";
8
+ import { CommonAlertDialog } from "./ui/CommonAlertDialog";
9
+
10
+ interface LayoutProps {
11
+ children: React.ReactNode;
12
+ }
13
+
14
+ export const Layout = ({ children }: LayoutProps) => {
15
+ const { currentUser } = useSelector((state: RootState) => state.auth);
16
+ const [isDialogOpen, setIsDialogOpen] = useState(false);
17
+
18
+ const handleLogout = () => {
19
+ actions.auth.clearToken(null);
20
+ toast.success("Logged out successfully");
21
+ };
22
+
23
+ return (
24
+ <div className="h-[100dvh] w-[100dvw] overflow-hidden flex flex-col bg-gray-100">
25
+ {currentUser && (
26
+ <header className="flex justify-between px-12 items-center bg-white text-center py-4 shadow-md">
27
+ <span className="text-3xl font-sans">Project</span>
28
+ <Button variant="secondary" onClick={() => setIsDialogOpen(true)}>
29
+ <p className="flex gap-3 items-center">
30
+ <LogOut /> Logout
31
+ </p>
32
+ </Button>
33
+ <CommonAlertDialog
34
+ isOpen={isDialogOpen}
35
+ onOpenChange={setIsDialogOpen}
36
+ title={
37
+ <div className="flex justify-center items-center gap-2 pb-3">
38
+ <TriangleAlert /> Confirm Logout
39
+ </div>
40
+ }
41
+ description="Are you sure you want to log out?"
42
+ confirmText="Logout"
43
+ confirmBtnVariant="danger"
44
+ cancelText="Cancel"
45
+ onConfirm={handleLogout}
46
+ />
47
+ </header>
48
+ )}
49
+ <main
50
+ className={cn(
51
+ "flex-1 min-h-0 flex justify-center items-center",
52
+ currentUser ? "px-6 py-6" : "p-0"
53
+ )}
54
+ >
55
+ <div className="h-full w-full">{children}</div>
56
+ </main>
57
+ </div>
58
+ );
59
+ };
@@ -0,0 +1,47 @@
1
+ import * as Dialog from "@radix-ui/react-dialog";
2
+ import { cn } from "../../lib/utils";
3
+
4
+ export const AlertDialog = Dialog.Root;
5
+ export const AlertDialogTrigger = Dialog.Trigger;
6
+ export const AlertDialogContent = ({
7
+ children,
8
+ className,
9
+ ...props
10
+ }: Dialog.DialogContentProps) => (
11
+ <>
12
+ <Dialog.Overlay className="fixed inset-0 bg-black/50 z-40" />
13
+ <Dialog.Content
14
+ className={cn(
15
+ "fixed z-50 bg-white p-6 rounded-md shadow-lg max-w-md w-full",
16
+ "top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2",
17
+ className
18
+ )}
19
+ {...props}
20
+ >
21
+ {children}
22
+ </Dialog.Content>
23
+ </>
24
+ );
25
+ export const AlertDialogHeader = ({
26
+ children,
27
+ className,
28
+ ...props
29
+ }: React.HTMLAttributes<HTMLDivElement>) => (
30
+ <div className={cn("mb-4", className)} {...props}>
31
+ {children}
32
+ </div>
33
+ );
34
+ export const AlertDialogFooter = ({
35
+ children,
36
+ className,
37
+ ...props
38
+ }: React.HTMLAttributes<HTMLDivElement>) => (
39
+ <div
40
+ className={cn("flex justify-center gap-2", className)}
41
+ {...props}
42
+ >
43
+ {children}
44
+ </div>
45
+ );
46
+ export const AlertDialogTitle = Dialog.Title;
47
+ export const AlertDialogDescription = Dialog.Description;
@@ -0,0 +1,61 @@
1
+ import { ButtonHTMLAttributes } from "react";
2
+ import { cn } from "../../lib/utils";
3
+
4
+ interface ButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> {
5
+ variant?: "primary" | "secondary" | "danger";
6
+ isLoading?: boolean;
7
+ }
8
+
9
+ export const Button = ({
10
+ children,
11
+ variant = "primary",
12
+ className,
13
+ disabled,
14
+ isLoading,
15
+ ...props
16
+ }: ButtonProps) => {
17
+ const baseStyles =
18
+ "inline-flex cursor-pointer items-center justify-center px-4 py-2 border text-sm font-medium rounded-md focus:outline-none disabled:opacity-50 disabled:cursor-not-allowed";
19
+
20
+ const variants = {
21
+ primary: "border-transparent text-white bg-indigo-600 hover:bg-indigo-700",
22
+ secondary: "border-gray-300 text-gray-700 bg-white hover:bg-gray-100",
23
+ danger: "border-transparent text-white bg-red-600 hover:bg-red-700"
24
+ };
25
+
26
+ return (
27
+ <button
28
+ className={cn(baseStyles, variants[variant], className)}
29
+ disabled={disabled || isLoading}
30
+ {...props}
31
+ >
32
+ {isLoading ? (
33
+ <>
34
+ <svg
35
+ className="animate-spin -ml-1 mr-2 h-4 w-4"
36
+ xmlns="http://www.w3.org/2000/svg"
37
+ fill="none"
38
+ viewBox="0 0 24 24"
39
+ >
40
+ <circle
41
+ className="opacity-25"
42
+ cx="12"
43
+ cy="12"
44
+ r="10"
45
+ stroke="currentColor"
46
+ strokeWidth="4"
47
+ ></circle>
48
+ <path
49
+ className="opacity-75"
50
+ fill="currentColor"
51
+ d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
52
+ ></path>
53
+ </svg>
54
+ Loading...
55
+ </>
56
+ ) : (
57
+ children
58
+ )}
59
+ </button>
60
+ );
61
+ };
@@ -0,0 +1,57 @@
1
+ import {
2
+ AlertDialog,
3
+ AlertDialogContent,
4
+ AlertDialogHeader,
5
+ AlertDialogFooter,
6
+ AlertDialogTitle,
7
+ AlertDialogDescription
8
+ } from "./AlertDialog";
9
+ import { Button } from "./Button";
10
+ import { ReactNode } from "react";
11
+
12
+ interface CommonAlertDialogProps {
13
+ isOpen: boolean;
14
+ onOpenChange: (isOpen: boolean) => void;
15
+ title: ReactNode;
16
+ description: ReactNode;
17
+ confirmText: string;
18
+ confirmBtnVariant?: "primary" | "secondary" | "danger" | undefined;
19
+ cancelText: string;
20
+ onConfirm: () => void;
21
+ }
22
+
23
+ export const CommonAlertDialog = ({
24
+ isOpen,
25
+ onOpenChange,
26
+ title,
27
+ description,
28
+ confirmText,
29
+ confirmBtnVariant = "primary",
30
+ cancelText,
31
+ onConfirm
32
+ }: CommonAlertDialogProps) => {
33
+ return (
34
+ <AlertDialog open={isOpen} onOpenChange={onOpenChange}>
35
+ <AlertDialogContent>
36
+ <AlertDialogHeader>
37
+ <AlertDialogTitle>{title}</AlertDialogTitle>
38
+ <AlertDialogDescription>{description}</AlertDialogDescription>
39
+ </AlertDialogHeader>
40
+ <AlertDialogFooter>
41
+ <Button variant="secondary" onClick={() => onOpenChange(false)}>
42
+ {cancelText}
43
+ </Button>
44
+ <Button
45
+ variant={confirmBtnVariant}
46
+ onClick={() => {
47
+ onConfirm();
48
+ onOpenChange(false);
49
+ }}
50
+ >
51
+ {confirmText}
52
+ </Button>
53
+ </AlertDialogFooter>
54
+ </AlertDialogContent>
55
+ </AlertDialog>
56
+ );
57
+ };
@@ -0,0 +1,73 @@
1
+ import { UseFormRegister } from "react-hook-form";
2
+ import { cn } from "../../lib/utils";
3
+
4
+ interface FormInputProps {
5
+ label: string;
6
+ name: string;
7
+ placeholder?: string;
8
+ type?: "text" | "number" | "select" | "password";
9
+ register: UseFormRegister<any>;
10
+ error?: string;
11
+ options?: { value: string; label: string }[];
12
+ valueAsNumber?: boolean;
13
+ className?: string;
14
+ readOnly?: boolean;
15
+ }
16
+
17
+ export const FormInput = ({
18
+ label,
19
+ name,
20
+ placeholder,
21
+ type = "text",
22
+ register,
23
+ error,
24
+ options,
25
+ valueAsNumber,
26
+ readOnly,
27
+ className = ""
28
+ }: FormInputProps) => {
29
+ return (
30
+ <div className={className}>
31
+ <label className="block text-md font-medium text-gray-700 mb-2">
32
+ {label}
33
+ </label>
34
+ {type === "select" ? (
35
+ <div className="relative">
36
+ <select
37
+ {...register(name)}
38
+ className="block w-full p-2 text-gray-900 rounded-md border border-gray-300 shadow-sm focus:outline-none focus:border-gray-500 sm:text-sm appearance-none"
39
+ >
40
+ {options?.map((option) => (
41
+ <option key={option.value} value={option.value}>
42
+ {option.label}
43
+ </option>
44
+ ))}
45
+ </select>
46
+ <div className="pointer-events-none absolute inset-y-0 right-0 flex items-center px-2 text-gray-700">
47
+ <svg
48
+ className="fill-current h-4 w-4"
49
+ xmlns="http://www.w3.org/2000/svg"
50
+ viewBox="0 0 20 20"
51
+ >
52
+ <path d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z" />
53
+ </svg>
54
+ </div>
55
+ </div>
56
+ ) : (
57
+ <input
58
+ type={type}
59
+ {...register(name, {
60
+ valueAsNumber: type === "number" && valueAsNumber
61
+ })}
62
+ readOnly={readOnly}
63
+ placeholder={placeholder}
64
+ className={cn(
65
+ "block w-full p-2 text-gray-900 rounded-md border border-gray-300 shadow-sm focus:outline-none focus:border-gray-500 sm:text-sm",
66
+ readOnly && "bg-gray-100"
67
+ )}
68
+ />
69
+ )}
70
+ {error && <p className="mt-1 text-sm text-red-600">{error}</p>}
71
+ </div>
72
+ );
73
+ };
@@ -0,0 +1,7 @@
1
+ export const Loader = () => {
2
+ return (
3
+ <div className='h-full w-full bg-inherit flex justify-center items-center'>
4
+ <svg width={30} height={30} viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path strokeWidth={0.1} fill="#4F39F6" d="M12,4a8,8,0,0,1,7.89,6.7A1.53,1.53,0,0,0,21.38,12h0a1.5,1.5,0,0,0,1.48-1.75,11,11,0,0,0-21.72,0A1.5,1.5,0,0,0,2.62,12h0a1.53,1.53,0,0,0,1.49-1.3A8,8,0,0,1,12,4Z"><animateTransform attributeName="transform" type="rotate" dur="0.75s" values="0 12 12;360 12 12" repeatCount="indefinite" /></path></svg>
5
+ </div>
6
+ )
7
+ }
@@ -0,0 +1,38 @@
1
+ import { useCallback, useEffect } from "react";
2
+ import { useLazyGetUserQuery } from "../redux/api/auth";
3
+ import { actions, RootState } from "../redux/store";
4
+ import { useSelector } from "react-redux";
5
+ import { AUTH_TOKEN } from "../lib/constants";
6
+
7
+ export const useFetchProfile = () => {
8
+ const { token } = useSelector((state: RootState) => state.auth);
9
+ const [getProfile, { isLoading }] = useLazyGetUserQuery();
10
+
11
+ useEffect(() => {
12
+ if (!token) {
13
+ const storedToken = localStorage.getItem(AUTH_TOKEN);
14
+ if (storedToken) {
15
+ actions.auth.setToken(storedToken);
16
+ }
17
+ }
18
+ }, []);
19
+
20
+ const handleFetch = useCallback(async () => {
21
+ const { data: user, error } = await getProfile(null);
22
+ actions.auth.setIsLoading(isLoading);
23
+
24
+ if (!isLoading) {
25
+ if (error) {
26
+ actions.auth.setCurrentUser(null);
27
+ } else {
28
+ actions.auth.setCurrentUser(user);
29
+ }
30
+ }
31
+ }, []);
32
+
33
+ useEffect(() => {
34
+ handleFetch();
35
+ }, [token]);
36
+
37
+ return isLoading;
38
+ };
@@ -0,0 +1 @@
1
+ @import "tailwindcss";
@@ -0,0 +1,24 @@
1
+ export const AUTH_TOKEN = 'authToken';
2
+
3
+ export const SignInFields = [
4
+ {
5
+ label: 'Email',
6
+ name: 'email',
7
+ placeholder: 'Enter email'
8
+ },
9
+ {
10
+ label: 'Password',
11
+ name: 'password',
12
+ placeholder: 'Enter password',
13
+ type: "password"
14
+ }
15
+ ] as const;
16
+
17
+ export const SignUpFields = [
18
+ {
19
+ label: 'Name',
20
+ name: 'name',
21
+ placeholder: 'Enter name'
22
+ },
23
+ ...SignInFields
24
+ ] as const;
@@ -0,0 +1,12 @@
1
+ import { z } from 'zod';
2
+
3
+ export const SigninSchema = z.object({
4
+ email: z.string().email('Invalid email'),
5
+ password: z.string().min(1, 'Password is required')
6
+ });
7
+
8
+ export const SignupSchema = z.object({
9
+ name: z.string().min(1, 'Name is required'),
10
+ email: z.string().email('Invalid email'),
11
+ password: z.string().min(1, 'Password is required')
12
+ });
@@ -0,0 +1,71 @@
1
+ import { clsx, type ClassValue } from "clsx";
2
+ import { twMerge } from "tailwind-merge";
3
+ import {
4
+ BaseQueryFn,
5
+ FetchArgs,
6
+ fetchBaseQuery,
7
+ FetchBaseQueryError
8
+ } from "@reduxjs/toolkit/query/react";
9
+ import { AUTH_TOKEN } from "./constants";
10
+ import { actions } from "../redux/store";
11
+ import toast from "react-hot-toast";
12
+
13
+ export function cn(...inputs: ClassValue[]) {
14
+ return twMerge(clsx(inputs));
15
+ }
16
+
17
+ export const isAuth = (): boolean => {
18
+ const token = localStorage.getItem(AUTH_TOKEN);
19
+ if (!token) {
20
+ return false;
21
+ }
22
+
23
+ try {
24
+ const decoded = JSON.parse(atob(token.split(".")[1]));
25
+ const currentTime = Date.now() / 1000;
26
+ return decoded.exp > currentTime;
27
+ } catch (error) {
28
+ return false;
29
+ }
30
+ };
31
+
32
+ const Headers = () => localStorage.getItem(AUTH_TOKEN) || "";
33
+
34
+ export const baseQueryInterceptor = (
35
+ baseUrl: string
36
+ ): BaseQueryFn<string | FetchArgs, unknown, FetchBaseQueryError> => {
37
+ const baseQuery = fetchBaseQuery({
38
+ baseUrl: baseUrl,
39
+ prepareHeaders: (headers) => {
40
+ headers.set("Authorization", `Bearer ${Headers()}`);
41
+ return headers;
42
+ }
43
+ });
44
+
45
+ return async (args, api, extraOptions) => {
46
+ const result = await baseQuery(args, api, extraOptions);
47
+ if (result.error && result.error.status === 401) {
48
+ actions.auth.clearToken(null);
49
+ }
50
+
51
+ return result;
52
+ };
53
+ };
54
+
55
+ export const handleError = async (error: any) => {
56
+ toast.error(
57
+ error?.data?.message ? error.data.message : "Something went wrong"
58
+ );
59
+ };
60
+
61
+ export const handleSuccess = async (obj: any) => {
62
+ toast.success(obj?.message ? obj.message : "Success");
63
+ };
64
+
65
+ export const handleValue = (row: any, field: any) => {
66
+ if (!field) return "-";
67
+ if (typeof field === "function") return field(row) ?? "-";
68
+ return field
69
+ .split(".")
70
+ .reduce((acc: any, part: any) => acc && (acc[part] ?? "-"), row);
71
+ };
@@ -0,0 +1,11 @@
1
+ import { createRoot } from 'react-dom/client'
2
+ import './index.css'
3
+ import App from './App.tsx'
4
+ import { Provider } from 'react-redux'
5
+ import { store } from './redux/store.ts'
6
+
7
+ createRoot(document.getElementById('root')!).render(
8
+ <Provider store={store}>
9
+ <App />
10
+ </Provider>
11
+ )
@@ -0,0 +1,5 @@
1
+ export default function Page () {
2
+ return (
3
+ <div>Home</div>
4
+ )
5
+ }
@@ -0,0 +1,67 @@
1
+ import { useForm } from "react-hook-form";
2
+ import { LoginFormData } from "../types";
3
+ import { zodResolver } from "@hookform/resolvers/zod";
4
+ import { SigninSchema } from "../lib/schema";
5
+ import { AUTH_TOKEN, SignInFields } from "../lib/constants";
6
+ import { FormInput } from "../components/ui/FormInput";
7
+ import { Button } from "../components/ui/Button";
8
+ import { Link } from "react-router-dom";
9
+ import { useLoginMutation } from "../redux/api/auth";
10
+ import { handleError, handleSuccess } from "../lib/utils";
11
+ import { actions } from "../redux/store";
12
+
13
+ export default function Login() {
14
+ const [login, { isLoading }] = useLoginMutation();
15
+
16
+ const {
17
+ register,
18
+ handleSubmit,
19
+ formState: { errors }
20
+ } = useForm<LoginFormData>({
21
+ resolver: zodResolver(SigninSchema)
22
+ });
23
+
24
+ const onSubmit = async (data: LoginFormData) => {
25
+ const { data: res, error } = await login(data);
26
+ if (error) {
27
+ handleError(error);
28
+ } else {
29
+ localStorage.setItem(AUTH_TOKEN, res.token);
30
+ actions.auth.setToken(res.token);
31
+ handleSuccess(res);
32
+ }
33
+ };
34
+ return (
35
+ <div className="h-full flex items-center justify-center bg-white">
36
+ <div className="w-full sm:max-w-[500px] p-4 sm:p-8 sm:py-10 sm:shadow-md rounded-lg space-y-6">
37
+ <div className="text-center text-3xl font-bold text-gray-900">
38
+ Login
39
+ </div>
40
+
41
+ <form className="space-y-6" onSubmit={handleSubmit(onSubmit)}>
42
+ {SignInFields.map(({ name, ...rest }) => (
43
+ <FormInput
44
+ key={name}
45
+ name={name}
46
+ register={register}
47
+ error={errors?.[name]?.message}
48
+ {...rest}
49
+ />
50
+ ))}
51
+
52
+ <Button
53
+ type="submit"
54
+ className="w-full text-md"
55
+ isLoading={isLoading}
56
+ >
57
+ Login
58
+ </Button>
59
+
60
+ <Link to="/signup" className="text-[14px] text-blue-500 underline">
61
+ Don't have an account? Sign up
62
+ </Link>
63
+ </form>
64
+ </div>
65
+ </div>
66
+ );
67
+ }