Authentication

Materially supports 3 popular Authentication methods.

Materially supports 3 authentication methods: Firebase Authentication, JSON Web Token, Auth0.

We provided JWT Authentication by default.

How does it work?

Only authenticated users can access dashboard pages. If a user is not authenticated, the user redirected to the login page.

We implemented to make this work the “Guard” concept. It is simply a component that wraps a route and checks for user authentication status before allowing the navigation.

We used two guards GuestGuard and AuthGuard, to find more information about guards, please visit the Routes.js page.

In the src/layout/App.jsx, we have specified auth provider JWT like,

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

App components wrap with the provider, like

<ThemeProvider theme={theme(customization)}>
  <AuthProvider>  
    <ThemeRoutes />
    <Snackbar />
  </FirebaseProvider>
</AuthProvider>

Using JWTProvider , we can use the context directly by importing useContext from React and specifying the context JWTContext that we want to use, or we can use the custom hook useAuth from src/hooks/useAuth.js

Auth Configuration:

You can edit this file at [ ../.env]

VITE_APP_VERSION = Version of app
GENERATE_SOURCEMAP = false

## Firebase - Google Auth 

VITE_APP_FIREBASE_API_KEY= Firebase Api key
VITE_APP_FIREBASE_AUTH_DOMAIN=  Firebase Auth domain
VITE_APP_FIREBASE_PROJECT_ID= Firebase Project Id
VITE_APP_FIREBASE_STORAGE_BUCKET= Firebase Storage Bucket
VITE_APP_FIREBASE_MESSAGING_SENDER_ID= Firebase Messaging Sender Id
VITE_APP_FIREBASE_APP_ID= Firebase App Id
VITE_APP_FIREBASE_MEASUREMENT_ID= Firebase Measurement Id

## Auth0

VITE_APP_AUTH0_CLIENT_ID= Auth0 Client Id
VITE_APP_AUTH0_DOMAIN= Auth0 Domain

## JWT

VITE_APP_JWT_SECRET_KEY= Secret key
VITE_APP_JWT_TIMEOUT= Timeout

Switching between Authentication methods

JWT to Firebase

Set Firebase Config - Open file '.env' at directory '.env' and set firebase configuration.

## Firebase

VITE_APP_FIREBASE_API_KEY= Firebase Api key
VITE_APP_FIREBASE_AUTH_DOMAIN=  Firebase Auth domain
VITE_APP_FIREBASE_PROJECT_ID= Firebase Project Id
VITE_APP_FIREBASE_STORAGE_BUCKET= Firebase Storage Bucket
VITE_APP_FIREBASE_MESSAGING_SENDER_ID= Firebase Messaging Sender Id
VITE_APP_FIREBASE_APP_ID= Firebase App Id
VITE_APP_FIREBASE_MEASUREMENT_ID= Firebase Measurement Id

Change Login Form - Open file 'index.jsx' at directory 'src/views/Login/index.jsx', and use the FirebaseLogin component.

// Replace at line 11:
import AuthLogin from './FirebaseLogin';
 
<AuthLogin />

AuthProvider for Layout - Open file 'App.jsx' at directory 'src/layout/App.jsx' and use AuthProvider

// Replace at line 22;
import { FirebaseProvider as AuthProvider } from 'contexts/FirebaseContext';

<AuthProvider>
    <Routes />
    <Snackbar />
</AuthProvider>

Change auth Hooks - Open file 'useAuth.js' at directory 'src/hooks/useAuth.js' and use JWTContext

// Replace from line 5:
import AuthContext from '../contexts/FirebaseContext';
const useAuth = () => useContext(AuthContext);

Firebase to Auth0

Set Auth Config - Open file '.env' at directory '.env' and set auth0 configuration.

## Auth0

VITE_APP_AUTH0_CLIENT_ID= Auth0 Client Id
VITE_APP_AUTH0_DOMAIN= Auth0 Domain

Change Login Form - Open file 'index.jsx' at directory 'src/views/Login/index.jsx', and use Auth0Login component.

// Replace at line 8:
import AuthLogin from './Auth0Login';

// Replace at line 21: 
<AuthLogin />

AuthProvider for Layout - Open file 'App.jsx' at directory 'src/layout/App.jsx' and use Auth0Provider

// Replace at line 23;
import { Auth0Provider as AuthProvider } from 'contexts/Auth0Context';

<AuthProvider>
    {renderRoutes(routes)}
</AuthProvider>

Change auth Hooks - Open file 'useAuth.js' at directory '../src/hooks/useAuth.js' and use Auth0Context

// Replace from line 2:
import AuthContext from '../contexts/Auth0Context';
const useAuth = () => useContext(AuthContext);

Last updated