0.57.0 • Published 8 months ago

@dzangolab/react-user v0.57.0

Weekly downloads
-
License
-
Repository
-
Last release
8 months ago

@dzangolab/react-user

A React library that provides an easy integration of authentication and user management functionality with SuperTokens.

Requirements

  • @dzangolab/react-form
  • @dzangolab/react-i18n
  • @dzangolab/react-layout
  • @dzangolab/react-ui
  • axios
  • primeicons
  • react-toastify

Installation

Install with npm:

npm install @dzangolab/react-user

Install with pnpm:

pnpm install @dzangolab/react-user

Usage

  1. Wrap your application code with the UserWrapper component
// src/main.tsx
import { UserWrapper } from "@dzangolab/react-user";

import config, { userConfig } from "./config";

ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
  // ...
    <UserWrapper config={userConfig}>
      <App />
    </UserWrapper>
  // ...
);
  1. Use user routes and route handlers from the package in your app's router
// src/Router.tsx
import {
  getUserProtectedRoutes,
  getUserPublicRoutes,
  ProtectedRoutesHandler,
  PublicRoutesHandler,
} from "@dzangolab/react-user";
import { BrowserRouter as Router, Route, Routes } from "react-router-dom";

export const AppRouter = () => {
  return (
    <>
      <Router>
        <Routes>
          <Route element={<BasicLayout />}>
            {/* auth routes */}
            <Route element={<ProtectedRoutesHandler />}> // route handler to perform redirections if user is not authenticated
              {/* protected routes from the package including users, invitations, email verification, etc */}
              {getUserProtectedRoutes()}
            </Route>

            {/* unauth routes */}
            <Route element={<PublicRoutesHandler />}> // route handler to perform redirections if user is authenticated
              {/* public routes from the package including login, signup, forgot password, etc */}
              {getUserPublicRoutes()}
            </Route>

            {/* public routes */}
            // ...
          </Route>
        </Routes>
      </Router>
    </>
  );
};

// src/App.tsx
import { ToastContainer } from "react-toastify";

import { AppRouter } from "./Routers";

export const App = () => {
  return (
    <>
      <AppRouter />
      <ToastContainer position="bottom-right" />
    </>
  );
};

Configuration

All configurations can be done with the config prop to UserWrapper component.

Minimum required configuration

{
  apiBaseUrl: string;
  appDomain: string;
  supertokens: {
    appName: string;
    apiDomain: string;
  };
  supportedRoles: string[]; // array of role strings supported by the app eg ["ADMIN", "SUPERADMIN"]
}

Using custom paths for provided pages

By default, following paths are used

  • acceptInvitation: '/signup/token/:token'
  • authCallbackGoogle: '/auth/callback/google'
  • changePassword: '/change-password'
  • emailVerificationReminder: '/email-verification-reminder'
  • emailVerificationVerify: '/verify-email'
  • forgotPassword: '/forgot-password'
  • login: '/login'
  • profile: '/profile'
  • resetPassword: '/reset-password'
  • signup: '/signup'
  • signupFirstUser: '/signup-first-user'

To customize any path, provide custom value for it in the customPaths config.

{
  // ...
  customPaths: {
    login: '/signin'
    signup: '/register'
  }
}

Using custom home route, default '/'

{
  // ...
  homeRoute: "/dashboard"
}
{
  // ...
  homeRoute: (user: UserType) => "/custom-path"
}

Customizing features

{ // ...
  features?: {
    forgotPassword?: boolean; // default `true`
    signup?: // default true
      | false
      | {
          emailVerification?: boolean; // default `false`
        };
    signupFirstUser?: boolean; // default `false`, requires `signup` to be `false`
    termsAndConditions?: {
      display: boolean;
      label: ReactNode;
      showCheckbox?: boolean;
    };
  };
}

Enabling social login providers

{
  // ...
  socialLoginProviders?: SocialLoginType[]; // currently only 'google' supported
}

Optional SuperTokens configuration

