1.1.0 • Published 5 months ago

auth-eros v1.1.0

Weekly downloads
-
License
ISC
Repository
-
Last release
5 months ago

Staff TGW

Installation

npm i axios@1.6.2
npm i staff-tgw --registry https://npm.thegioiwhey.com

Usage

Import
import axios from "axios";
import Staff from "staff-tgw";
Init class
const staff = new Staff({
  axios,
  baseURL: "url-for-auth",
});
Login
type User = {
  id: number,
  firstName: string,
  lastName: string,
  avatar: string,
  positionName: string,
};

type LoginResponse = {
  accessToken: string,
  user: User,
};

// Solution 1:
staff.login({
  body: {
    username: "your-username",
    password: "your-password",
  },
  onSuccess(data: LoginResponse) {
    // do something when success
  },
  // ... some other function props else
});

// Solution 2:

const { status, data, code } = await staff.login({
  body: {
    username: "your-username",
    password: "your-password",
  },
});

// validate result by status
Propsis requiredDescription
bodytrueBody of login request, including username and password
onSuccessfalseA function executed when login success
onWrongCredentialsfalseA function executed when wrong username or password
onBadRequestfalseA function executed when wrong validate
onErrorfalseA function executed when other errors
Check permission
type CheckPermissionBody = {
  resourceCode: string,
  actionCodes: string[],
  type: "one" | "all",
};

type CheckPermissionResponse = {
  hasPermission: boolean,
  userId: number,
};

// solution 1
staff.checkPermission({
  accessToken: "your-access-token",
  body: {
    resourceCode: "your-resource-code",
    type: "all",
    actionCodes: ["action-code-1", "action-code-2"],
  },
  onSuccess(data: CheckPermissionResponse) {
    // do something when success
    // check has permission from data
  },
  // ... some other function props else
});

// solution 2
// like login
Propsis requiredDescription
accessTokentrueAccess token
bodytrueBody of check permission request
onSuccessfalseA function executed when login success
onBadRequestfalseA function executed when wrong validate
onUnauthorizedfalseA function executed when wrong access token
onErrorfalseA function executed when other errors
Get roles
type AuthRoleResponse = {
  roleActions: {
    resourceCode: string,
    actions: {
      code: string,
      label: string,
      isAllowed: boolean,
    }[],
  }[],
  userId: number,
};

staff.getRoles({
  accessToken: "your-access-token",
  onSuccess(data: AuthRoleResponse) {
    // do something when success
  },
});
Propsis requiredDescription
accessTokentrueAccess token
onSuccessfalseA function executed when success
onBadRequestfalseA function executed when wrong validate
onUnauthorizedfalseA function executed when wrong access token
onErrorfalseA function executed when other errors
1.1.0

5 months ago

1.0.0

5 months ago