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
Props | is required | Description |
---|
body | true | Body of login request, including username and password |
onSuccess | false | A function executed when login success |
onWrongCredentials | false | A function executed when wrong username or password |
onBadRequest | false | A function executed when wrong validate |
onError | false | A 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
Props | is required | Description |
---|
accessToken | true | Access token |
body | true | Body of check permission request |
onSuccess | false | A function executed when login success |
onBadRequest | false | A function executed when wrong validate |
onUnauthorized | false | A function executed when wrong access token |
onError | false | A 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
},
});
Props | is required | Description |
---|
accessToken | true | Access token |
onSuccess | false | A function executed when success |
onBadRequest | false | A function executed when wrong validate |
onUnauthorized | false | A function executed when wrong access token |
onError | false | A function executed when other errors |