@shark-pepper/create-app 1.0.3 → 1.0.4
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.
- package/README.md +31 -2
- package/package.json +1 -1
- package/templates/react-app/package.base.json +13 -5
- package/templates/react-app/pnpm-lock.yaml +730 -252
- package/templates/react-app/src/App.tsx +3 -1
- package/templates/react-app/src/components/ProtectedRoute.tsx +17 -0
- package/templates/react-app/src/index.tsx +15 -4
- package/templates/react-app/src/pages/Home.tsx +15 -0
- package/templates/react-app/src/pages/Login.tsx +29 -0
- package/templates/react-app/src/routes/index.tsx +24 -0
- package/templates/react-app/src/styles/Home.scss +9 -0
- package/templates/react-app/src/styles/Login.scss +16 -0
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { ReactNode } from 'react';
|
|
2
|
+
import { Navigate, useLocation } from 'react-router-dom';
|
|
3
|
+
|
|
4
|
+
interface ProtectedRouteProps {
|
|
5
|
+
children: ReactNode;
|
|
6
|
+
redirectPath?: string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export default function ProtectedRoute({ children, redirectPath = '/login' }: ProtectedRouteProps) {
|
|
10
|
+
const location = useLocation();
|
|
11
|
+
const isAuth = localStorage.getItem('isAuth') === 'true';
|
|
12
|
+
|
|
13
|
+
if (!isAuth) {
|
|
14
|
+
return <Navigate to={redirectPath} state={{ from: location }} replace />;
|
|
15
|
+
}
|
|
16
|
+
return children;
|
|
17
|
+
}
|
|
@@ -1,6 +1,17 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { createRoot } from 'react-dom/client';
|
|
3
|
+
import { BrowserRouter, useRoutes } from 'react-router-dom';
|
|
4
|
+
import routes from './routes';
|
|
5
|
+
import 'normalize.css';
|
|
3
6
|
|
|
4
|
-
const
|
|
7
|
+
const AppRouter: React.FC = () => useRoutes(routes);
|
|
8
|
+
|
|
9
|
+
const container = document.getElementById('root')!;
|
|
5
10
|
const root = createRoot(container);
|
|
6
|
-
root.render(
|
|
11
|
+
root.render(
|
|
12
|
+
<React.StrictMode>
|
|
13
|
+
<BrowserRouter>
|
|
14
|
+
<AppRouter />
|
|
15
|
+
</BrowserRouter>
|
|
16
|
+
</React.StrictMode>,
|
|
17
|
+
);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import '../styles/Home.scss';
|
|
2
|
+
|
|
3
|
+
export default function Home() {
|
|
4
|
+
const handleClick = () => {
|
|
5
|
+
localStorage.setItem('isAuth', 'false');
|
|
6
|
+
window.location.reload();
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
return (
|
|
10
|
+
<div className="home">
|
|
11
|
+
<h1>Home Page</h1>
|
|
12
|
+
<button onClick={handleClick}>Logout</button>
|
|
13
|
+
</div>
|
|
14
|
+
);
|
|
15
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Navigate, useLocation, useNavigate } from 'react-router-dom';
|
|
2
|
+
import '../styles/Login.scss';
|
|
3
|
+
|
|
4
|
+
export default function Login() {
|
|
5
|
+
const navigate = useNavigate();
|
|
6
|
+
const location = useLocation();
|
|
7
|
+
|
|
8
|
+
const from = (location.state as any)?.from?.pathname || '/';
|
|
9
|
+
const isAuth = localStorage.getItem('isAuth') === 'true';
|
|
10
|
+
|
|
11
|
+
if (isAuth) {
|
|
12
|
+
return <Navigate to="/" replace />;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const handleLogin = () => {
|
|
16
|
+
localStorage.setItem('isAuth', 'true');
|
|
17
|
+
navigate(from, { replace: true });
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
return (
|
|
21
|
+
<div className="container">
|
|
22
|
+
<div className="welcome">
|
|
23
|
+
<h1>Thanks for using @shark-pepper/create-app</h1>
|
|
24
|
+
<h2>Let's start coding!</h2>
|
|
25
|
+
</div>
|
|
26
|
+
<button onClick={handleLogin}>Login Now</button>
|
|
27
|
+
</div>
|
|
28
|
+
);
|
|
29
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Navigate, RouteObject } from 'react-router-dom';
|
|
2
|
+
import ProtectedRoute from '@/components/ProtectedRoute';
|
|
3
|
+
import Home from '@/pages/Home';
|
|
4
|
+
import Login from '@/pages/Login';
|
|
5
|
+
import App from '../App';
|
|
6
|
+
|
|
7
|
+
const routes: RouteObject[] = [
|
|
8
|
+
{
|
|
9
|
+
path: '/',
|
|
10
|
+
element: (
|
|
11
|
+
<ProtectedRoute>
|
|
12
|
+
<App />
|
|
13
|
+
</ProtectedRoute>
|
|
14
|
+
),
|
|
15
|
+
children: [
|
|
16
|
+
{ index: true, element: <Home /> },
|
|
17
|
+
{ path: 'home', element: <Navigate to="/" replace /> },
|
|
18
|
+
{ path: '*', element: <div>404 Not Found</div> },
|
|
19
|
+
],
|
|
20
|
+
},
|
|
21
|
+
{ path: 'login', element: <Login /> },
|
|
22
|
+
];
|
|
23
|
+
|
|
24
|
+
export default routes;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
@mixin flex-center($direction: row) {
|
|
2
|
+
display: flex;
|
|
3
|
+
flex-direction: $direction;
|
|
4
|
+
justify-content: center;
|
|
5
|
+
align-items: center;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
.container {
|
|
9
|
+
height: 100vh;
|
|
10
|
+
@include flex-center(column);
|
|
11
|
+
.welcome {
|
|
12
|
+
width: 750px;
|
|
13
|
+
margin-bottom: 40px;
|
|
14
|
+
@include flex-center(column);
|
|
15
|
+
}
|
|
16
|
+
}
|