create-next-structure 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 (40) hide show
  1. package/LICENSE +7 -0
  2. package/README.md +51 -0
  3. package/bin/index.js +90 -0
  4. package/package.json +44 -0
  5. package/templates/.env.template +36 -0
  6. package/templates/app/(dashboard)/dashboard/page.jsx +40 -0
  7. package/templates/app/(dashboard)/layout.jsx +24 -0
  8. package/templates/app/(dashboard)/users/page.jsx +64 -0
  9. package/templates/app/globals.css +14 -0
  10. package/templates/app/layout.jsx +25 -0
  11. package/templates/app/login/page.jsx +69 -0
  12. package/templates/app/page.jsx +10 -0
  13. package/templates/components/auth/withAuth.jsx +50 -0
  14. package/templates/components/auth/withPublic.jsx +50 -0
  15. package/templates/components/layout/Header.jsx +29 -0
  16. package/templates/components/layout/Sidebar.jsx +35 -0
  17. package/templates/components/ui/Button.jsx +36 -0
  18. package/templates/components/ui/ErrorDisplay.jsx +19 -0
  19. package/templates/components/ui/Input.jsx +30 -0
  20. package/templates/components/ui/Loading.jsx +16 -0
  21. package/templates/components/ui/Modal.jsx +33 -0
  22. package/templates/components/ui/index.js +10 -0
  23. package/templates/contexts/AuthContext.jsx +112 -0
  24. package/templates/docs/README.md +128 -0
  25. package/templates/hooks/useAsync.js +38 -0
  26. package/templates/hooks/useAuth.js +93 -0
  27. package/templates/hooks/useForm.js +67 -0
  28. package/templates/jsconfig.json +27 -0
  29. package/templates/lib/api/apiClient.js +105 -0
  30. package/templates/lib/api/auth.api.js +36 -0
  31. package/templates/lib/api/user.api.js +43 -0
  32. package/templates/next.config.js +18 -0
  33. package/templates/package.json +23 -0
  34. package/templates/store/ReduxProvider.jsx +34 -0
  35. package/templates/store/api/apiSlice.js +108 -0
  36. package/templates/store/api/authApi.js +155 -0
  37. package/templates/store/api/exampleApi.js +108 -0
  38. package/templates/store/api/userApi.js +114 -0
  39. package/templates/store/slices/authSlice.js +86 -0
  40. package/templates/store/store.js +27 -0
@@ -0,0 +1,86 @@
1
+ /**
2
+ * Auth Slice
3
+ * Responsibility: Manage authentication state (user, tokens)
4
+ *
5
+ * USAGE GUIDE:
6
+ * - Import: import { selectCurrentUser, selectToken, setCredentials, logout } from '@/store/slices/authSlice'
7
+ * - Use in components: const user = useSelector(selectCurrentUser)
8
+ * - Dispatch actions: dispatch(setCredentials({ user, accessToken, refreshToken }))
9
+ */
10
+
11
+ import { createSlice } from "@reduxjs/toolkit";
12
+
13
+ // Initial state with token from localStorage (client-side only)
14
+ const getInitialState = () => {
15
+ if (typeof window !== "undefined") {
16
+ const accessToken = localStorage.getItem("accessToken");
17
+ const refreshToken = localStorage.getItem("refreshToken");
18
+ const user = localStorage.getItem("user");
19
+
20
+ return {
21
+ user: user ? JSON.parse(user) : null,
22
+ accessToken: accessToken || null,
23
+ refreshToken: refreshToken || null,
24
+ };
25
+ }
26
+
27
+ return {
28
+ user: null,
29
+ accessToken: null,
30
+ refreshToken: null,
31
+ };
32
+ };
33
+
34
+ const authSlice = createSlice({
35
+ name: "auth",
36
+ initialState: getInitialState(),
37
+ reducers: {
38
+ setCredentials: (state, action) => {
39
+ const { user, accessToken, refreshToken } = action.payload;
40
+
41
+ state.user = user;
42
+ state.accessToken = accessToken;
43
+
44
+ if (refreshToken) {
45
+ state.refreshToken = refreshToken;
46
+ }
47
+
48
+ // Persist to localStorage
49
+ if (typeof window !== "undefined") {
50
+ if (accessToken) localStorage.setItem("accessToken", accessToken);
51
+ if (refreshToken) localStorage.setItem("refreshToken", refreshToken);
52
+ if (user) localStorage.setItem("user", JSON.stringify(user));
53
+ }
54
+ },
55
+ updateAccessToken: (state, action) => {
56
+ state.accessToken = action.payload;
57
+
58
+ // Persist to localStorage
59
+ if (typeof window !== "undefined") {
60
+ localStorage.setItem("accessToken", action.payload);
61
+ }
62
+ },
63
+ logout: (state) => {
64
+ state.user = null;
65
+ state.accessToken = null;
66
+ state.refreshToken = null;
67
+
68
+ // Clear localStorage
69
+ if (typeof window !== "undefined") {
70
+ localStorage.removeItem("accessToken");
71
+ localStorage.removeItem("refreshToken");
72
+ localStorage.removeItem("user");
73
+ }
74
+ },
75
+ },
76
+ });
77
+
78
+ export const { setCredentials, updateAccessToken, logout } = authSlice.actions;
79
+
80
+ export default authSlice.reducer;
81
+
82
+ // Selectors
83
+ export const selectCurrentUser = (state) => state.auth.user;
84
+ export const selectToken = (state) => state.auth.accessToken;
85
+ export const selectRefreshToken = (state) => state.auth.refreshToken;
86
+ export const selectIsAuthenticated = (state) => !!state.auth.accessToken;
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Redux Store Configuration
3
+ * Responsibility: Configure Redux store with middleware and reducers
4
+ *
5
+ * USAGE GUIDE:
6
+ * - This is the main Redux store configuration
7
+ * - All slices and API services are automatically configured
8
+ * - The store is wrapped in the root layout
9
+ */
10
+
11
+ import { configureStore } from "@reduxjs/toolkit";
12
+ import authReducer from "./slices/authSlice";
13
+ import { apiSlice } from "./api/apiSlice";
14
+
15
+ export const store = configureStore({
16
+ reducer: {
17
+ auth: authReducer,
18
+ // Add the RTK Query API reducer
19
+ [apiSlice.reducerPath]: apiSlice.reducer,
20
+ },
21
+ // Adding the api middleware enables caching, invalidation, polling, and other features of RTK Query
22
+ middleware: (getDefaultMiddleware) =>
23
+ getDefaultMiddleware().concat(apiSlice.middleware),
24
+ devTools: process.env.NODE_ENV !== "production",
25
+ });
26
+
27
+ export default store;