react-starter-auth
v0.1.81
Published
React Next Starter Authentication
Downloads
24
Maintainers
Readme
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: '[email protected]',
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'