# @pondoknusa/auth

Authentication and authorization for Pondoknusa — session guards, hashed API tokens (`tyr_*`),
CSRF protection, Gate policies, password resets, and social OAuth with PKCE.

Auth is its own package so any app — full-stack, JSON API, or headless — gets the same guards,
token repository, CSRF verification, password-reset broker, and OAuth drivers without
re-assembling them on top of the HTTP kernel. It stays presentation-agnostic: a guard reads the
current user from a `PondoknusaRequest`, the user provider is an Eloquent model, and the `Auth`,
`Gate`, and `Password` facades in `@pondoknusa/core` are thin wrappers over what this package
registers. Sessions persist to the database by default — or Redis, or an in-memory array for
tests — and can be encrypted at rest through `@pondoknusa/crypto`.

> Part of the [Pondoknusa](https://pondoknusa.dev) framework — a TypeScript-native full-stack
> framework built on standard Web APIs. Requires **Node.js ≥ 26**.

## Install

```bash
npm install @pondoknusa/auth @pondoknusa/database
```

## Quick start

Scaffold the `User` model, auth routes, and migrations, then migrate:

```bash
pondoknusa auth:install
pondoknusa migrate
```

Guards are declared in `config/auth.ts`; the `Auth` and `Gate` facades drive login, tokens, and
authorization:

```typescript
import { Auth, Gate } from '@pondoknusa/core';

// Sign in against the default session guard
const authenticated = await Auth.attempt({ email: 'ada@example.com', password: 'secret' });

// Issue a personal access token — only a SHA-256 hash is stored, shown once as plainTextToken
const token = await Auth.createToken('mobile-app', ['posts:read', 'posts:write'], {
  expiresIn: '90d',
});

// Authorize an ability against a policy registered in config/auth.ts
await Gate.authorize(Auth.user(), 'update', post);
```

Protect routes with the guard middleware aliases the scaffold registers:

```typescript
import { Route } from '@pondoknusa/core';

Route.middleware('auth').get('/dashboard', showDashboard);
Route.middleware('auth:api').get('/api/me', showProfile);
```

Social OAuth ships with GitHub, Google, Discord, Microsoft, X, Facebook, LinkedIn, Apple,
GitLab, Slack, Spotify, Twitch, and Bitbucket drivers — all using PKCE for the authorization
code flow. Register your own providers with `registerOAuthDriver(name, Driver)` before auth
boots.

## Related packages

- [`@pondoknusa/core`](https://www.npmjs.com/package/@pondoknusa/core) — hosts the `Auth`,
  `Gate`, and `Password` facades this package backs.
- [`@pondoknusa/auth-oauth`](https://www.npmjs.com/package/@pondoknusa/auth-oauth) — add an
  OAuth2 authorization server so third-party clients can act on behalf of your users.
- [`@pondoknusa/auth-passkey`](https://www.npmjs.com/package/@pondoknusa/auth-passkey) — add
  WebAuthn / passkey sign-in alongside passwords.
- [`@pondoknusa/crypto`](https://www.npmjs.com/package/@pondoknusa/crypto) — post-quantum
  session encryption and signed tokens.

## Documentation

- [Authentication guide](https://pondoknusa.dev/guide/auth)
- [Package reference](https://pondoknusa.dev/reference/generated/packages/auth)
- [Source](https://git.simonharms.com/thesimonharms/pondoknusa/src/branch/main/packages/auth)

MIT © Pondoknusa Contributors
