Skip to content

Authentication library built for react token based authentication with JWT

Notifications You must be signed in to change notification settings

akosidencio/react-starter-auth

Repository files navigation

Features

Authentication library built for react token based authentication with JWT

  • Lightweight and easy to use
  • Built for React JS
  • Works with Next js
  • JWT based authentication
  • Secure client authentication

Installation

  npm install react-starter-auth
  
  yarn react-starter-auth

Setup

import { AuthProvider } from 'react-starter-auth';

<AuthProvider>
  <App>
</AuthProvider>

Signin

import { useAuth } from 'react-starter-auth'

const { signIn } = useAuth()

const access_token = 'jsjdjdjsxxfd' // response from api
const user {
    name: 'john doe',
    email: 'example@example.com',
    phone: '',
    role: ''
}

const authuser = {
  token: access_token,
  user: user
}
signIn(authuser)

User

import { useAuth } from 'react-starter-auth'

const { isAuthenticated, user } = useAuth()

Fetcher

fetcher extends the native Web fetch() API to update each request on the server to set headers Authorizaton Bearer upon sign in.

import { fetcher } from 'react-starter-auth'

const res = fetcher('https://example.com/api/posts') // GET
const data = await res.json()

const res = fetcher('https://example.com/api/posts', { method: 'POST', body: JSON.stringify(data) }) // POST
const data = await res.json()

Private route page

import { ProtectedRoute, withAuthentication } from 'react-starter-auth'

About

Authentication library built for react token based authentication with JWT

Resources

Stars

Watchers

Forks

Packages

No packages published