Skip to content

Accessors

MohammadAli Arjomand edited this page Sep 12, 2023 · 5 revisions

Accessors are checker for access to routes

Create Accessor

First, make a accessor via singlighter

node singlighter make:accessor UserAgentAccessor

All accessors is in ~/Scripts/Accessors, so UserAgentAccessor is in ~/Scripts/Accessors/UserAgentAccessor.js

If accessor return true, SinglightJs will routing user to the page, but if return false, user get 403 Error

For example, i want create a accessor to check user agent, if contains Chrome can access and if not, get 403 Error

export default function UserAgentAccessor() {
   return navigator.userAgent.includes("Chrome");
}

Now i need to register accessor in the manager, import accessor in ~/Scripts/Accessors/Manager.js

import UserAgentAccessor from './UserAgentAccessor.js';

Then register it

export default {
    UserAgentAccessor
};

Use Accessor

For using accessors, write accessor name without Accessor to fourth router parameter

router.addRoute("/", HomePage, null, "UserAgent");

And you can more than 1 accessors

router.addRoute("/", HomePage, null, "UserAgent,Ip,Auth");
// or
router.addRoute("/", HomePage, null, ["UserAgent", "Ip", "Auth"]);

NOTICE The old usage method also is available

router.addRoute("/", HomePage, null, () => false);

IMPORTANT SinglightJs is a front-end framework and accessors working on front-end, so DON'T use SinglightJs-Accessors for very important things

Clone this wiki locally