{
  // ...
  supertokens: {
    apiBasePath?: string; // default `/auth`
    sessionConfig?: Session.UserInput;
    thirdPartyEmailPasswordConfig?: ThirdPartyEmailPassword.UserInput;
  };
}

Using custom pages

Custom page components can be used for predefined routes in package by passing routes option to getUserProtectedRoutes and getUerPublicRoutes

// /src/Router.tsx

...
 {getUserProtectedRoutes({
    routes: {
      emailVerificationReminder: {
        element: <CustomEmailVerificationReminderPage />, // custom element for email verification reminder route
      },
    },
  })}

...
  {getUserPublicRoutes({
    routes: {
      login: {
        element: <CustomLoginPage />, // custom element for email verification reminder route
      },
    },
  })}

Supported route options by getUserProtectedRoutes

  • changePassword
  • emailVerificationReminder
  • emailVerificationVerify
  • profile

Supported route options by getUserPublicRoutes

  • acceptInvitation
  • authCallbackGoogle
  • forgotPassword
  • login
  • resetPassword
  • signup
  • signupFirstUser
0.55.0

9 months ago

0.51.0

11 months ago

0.51.1

11 months ago

0.56.1

8 months ago

0.56.2

8 months ago

0.56.0

8 months ago

0.52.0

11 months ago

0.49.0

12 months ago

0.57.0

8 months ago

0.53.0

11 months ago

0.54.0

9 months ago

0.50.1

12 months ago

0.50.0

12 months ago

0.48.3

1 year ago

0.43.0

1 year ago

0.41.0

1 year ago

0.48.2

1 year ago

0.48.0

1 year ago

0.48.1

1 year ago

0.46.0

1 year ago

0.44.0

1 year ago

0.42.0

1 year ago

0.45.1

1 year ago

0.47.0

1 year ago

0.45.0

1 year ago

0.40.0

1 year ago

0.39.0

1 year ago

0.38.0

1 year ago

0.37.0

2 years ago

0.36.1

2 years ago

0.36.0

2 years ago

0.35.1

2 years ago

0.35.0

2 years ago

0.34.0

2 years ago

0.33.0

2 years ago

0.32.0

2 years ago

0.31.0

2 years ago

0.30.0

2 years ago

0.29.0

2 years ago

0.28.0

2 years ago

0.27.0

2 years ago

0.26.0

2 years ago

0.25.0

2 years ago

0.8.6

2 years ago

0.20.0

2 years ago

0.19.0

2 years ago

0.11.0

2 years ago

0.13.0

2 years ago

0.13.1

2 years ago

0.15.0

2 years ago

0.13.2

2 years ago

0.17.0

2 years ago

0.23.0

2 years ago

0.9.0

2 years ago

0.21.0

2 years ago

0.12.0

2 years ago

0.14.0

2 years ago

0.16.0

2 years ago

0.18.0

2 years ago

0.10.0

2 years ago

0.24.0

2 years ago

0.22.1

2 years ago

0.22.0

2 years ago

0.8.5

2 years ago

0.8.4

2 years ago

0.7.6

2 years ago

0.4.9

3 years ago

0.7.5

3 years ago

0.4.8

3 years ago

0.8.1

2 years ago

0.7.2

3 years ago

0.5.4

3 years ago

0.8.0

2 years ago

0.7.1

3 years ago

0.5.3

3 years ago

0.8.3

2 years ago

0.7.4

3 years ago

0.8.2

2 years ago

0.7.3

3 years ago

0.5.5

3 years ago

0.5.0

3 years ago

0.7.0

3 years ago

0.6.1

3 years ago

0.5.2

3 years ago

0.6.0

3 years ago

0.5.1

3 years ago

0.4.7

3 years ago

0.4.6

3 years ago

0.4.5

3 years ago

0.4.4

3 years ago

0.4.3

3 years ago

0.4.2

3 years ago

0.4.1

3 years ago

0.4.0

3 years ago

0.3.2

3 years ago