Skip to content

Commit

Permalink
feature/vitest (#13)
Browse files Browse the repository at this point in the history
* test(`routes.spec.ts`): different `routes` configurations

* chore: changeset & fix docs link

* test: routes.spec.tsx

* docs: readme.md

* docs: typedocs

* chore: fix main.yml

* chore: fix main.yml

* chore: fix main.yml

* docs: add gh pages link

---------

Co-authored-by: Pasecinic Nichita <nichita.pasecinic@jivygroup.com>
  • Loading branch information
nichitaa and nichita-pasecinic authored Jan 14, 2024
1 parent 060d2ff commit 7369760
Show file tree
Hide file tree
Showing 46 changed files with 9,292 additions and 6,499 deletions.
5 changes: 5 additions & 0 deletions .changeset/bright-peaches-speak.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nichitaa/auth-react-router": patch
---

vitest configuration and unit tests
12 changes: 11 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,14 @@ jobs:
node-version: '20.x'
registry-url: 'https://registry.npmjs.org'
- run: npm i
- run: npm run lint && npm run build && npm run publint
- run: npm run lint
- run: npm run test
- run: npm run build
- run: npm run publint
- run: npm run typedoc

- name: Deploy Docs
uses: JamesIves/github-pages-deploy-action@4.1.4
with:
branch: gh-pages
folder: docs
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
node_modules
dist
.idea
.idea
coverage
docs
demo-app
159 changes: 159 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
**`@nichitaa/auth-react-router`** is a lightweight package that uses and extends basic `react-router-dom` v6
functionality and allows you to define the routes based on user `authorized` and `roles` states. It provides a simple
API for configuring `public`, `private` and `common` routes, and it has a simple and advance RBAC configuration.

[Documentation (`typedoc`)](https://nichitaa.github.io/auth-react-router/)

This code and routing pattern is used on most of my personal projects and would probably meet most of the routing
requirement for any other react application, but beware and use it at your own risk.

### Getting Started 🎉
#### Peer Dependencies 🔨

* `react`: `>=16`,
* `react-dom`: `>=16`,
* `react-router-dom`: `^6.x`

#### Installation ✨
```shell
npm install @nichitaa/auth-react-router
```

#### Example

Define your application routes *(easier to maintain if are in separate file)

```JSX
// src/routes.tsx
import { RoutesConfig } from '@nichitaa/auth-react-router';
import { Outlet } from 'react-router-dom';
import { lazy } from 'react';

/**
* using normal `imports` or `lazy`
*/
import PageA from '../pages/LoginPage.tsx';

const LazyPageB = lazy(() => import('../pages/PageB'));
const LazyPageC = lazy(() => import('../pages/PageC'));
const LazyPageD = lazy(() => import('../pages/PageD'));

/**
* optionally define roles (if you want to use RBAC functionality)
*/
export const roles = {
ADMIN: 'ADMIN',
OPERATION: 'OPERATION',
MANAGER: 'MANAGER',
REGULAR: 'REGULAR',
} as const;


/** routes definition */
export const routes: RoutesConfig = {
/**
* globally configured fallbacks (can be configured at route level too)
*/
fallback: {
/**
* if user is on private route and has `authorized: false`, then he probably should be redirectied to a public route
* and vice-versa, if those route fallbacks are not provided, default one is first common route (common[0].path)
*/
privateRoute: '/b',
publicRoute: '/c',
InvalidRoles: route => <GlobalInvalidRoute {...route} />,
Suspense: <>...loading page...</>,
},
/**
* common routes (accessible by all users)
*/
common: [
{
path: '/',
element: <>Route available for all users</>,
},
{
path: '*',
element: <p>PAGE NOT FOUND 404</p>,
},
],
/**
* public paths (accessible only by users with `authorized: false`)
*/
public: [
{
path: '/b',
// with <Outlet/>
element: <>
<CustomLayout>
<Outlet />
</CustomLayout>
</>,
// nested routes
routes: [
{
index: true,
element: <LazyPageB />,
},
{
path: ':id',
element: <LazyPageC />,
},
],
},
],
/**
* private paths (accessible only by users with `authorized: true`)
*/
private: [
{
path: '/c',
element: <>Private page protected by all roles</>,
roles: [roles.ADMIN, roles.MANAGER],
allRolesRequired: true,
},
{
path: '/c',
element: <>Private page protected by at least one role</>,
roles: [roles.OPERATION, roles.MANAGER],
},
{
path: '/d',
element: <LazyPageD />,
roles: [roles.OPERATION, roles.MANAGER],
// Private route with its own fallbacks
fallback: {
Suspense: <>...loading page for private route /d...</>,
InvalidRoles: (route) => <>:( could not access route /d</>,
/**
* fallback route if unauthroized user tryies to access `/d` route,
* usually you'd want to redirect him to a public/common page (a page that he has access to)
*/
route: '/b',
},
},
],
};
```
Configure `AuthReactRouter` and render `RoutesRoot`
```JSX
import { AuthReactRouter, RoutesRoot } from '@nichitaa/auth-react-router';
import { BrowserRouter } from 'react-router-dom';
import { routes, roles } from './routes';

export const App = () => {
const { isAuthorized, userRoles } = useAuthProvider();
return (
<BrowserRouter>
<AppRouter authorized={isAuthorized} routes={routes} roles={userRoles}>
<RoutesRoot />
</AppRouter>
</BrowserRouter>
);
};
```
To add a new route just add it to `public`, `private` or `common` array and it will work.
18 changes: 0 additions & 18 deletions demo-app/.eslintrc.cjs

This file was deleted.

24 changes: 0 additions & 24 deletions demo-app/.gitignore

This file was deleted.

12 changes: 0 additions & 12 deletions demo-app/index.html

This file was deleted.

Loading

0 comments on commit 7369760

Please sign in to comment.