npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-screen-auth-guard

v0.1.2

Published

Protect certain screens against unauthenticated (or authenticated) visits (e.g. member page, login, register, ...)

Downloads

130

Readme

react-native-auth-guard

A flexible and type-safe authentication guard for React Native apps using React Navigation and TypeScript.

Overview

react-native-auth-guard provides an easy way to enforce authentication requirements on screens within a React (Native) app. By wrapping components with a higher-order component (HOC), you can automatically redirect users based on their authentication status.

Features

  • Type-safe: Built with TypeScript, ensuring reliable, predictable behavior.
  • Flexible authentication: Supports both authenticated-only and unauthenticated-only screens.
  • Easy integration: Designed to work seamlessly with React Navigation.

Installation

Install the package using npm or yarn:

npm install react-native-auth-guard
# or
yarn add react-native-auth-guard

Peer Dependencies

Ensure you have the following dependencies installed:

  • react
  • @react-navigation/native

Usage

Step 1: Wrap Your App with AuthConfigProvider

The AuthConfigProvider should be set up at the root level of your app. Configure it with:

  • getAuthState: A function returning a boolean indicating the current authentication state.
  • unauthenticatedAction: A function to execute when an unauthenticated user tries to access an authenticated-only screen.
  • authenticatedAction: A function to execute when an authenticated user tries to access an unauthenticated-only screen.
import React from 'react';
import { AuthConfigProvider } from 'react-native-auth-guard';
import AppNavigator from './AppNavigator';
import { getAuthState } from './authState';

const App = () => (
  <AuthConfigProvider
    getAuthState={getAuthState}
    unauthenticatedAction={() => {
      // Define your unauthenticated action, e.g., navigate to the Login screen
    }}
    authenticatedAction={() => {
      // Define your authenticated action, e.g., navigate to the Home screen
    }}
  >
    <AppNavigator />
  </AuthConfigProvider>
);

export default App;

Step 2: Define Your Authentication State Logic

Define your authentication state and actions in authState.ts (or any relevant file):

let isAuthenticated = false;

export const getAuthState = () => isAuthenticated;

export const login = () => {
  isAuthenticated = true;
};

export const logout = () => {
  isAuthenticated = false;
};

Step 3: Use withAuthGuard in Navigator Stack Definitions

Apply the withAuthGuard HOC to enforce authentication requirements directly within your navigation stack definitions:

// AppNavigator.tsx

import React from 'react';
import { createStackNavigator } from '@react-navigation/stack';
import { withAuthGuard, AuthRequirement } from 'react-native-auth-guard';
import HomeScreen from './screens/HomeScreen';
import LoginScreen from './screens/LoginScreen';

const Stack = createStackNavigator();

const AppNavigator = () => (
  <Stack.Navigator>
    <Stack.Screen
      name="Home"
      component={withAuthGuard(HomeScreen, {
        authRequirement: AuthRequirement.Authenticated,
      })}
    />
    <Stack.Screen
      name="Login"
      component={withAuthGuard(LoginScreen, {
        authRequirement: AuthRequirement.Unauthenticated,
      })}
    />
    {/* Add other screens as needed */}
  </Stack.Navigator>
);

export default AppNavigator;

By defining withAuthGuard directly in the navigator stack, you ensure that authentication requirements are applied consistently when navigating between screens.

Alternative: Use withAuthGuard Directly in Screen Files

If you prefer, you can also use withAuthGuard within individual screen files, wrapping the component before exporting it.

// HomeScreen.tsx

import React from 'react';
import { View, Text } from 'react-native';
import { withAuthGuard, AuthRequirement } from 'react-native-auth-guard';

const HomeScreen: React.FC = () => (
    <View>
        <Text>Welcome to the Home Screen!</Text>
</View>
);

export default withAuthGuard(HomeScreen, {
    authRequirement: AuthRequirement.Authenticated,
});

API

AuthConfigProvider

This context provider requires:

  • getAuthState: A function returning a boolean indicating authentication status.
  • unauthenticatedAction: A function executed when an unauthenticated user attempts to access an authenticated-only screen.
  • authenticatedAction: A function executed when an authenticated user attempts to access an unauthenticated-only screen.

withAuthGuard

A higher-order component for protecting screens with authentication requirements.

Options:

  • authRequirement: One of AuthRequirement.Authenticated or AuthRequirement.Unauthenticated.

AuthRequirement

An enum defining the authentication requirements:

  • Authenticated: The screen is accessible only to authenticated users.
  • Unauthenticated: The screen is accessible only to unauthenticated users.

Limitations

Limited Granular Control:

Applying withAuthGuard directly in the navigator or screen file enforces a fixed authentication requirement. Adjusting requirements dynamically based on changing app conditions (e.g., role-based access) is more challenging.

Redirection Timing:

Redirects are triggered based on the authRequirement, but there may be slight delays in asynchronous authentication checks (e.g., when the auth state relies on API calls or async storage). This can cause flickers where a screen briefly displays before redirecting.

Lack of Deep Navigation Awareness:

When navigating deeply within nested stacks or tab navigators, the withAuthGuard may not always account for the full navigation context. It’s designed for single-level navigation, so nested navigator overrides aren’t supported in this basic setup.

Global State Dependence:

Since it relies on a global AuthConfigProvider context, any issues with the context provider (e.g., missing or unmounted context) could prevent authentication requirements from applying, especially if the context isn’t correctly propagated in large, multi-screen apps.

Initial Render Handling:

Without preloading the auth state, unauthenticated screens can briefly render before unauthenticatedAction redirects, leading to UX issues.

Testing

The library includes Jest test cases to verify the correct behavior of the HOC and context under various conditions. Run tests with:

npm test

Example

The example directory contains a sample project demonstrating usage of react-native-auth-guard.

License

This project is licensed under the MIT License. See the LICENSE file for details.