https://github.com/osskit/authorized-rules
https://github.com/osskit/authorized-rules
Last synced: 11 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/osskit/authorized-rules
- Owner: osskit
- Created: 2021-11-26T10:28:57.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2023-12-15T20:22:08.000Z (over 2 years ago)
- Last Synced: 2025-06-13T20:06:41.209Z (12 months ago)
- Language: TypeScript
- Size: 64.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# authorized-rules
Add your custom rules to authorize your user's tokens 🔐
## Install
```
yarn add @osskit/authorized-rules
```
## Usage
### Basic Example
```
import { RuleResult, or, authorize } from '@osskit/authorized-rules';
export interface AuthorizedUser {
admin?: boolean;
email?: string;
ids?: string[];
}
const userIsAdmin =
() =>
({ admin }: AuthorizedUser): RuleResult => ({ passed: !!admin, ruleName: 'userIsAdmin' });
export const userHasAccess =
(scopes: string[]) =>
({ scopes }: AuthorizedUser): RuleResult => ({
ruleName: 'userHasAccess',
passed: !!scopes?.length && scopes.every((scope) => scopes.includes(scope)),
});
export const validExample = () => {
authorize({ admin: true, email: 'a@a.com', scopes: ['scope-1', 'scope-2'] }, or([userHasAccess(['scope-1']), userIsAdmin()]));
console.log('valid!');
};
export const invalidExample = () => {
authorize({ admin: true, email: 'a@a.com', scopes: ['scope-1', 'scope-2'] }, or([userHasAccess(['scope-3']), userIsAdmin()]));
// throws 403 Forbidden error
};
```