Authentication
Auth0, JWT, Firebase setup
Berry includes four Authentication methods Firebase, JSON Web Token (JWT), Auth0, AWS for their users. Users can change it as per their needs.
Firebase Authentication is set by default
Authentication

How does it work?

Only authenticated users can access dashboard pages. If a user is not authenticated, the user is 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 FirebaseProvider like,
App.js
1
import { FirebaseProvider as AuthProvider } from 'contexts/FirebaseContext';
Copied!
App component wrap with the <FirebaseProvider>
1
<ThemeCustomization>
2
...
3
<AuthProvider>
4
<Routes />
5
<Snackbar />
6
</AuthProvider>
7
...
8
</<ThemeCustomization>>
Copied!
Using <FirebaseProvider>, we can use the context directly by importing useContext from React and specifying the context FirebaseContext or we can use the custom hook useAuth from src/hooks/useAuth.js

Auth Configuration:

All configurations related to authentication are stored in config.js. Those configs are like APIKey to connect authentication server, project id, etc.
Berry has a dummy/test config 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.
config.js
1
// JWT JSON Web Token method
2
export const JWT_API = {
3
secret: 'SECRET-KEY',
4
timeout: '1 days'
5
};
6
7
// Firebase Authentication method
8
export const FIREBASE_API = {
9
apiKey: "API-KEY",
10
authDomain: "AUTH-DOMAIN",
11
databaseURL: "DATABASE-URL",
12
projectId: "PROJECT-ID",
13
storageBucket: "STORAGE-BUCKET",
14
messagingSenderId: "MESSAGEING-SENDER-ID",
15
appId: "APP-ID",
16
measurementId: "MEASUREMENT-ID"
17
};
18
19
// Auth0 method
20
export const AUTH0_API = {
21
client_id: 'CLIENT-ID',
22
domain: 'DOMAIN'
23
};
24
25
// AWS method
26
export const AWS_API = {
27
poolId: 'poolid',
28
appClientId: 'appid'
29
};
Copied!
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

Firebase 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
..\src\config.js
1
...
2
auth0: {
3
client_id: 'This is dummy id',
4
domain: 'this.is.dummy.domain'
5
}
6
...
Copied!
Change AuthProvider
..\src\App.js
1
import { Auth0Provider } from 'contexts/Auth0Context';
2
3
// Also find & edit below code block
4
<Auth0Provider>
5
<Routes />
6
<Snackbar />
7
</Auth0Provider>
Copied!
Change auth Hooks
Comment another context in the following file and uncomment Auth0 one.
..\src\hooks\useAuth.js
1
2
import AuthContext from 'contexts/Auth0Context';
Copied!

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\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

Firebase to JWT

Set JWT Config
At present, jwt uses a dummy backend call, so we don't need any secret, but in actual implementation, you need to set a secret in the following file. For more detail refer to JWT here: https://jwt.io/introduction
..\src\config.js
1
...
2
jwt: {
3
secret: 'SECRET-KEY',
4
timeout: '1 days'
5
}
6
...
Copied!
Change AuthProvider
..\src\App.js
1
// Replace at line 6:
2
import { JWTProvider } from "./contexts/JWTContext";
3
4
// Also find & edit below code block
5
<JWTProvider>
6
<Routes />
7
<Snackbar />
8
</JWTProvider>
Copied!
Change auth Hooks
Comment another context in the following file and uncomment JWT one.
..\src\hooks\useAuth.js
1
import AuthContext from 'contexts/JWTContext';
Copied!

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\JWTLogin to src\views\pages\authentication\auth-forms\AuthLogin.tsx
For nextJS, src\components\Authentication\login\JWTLogin 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\JWTRegister to src\views\pages\authentication\auth-forms\AuthRegister.tsx
For nextJS, src\components\Authentication\login\JWTRegister to src\components\Authentication\auth-forms\AuthRegister.tsx

Firebase 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/
..\src\config.js
1
...
2
3
export const AWS_API = {
4
poolId: 'poolid',
5
appClientId: 'appid'
6
};
7
8
...
Copied!
Change AuthProvider
..\src\App.js
1
import { AWSCognitoProvider as AuthProvider } from 'contexts/AWSCognitoContext';
2
3
// Also find & edit below code block
4
<AuthProvider>
5
<Routes />
6
<Snackbar />
7
</AuthProvider>
Copied!
Change auth Hooks
Comment another context in the following file and uncomment Auth0 one.
..\src\hooks\useAuth.js
1
2
import AuthContext from 'contexts/AWSCognitoContext';
Copied!

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