1.0.3 • Published 2 years ago
simple-front-jwt-auth v1.0.3
Simple JWT authentication
Simple JWT authentication allows you to authenticate user on client side based on provided JSON Web Token.
Installation
Install with npm
Run npm install simple-front-jwt-auth to install the library
Usage
- To login user use
handleLoginmethod
import { handleLogin, setLoginCallback } from "simple-front-jwt-auth";
// optionally you can set callback after login
setLoginCallback(() => {
// do something
});
// login user with token and set token expiration in seconds
handleLogin(token, 3600);- To auto login user use
checkAutologinmethod. User will be authenticated only if all the required data is available and the token has not expired. Otherwise user will be logged out
import { checkAutologin, setAutologinCallback } from "simple-front-jwt-auth";
// optionally you can set callback after auto login
setAutologinCallback(() => {
// do something
});
// check if user should be authenticated then log in or log out user
checkAutologin();- To logout user use
handleLogoutmethod
import { handleLogout, setLogoutCallback } from "simple-front-jwt-auth";
// optionally you can set callback after logout
setLogoutCallback(() => {
// do something
});
// logout user
handleLogout();- To get user use
getUsermethod. This method returnsuserif user is authenticated otherwisenull
import { getUser } from "simple-front-jwt-auth";
// get user
const user = getUser();