# Authentication

Berry offers four authentication methods - JSON Web Token (JWT), Firebase, Auth0 and AWS - for users to choose from and can be changed to match the user's needs.

{% hint style="info" %}
JWT Authentication is set by default
{% endhint %}

{% embed url="<https://youtu.be/daHRKlIi6Uc?list=PLknn3jaIuWiDKKEy3EO-p5-MP1nSOgUr1>" %}
Authentication
{% endembed %}

## How does it work?

Access to dashboard pages is restricted to authenticated users. If a user is not authenticated, they will be redirected to the login page.

We used two guards **`GuestGuard`** and **`AuthGuard.`** Guards have been configured in **`src\utils\route-guard\`** folder.

In the **`src/layout/App.js`**, we have specified auth provider **`JWTProvider`** like,

{% code title="App.js" %}

```javascript
import { JWTProvider as AuthProvider } from 'contexts/JWTContext';
```

{% endcode %}

App component wrap with the **`<JWTProvider>`**

```javascript
<ThemeCustomization>
  ...
  <AuthProvider>
    <Routes />
    <Snackbar />
  </AuthProvider>
  ...
</<ThemeCustomization>>
```

Using **`<JWTProvider>`**, we can use the context directly by importing **`useContext`** from React and specifying the context **`JWTContext`** or we can use the custom hook **`useAuth`** from `src/hooks/useAuth.js`

## For Remix

We used two guards **`GuestGuard`** and **`AuthGuard.`** Guards have been configured in **`app\utils\route-guard\`** folder.

In the **`app/root.tsx`**, we have specified auth provider **`JWTProvider`** like,

{% code title="root.tsx" %}

```typescript
import { JWTProvider as AuthProvider } from 'contexts/JWTContext';
```

{% endcode %}

App component wrap with the **`<JWTProvider>`**

```typescript
<ThemeCustomization>
  ...
  <AuthProvider>
    <Outlet />
    <Snackbar />
  </AuthProvider>
  ...
</ThemeCustomization>
```

## Auth Configuration:

All configurations related to authentication are stored in .env file. Those configs are like APIKey to connect authentication server, project id, etc.

{% hint style="danger" %}
Berry has a dummy/test credentials to make authentication work. Users have to change API and secret as per their project needs. One should not use those provided keys in their live environment.
{% endhint %}

{% code title=".env" %}

```javascript
## Firebase - Google Auth 

REACT_APP_FIREBASE_API_KEY=
REACT_APP_FIREBASE_AUTH_DOMAIN=
REACT_APP_FIREBASE_PROJECT_ID=
REACT_APP_FIREBASE_STORAGE_BUCKET=
REACT_APP_FIREBASE_MESSAGING_SENDER_ID=
REACT_APP_FIREBASE_APP_ID=
REACT_APP_FIREBASE_MEASUREMENT_ID=

## AWS

REACT_APP_AWS_POOL_ID=
REACT_APP_AWS_APP_CLIENT_ID=

## Auth0

REACT_APP_AUTH0_CLIENT_ID=
REACT_APP_AUTH0_DOMAIN=

```

{% endcode %}

> The theme provides working an example for Login and Register only. Other flow like reset password, verification have to make it workable by the user himself.

## Switching between Authentication methods

### JWT to **Firebase**

**Set Firebase Config**

At present, firebase uses you need to set a secret in the following file. For more detail refer to firebase here: <https://firebase.google.com/docs/reference/rest/auth>

{% code title=".env" %}

```javascript
###
## Firebase - Google Auth 

