diff --git a/.env b/.env new file mode 100644 index 0000000..caad59b --- /dev/null +++ b/.env @@ -0,0 +1,9 @@ +# Allow the app to work in anonymous mode. +# +# This enables accessing app sections and API calls that +# otherwise would require login. +# +# You CAN'T use this as part of your task01 solution, but +# it might be useful if you want to work on task02 without +# fully implementing task01 first. +VITE_ALLOW_ANONYMOUS_ACCESS=false # true|false diff --git a/docs/task-02.md b/docs/task-02.md index 41c135e..feac150 100644 --- a/docs/task-02.md +++ b/docs/task-02.md @@ -9,6 +9,8 @@ While they would love for us to implement it; they told us it would suffice for We believe there is value in that and after some conversation we agreed on implementing a quick solution to validate if there is actual interest in this by our customers. +If you decided to start with this task before having [task 01](./task-01.md) fully working; you can disable the app's login requirement by setting `VITE_ALLOW_ANONYMOUS_ACCESS=true` in your `.env` file. + **Dos and Do not's** - You **must** implement something that fullfil the club's needs diff --git a/src/App.tsx b/src/App.tsx index 71fa4c4..f1d30bb 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -36,11 +36,20 @@ function AppRouter() { return
Loading authorization...
} + // Note: + // You can toggle this boolean for task02 to easily get past the authentication. + // This might prove useful if you want to start with task02 without completing task01; + // or if you are having issues with task01 and want to progress with something else. + // DO NOT USE this as part of your task01 + const allowAnonymousAccess = import.meta.env.VITE_ALLOW_ANONYMOUS_ACCESS === 'true' + + const canAccessProtectedRoutes = Boolean(auth.currentUser || allowAnonymousAccess) + return ( - {auth.currentUser ? ( + {canAccessProtectedRoutes ? ( ) : ( @@ -48,7 +57,7 @@ function AppRouter() { - {auth.currentUser ? ( + {canAccessProtectedRoutes ? ( { auth.logout().catch((error: unknown) => { @@ -61,7 +70,7 @@ function AppRouter() { )} - {auth.currentUser ? : } + {canAccessProtectedRoutes ? : } ) diff --git a/src/lib/msw/handlers.ts b/src/lib/msw/handlers.ts index 34a5b0f..a7ee13f 100644 --- a/src/lib/msw/handlers.ts +++ b/src/lib/msw/handlers.ts @@ -150,6 +150,10 @@ export const handlers: RequestHandler[] = [ }), http.all('/api/*', async ({ request }) => { + if (import.meta.env.VITE_ALLOW_ANONYMOUS_ACCESS === 'true') { + return + } + const auth = request.headers.get('authorization') if (!auth) { diff --git a/src/vite-env.d.ts b/src/vite-env.d.ts index 11f02fe..cbe5cc9 100644 --- a/src/vite-env.d.ts +++ b/src/vite-env.d.ts @@ -1 +1,13 @@ /// + +interface ImportMetaEnv { + /** + * Whether or not we allow accessing the app and api without credentials. + * + * You can set this variable via `.env` file to skip the login. + * + * This might be useful if you want to start with task02 without doing + * task01 first. + */ + readonly VITE_ALLOW_ANONYMOUS_ACCESS: string +}