[**Documentation**](../../../README)

***

[Documentation](../../../README) / [@zerotal/auth](../README) / Gate

# Variable: Gate

> `const` **Gate**: [`GateService`](../classes/GateService)

Defined in: [auth/src/Gate.ts:40](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/auth/src/Gate.ts#L40)

The `Gate` facade — model authorization, backed by [GateService](../classes/GateService).

## Remarks

Resolves the `gate` binding from the container, so it is usable only after
`app.boot()`. It routes an ability check through, in order: `before` hooks
(e.g. a super-admin bypass), closure abilities registered with
[defineAbility](../classes/GateService#defineability), a [Policy](../classes/Policy) registered
for the model's class via [registerPolicy](../classes/GateService#registerpolicy),
and finally the user's own relational permissions (`user.can(ability)`).

Use [allows](../classes/GateService#allows) for a boolean and
[authorize](../classes/GateService#authorize) to throw on denial. For abilities
that hit the database, use the `*Async` variants — the sync forms treat a
returned Promise as truthy and would wrongly allow.

## Example

```ts
import { Gate } from '@zerotal/auth';
import { Post } from '../models/Post.ts';
import { PostPolicy } from '../policies/PostPolicy.ts';

// Register a policy for a model class (typically at boot):
Gate.registerPolicy(Post, PostPolicy);

// Define an ad-hoc ability (no model needed):
Gate.defineAbility('update-post', (user, post) => user?.id === post.userId);

// Check in a controller:
Gate.allows('update-post', post);   // boolean
Gate.authorize('delete', post);      // throws ForbiddenError when denied

// Explicit policy form:
Gate.via(PostPolicy).allows('update', post);
```
