Skip to content

Authentication status

Introduction

To get the information of whether the user is authenticated or not, React Auth Kit comes with isAuth functions


IsAuth functionality available in both hook and Higher Order Component

  • For Functional Components, you can use useIsAuthenticated() hook inside any components
  • For class-based components, you can wrap the component inside withIsAuthenticated() HOC function.

Hook

Check the authentication status in React Functional Components(FC) by adding the useIsAuthenticated hook inside it.

Import

Import useIsAuthenticated in your app
import useIsAuthenticated from 'react-auth-kit/hooks/useIsAuthenticated'

Demo

Component.js
import React from 'react';
import useIsAuthenticated from 'react-auth-kit/hooks/useIsAuthenticated'

const AnyComponent = () => {
    const isAuthenticated = useIsAuthenticated()

    if(isAuthenticated){
        // Redirect to Dashboard
    }
    else {
        // Redirect to Login
    }
}

Higher Order Component

Import

Import withAuthUser in your app
import withIsAuthenticated from 'react-auth-kit/hoc/withIsAuthenticated';

Usage

component.jsx
import React from "react";
import withIsAuthenticated from 'react-auth-kit/hoc/withIsAuthenticated';

class SomeComponent extends React.Component {

    render(){
        if(this.props.isAuthenticated){
            // Redirect to Dashboard
        }
        else {
            // Redirect to Login
        }
    }
}

export default withIsAuthenticated(SomeComponent)

API


— 🔑 —

React Auth Kit is Apache 2.0 License code