auth-token-registry
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

Npm install auth-token-registry

Usage

// Create new token with user object
const [token, refreshToken] = await createTokens(user, SECRET_1, SECRET_2);

// Refresh token
const { token, refreshToken, user } = refreshTokens(refreshToken, findUser, SECRET_1, SECRET_2);

Example

const { createTokens, refreshTokens, decode } = require('auth-token-registry');

// Create token when user logs in
function userLogin(user) {
  createUser(user).then(async res => {
    const [token, refreshToken] = await createTokens(user, SECRET_1, SECRET_2);
  });
}

// In auth middleware token gets refreshed when it's expired
async function authMiddleware() {
  try {
    // if can't decode throws error
    await decode(refreshToken, SECRET_2); 
    // findUser is a function that receives user id and checks in db for user existance
    const newTokens = await refreshTokens(refreshToken, findUser, SECRET_1, SECRET_2);
  } 
  catch (err) { throw err }
}

Package Sidebar

Install

npm i auth-token-registry

Weekly Downloads

1

Version

1.0.3

License

MIT

Unpacked Size

92.4 kB

Total Files

12

Last publish

Collaborators

  • edo1792