REACT_APP_FIREBASE_API_KEY=
REACT_APP_FIREBASE_AUTH_DOMAIN=
REACT_APP_FIREBASE_PROJECT_ID=
REACT_APP_FIREBASE_STORAGE_BUCKET=
REACT_APP_FIREBASE_MESSAGING_SENDER_ID=
REACT_APP_FIREBASE_APP_ID=
REACT_APP_FIREBASE_MEASUREMENT_ID=
###
```

{% endcode %}

**Change AuthProvider**

{% code title="..\src\App.js" %}

```javascript
// Replace at line 6:
import { FirebaseProvider as AuthProvider } from 'contexts/FirebaseContext';
```

{% endcode %}

**Change auth Hooks**

Comment another context in the following file and uncomment Firebase one.

{% code title="..\src\hooks\useAuth.js" %}

```javascript
import AuthContext from 'contexts/FirebaseContext';
```

{% endcode %}

#### Copy login code

It's also super simple. We have provided a code that just needs to be replaced. Copy `src\views\pages\authentication\login\FirebaseLogin` to `src\views\pages\authentication\auth-forms\AuthLogin.tsx`

**For nextJS,** `src\components\Authentication\login\FirebaseLogin` to `src\components\Authentication\auth-forms\AuthLogin.tsx`

#### Copy register code

We have provided a code that just needs to be replaced. Copy `src\views\pages\authentication\login\FirebaseRegister` to `src\views\pages\authentication\auth-forms\AuthRegister.tsx`

**For nextJS,** `src\components\Authentication\login\JWTRegister` to `src\components\Authentication\auth-forms\AuthRegister.tsx`

### **JWT to Auth0**

**Set Auth0 Config**

At present, Auth0 uses a dummy client id and domain, so we don't need to change anything, but in actual implementation, you need to set client id and domain in the following file. For more detail refer to Auth0 here: <https://auth0.com/docs/get-started/auth0-overview>

{% code title=".env" %}

```javascript
###
## Auth0

REACT_APP_AUTH0_CLIENT_ID=
REACT_APP_AUTH0_DOMAIN=
###
```

{% endcode %}

**Change AuthProvider**

{% code title="..\src\App.js" %}

```javascript
import { Auth0Provider as AuthProvider } from 'contexts/Auth0Context';
```

{% endcode %}

**Change auth Hooks**

Comment another context in the following file and uncomment Auth0 one.

{% code title="..\src\hooks\useAuth.js" %}

```javascript
import AuthContext from 'contexts/Auth0Context';
```

{% endcode %}

#### Copy login code

It's super simple. We have provided a code that just needs to be replaced.&#x20;

Copy code from `src\views\pages\authentication\login\Auth0Login` to `src\views\pages\authentication\auth-forms\AuthLogin.tsx`

**For nextJS,** `src\components\Authentication\login\Auth0Login` to `src\components\Authentication\auth-forms\AuthLogin.tsx`

#### Copy register code

We have provided a code that just needs to be replaced. Copy code from `src\views\pages\authentication\login\Auth0Register` to `src\views\pages\authentication\auth-forms\AuthRegister.tsx`

**For nextJS,** `src\components\Authentication\login\Auth0Register` to `src\components\Authentication\auth-forms\AuthRegister.tsx`

### **JWT to AWS Congnito**

**Set AWS Config**

At present, AWS uses a dummy config, so we don't need to change anything, but in actual implementation, you need to set poolId and appClientId in the following file. For more detail refer to AWS here: <https://aws.amazon.com/cognito/>

{% code title="." %}

```javascript
###
## AWS

REACT_APP_AWS_POOL_ID=
REACT_APP_AWS_APP_CLIENT_ID=env
###
```

{% endcode %}

**Change AuthProvider**

{% code title="..\src\App.js" %}

```javascript
import { AWSCognitoProvider as AuthProvider } from 'contexts/AWSCognitoContext';
```

{% endcode %}

**Change auth Hooks**

Comment another context in the following file and uncomment Auth0 one.

{% code title="..\src\hooks\useAuth.js" %}

```javascript
import AuthContext from 'contexts/AWSCognitoContext';
```

{% endcode %}

#### Copy login code

It's super simple. We have provided a code that just needs to be replaced. Copy code from `src\views\pages\authentication\login\AWSCognitoLogin` to `src\views\pages\authentication\auth-forms\AuthLogin.tsx`

**For nextJS,** `src\components\Authentication\login\AWSCongnitoLogin` to `src\components\Authentication\auth-forms\AuthLogin.tsx`

#### Copy register code

We have provided a code that just needs to be replaced. Copy code from `src\views\pages\authentication\login\AWSCognitoRegister` to `src\views\pages\authentication\auth-forms\AuthRegister.tsx`

**For nextJS,** `src\components\Authentication\login\AWSCognitoRegister` to `src\components\Authentication\auth-forms\AuthRegister.tsx`
