# Auth flow

[![N|Solid](https://svgshare.com/i/nwR.svg)](nbcomputers.ru)

Work auth flow context that enables any component to get the current auth state
user type and rerender if it changes

- React/TypeScript
- Redux
- NextJS
- ✨Magic ✨

## Features

- UserProvider includes a user type and a setter function to change it
- useJWTAuthentication checks for a VERIFY cookie and sets the authorization flag isAuthorized=true. If the user is
  logged in, it sends a request to refresh the token
- setAuthCookie Callback for set expire auth cookies
- Logout hook

## Installation

@nbcom/auth requires [Node.js](https://nodejs.org/) v16+, [@nbcom/api](https://www.npmjs.com/package/@nbcom/auth) to
run.

Install the package

```sh
npm install @nbcom/auth
```

## Development

### Using user provider

```sh
import { UserProvider } from '@nbcom/auth'

  <UserProvider>
    <Template>
      <Component {...pageProps} />
    </Template>
  </UserProvider>
```

### Using logout hook

You must create proxy file pages/api/removeAuthCookiesProxy.ts with your domain like 'someurl.ru'

```sh
import { removeAuthCookies } from "@nbcom/auth";

export default removeAuthCookies('domain');
```

Then you can use the hook logOut

```sh
import { useLogOut } from '@nbcom/auth'

const { logOut } = useLogOut();
<Button onClick={logOut}> Exit from acc</Button>
```

### Boolean flag && set type user

```sh
import { useUser } from '@nbcom/auth'
const { isJuridicalUser, changeUserType, isAuthorized } = useUser();
```

## Good luck