Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/osskit/authorized-rules


https://github.com/osskit/authorized-rules

Last synced: 9 days ago
JSON representation

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
};
```