1.3.1 • Published 17 days ago

user-context-fran-dev v1.3.1

Weekly downloads
-
License
MIT
Repository
github
Last release
17 days ago

user-context-fran-dev

Made with create-react-library

The intention of user-context is to keep the user login while the token is valid. If no you can use the Logout function to erase the token saved in localStorage to be able to redirect the user to another view. The context will let you create authenticated requests for secure API's.

NPM JavaScript Style Guide

Install

npm install --save user-context-fran-dev

Usage

import React, { useEffect, useState } from 'react'
import * as userContext from 'user-context-fran-dev'
import jwtDecode from 'jwt-decode'

const App = () => {
  const [ auth, setAuth ] = useState(undefined)
  const [reloadUser, setReloadUser] = useState(false)
 
 const login = () => {
  context.login(token, setAuth)
  }

  const logout = () => {
    context.logout(auth, setAuth)
  }
  
  const autData = useMemo(
    () => ({
      auth,
      login,
      logout,
      setReloadUser,
    }),
    [auth]
  );

 
   useEffect(() => {
    const token = context.getToken()
    if(token){
      setAuth({
        token,
        idUser: jwtDecode(token).id
      })
    } else{
      setAuth(null)

    }
    setReloadUser(false)
  }, [setReloadUser])
  
   return (
    <context.AuthContext.Provider value={autData}>
    </context.AuthContext.Provider>
    )

}

License

MIT © fran-dev

1.3.1

17 days ago

1.3.0

20 days ago

1.2.2

11 months ago

1.1.10

11 months ago

1.2.0

1 year ago

1.1.9

1 year ago

1.2.1

1 year ago

1.1.8

2 years ago

1.1.7

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.0

2 years ago