Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/osskit/authorized-rules
https://github.com/osskit/authorized-rules
Last synced: 9 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/osskit/authorized-rules
- Owner: osskit
- Created: 2021-11-26T10:28:57.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-12-15T20:22:08.000Z (about 1 year ago)
- Last Synced: 2024-12-08T23:48:51.668Z (16 days 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: '[email protected]', scopes: ['scope-1', 'scope-2'] }, or([userHasAccess(['scope-1']), userIsAdmin()]));
console.log('valid!');
};export const invalidExample = () => {
authorize({ admin: true, email: '[email protected]', scopes: ['scope-1', 'scope-2'] }, or([userHasAccess(['scope-3']), userIsAdmin()]));
// throws 403 Forbidden error
};
```