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

v1.0.3

Published

React Login For MERN Stack

Downloads

5

Readme

react-authify

Authentication Library for MERN Stack

React-Authify is a simple library for implementing authentication in a MERN (MongoDB, Express, React, Node.js) stack. It provides an interface, AuthContextInterface, with properties such as user, isLoggedIn, token, message, loginUser, isLogin, logout

Overall, this library provides a simple and easy-to-use authentication solution for MERN stack applications, allowing developers to implement login and logout functionality without writing extra code.

Integration

To use react-authify for authentication in your backend, check out the node-authify npm package. It provides easy-to-use authentication middleware for Node.js and works seamlessly with react-authify.

Installation

To install the library, use npm or yarn:

npm install react-authify
yarn add react-authify

Usage

Wrap your application with the ReactAuthify component and use the useAuthContext hook to access the authentication context in any component that needs it.

import { ReactAuthify, useAuthContext } from 'react-authify';

function App() {
  return (
    <ReactAuthify>
      <MainComponent />
    </ReactAuthify>
  );
}

// MainComponent.jsx
import { useAuthContext } from 'react-authify';

function MainComponent() {
  const { user, isLoggedIn, token, loginUser, logout } = useAuthContext();

  return (
    <>
      <Route path='login' element={isLoggedIn && token <Navigate  to='/' /> : <Login/>} />
    </>
  );
}

isLogin Functionality

The isLogin function checks if a token is stored in the localStorage of the browser. If a token exists, it retrieves the token and parses it into a JavaScript object. If the token has not expired, the user is set as logged in and the token is stored in the component state using the setUser, setIsLoggedIn, and setToken functions. If the token has expired, it is removed from the localStorage and the user is set as logged out with an empty token and user object.

// MainComponent.jsx
import { useAuthContext } from 'react-authify';

function MainComponent() {
  const { isLogin } = useAuthContext();

  useEffect(() => {
    isLogin();
  }, []);

  return (
    <>
      <Route path='login' element={isLoggedIn && token <Navigate  to='/' /> : <Login/>} />
    </>
  );
}

Login Functionality

The loginUser function is used for logging in the user account. It takes the login URL and the user's credentials {email & password} as arguments, and an optional expirationTimeInHours argument to set the token expiry time. On successful login, it stores the token in the local storage and sets the isLoggedIn, token, and user state variables. On error, it sets the message state variable with the error message.

import { useAuthContext } from 'react-authify';

const { loginUser } = useAuthContext();

const handleLogin = async (event) => {
  event.preventDefault();

  const formData = {
    email,
    password,
  };
  // last argument is optional, you can set logout time based on this.
  // for example 10 = 10 hours, It will auto logout after 10 hours
  await loginUser('/api/login', formData, 10);
};`

Logout Functionality

The logout function removes the token from the local storage and sets the isLoggedIn and token state variables to false and null respectively.

import { useAuthContext } from 'react-authify';

const { logout } = useAuthContext();

<button onClick={logout}>Logout</button>`

Feedback

React-Authify is actively under development to add more features, improve performance, and address any issues reported during testing. Feedback from users testing the library is appreciated.