# adonisjs/ally architecture

> Architecture diagram of the GitHub repository adonisjs/ally, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/adonisjs/ally
- Repository: https://github.com/adonisjs/ally

## Overview

1. **What this repo is**
   - `@adonisjs/ally` is a reusable **AdonisJS package** for social login.
   - It is not an application by itself; it provides the integration layer between an AdonisJS host app and external OAuth providers such as GitHub, Google, Twitter, X, Facebook, Discord, Spotify, and LinkedIn.

2. **Main boundary**
   - The core architectural boundary is: **AdonisJS app → Ally package → external auth provider**.
   - The package owns redirect URL generation, provider-specific OAuth handling, and user/profile normalization. The host app owns routes, session handling, and what to do after authentication succeeds.

3. **Public entry and framework integration**
   - `index.ts` is the package entrypoint.
   - `providers/ally_provider.ts` is the AdonisJS service provider integration point, responsible for registering the package into the framework container/lifecycle.
   - `configure.ts` and `src/define_config.ts` support package configuration inside the consuming app, likely through AdonisJS’s config/installer conventions.

4. **Central orchestration layer**
   - `src/ally_manager.ts` is the main runtime coordinator.
   - It likely acts as the factory/registry for provider drivers, exposing a uniform API to start auth flows and process callbacks regardless of OAuth provider type.

5. **Protocol abstraction**
   - `src/abstract_drivers/oauth1.ts` and `src/abstract_drivers/oauth2.ts` capture the shared mechanics for the two auth families.
   - This is a key architectural split: provider drivers inherit from one of these abstractions instead of implementing the protocol from scratch.

6. **Provider-specific implementations**
   - `src/drivers/` contains concrete integrations for each service:
     - GitHub, Google, Facebook, Discord, Spotify, LinkedIn, LinkedIn OpenID Connect, Twitter, and Twitter X.
   - These files are where provider-specific endpoints, scopes, token exchange details, and profile mapping are isolated.

7. **Request/response modeling**
   - `src/redirect_request.ts` encapsulates the redirect step of the OAuth flow.
   - `src/types.ts` and `src/errors.ts` define the package’s public contracts and failure modes, so the manager and drivers can communicate consistently.

8. **Legacy or compatibility surface**
   - The top-level `providers/` folder contains `ally_provider.ts`, which suggests a packaged compatibility/export boundary separate from the core source tree.
   - `src/ally_manager.ts` plus the provider file form the framework-facing surface area of the library.

9. **Consumer configuration and scaffolding**
   - `stubs/config/ally.stub` and `stubs/main.ts` are installation/scaffold artifacts used when the package is added to a host project.
   - `examples/config/*.ts` shows provider-specific configuration patterns for each supported service.

10. **Example applications**
   - The `examples/` directory demonstrates end-to-end usage, with one example per provider plus `examples/app.ts`.
   - These examples are important because they show the intended data flow from app route to provider redirect and callback handling.

11. **Tests as architecture documentation**
   - `tests/ally_manager.spec.ts`, `tests/ally_provider.spec.ts`, `tests/configure.spec.ts`, `tests/define_config.spec.ts`, and `tests/redirect_request.spec.ts` validate the package’s main seams.
   - The tests focus on orchestration, configuration, and redirect behavior rather than business logic, which reinforces that the package is a framework integration layer.

12. **Repository automation**
   - `.github/workflows/` includes checks, release, labels, and stale automation.
   - This affects delivery and maintenance, but not the runtime architecture of the package itself.

13. **How to draw the diagram**
   - Place the AdonisJS host app at the center-left.
   - Show `AllyProvider` registering `AllyManager` and config into the app.
   - From `AllyManager`, branch to shared OAuth1/OAuth2 abstractions, then to provider-specific drivers.
   - From the drivers, connect outward to the external identity providers.
   - Add `redirect_request.ts` on the path between app requests and provider redirects, and `errors.ts/types.ts` as shared support modules.

Main entry points: [`src/abstract_drivers/oauth2.ts`](https://github.com/adonisjs/ally/blob/6.x/src/abstract_drivers/oauth2.ts) (OAuth2 base), [`src/ally_manager.ts`](https://github.com/adonisjs/ally/blob/6.x/src/ally_manager.ts) (Ally manager), [`configure.ts`](https://github.com/adonisjs/ally/blob/6.x/configure.ts) (Configure), [`providers/ally_provider.ts`](https://github.com/adonisjs/ally/blob/6.x/providers/ally_provider.ts) (Ally provider)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_integration["Framework integration"]
  node_host_app["AdonisJS app<br/>host app"]
  node_index_ts["Package entry<br/>entrypoint<br/>[index.ts]"]
  node_ally_provider["Ally provider<br/>service provider<br/>[ally_provider.ts]"]
  node_configure_ts["Configure<br/>installer hook<br/>[configure.ts]"]
  node_define_config["Define config<br/>config schema<br/>[define_config.ts]"]
end

subgraph group_core["Core runtime"]
  node_ally_manager["Ally manager<br/>runtime coordinator<br/>[ally_manager.ts]"]
  node_redirect_request["Redirect req<br/>oauth request"]
  node_types_ts["Types<br/>contracts<br/>[types.ts]"]
  node_errors_ts["Errors<br/>failure model<br/>[errors.ts]"]
end

subgraph group_protocol["OAuth protocol"]
  node_oauth1["OAuth1 base<br/>abstract driver<br/>[oauth1.ts]"]
  node_oauth2["OAuth2 base<br/>abstract driver<br/>[oauth2.ts]"]
end

subgraph group_drivers["Provider drivers"]
  node_github_driver["GitHub<br/>provider driver<br/>[github.ts]"]
  node_google_driver["Google<br/>provider driver<br/>[google.ts]"]
  node_facebook_driver["Facebook<br/>provider driver<br/>[facebook.ts]"]
  node_discord_driver["Discord<br/>provider driver<br/>[discord.ts]"]
  node_spotify_driver["Spotify<br/>provider driver<br/>[spotify.ts]"]
  node_linkedin_driver["LinkedIn<br/>provider driver<br/>[linked_in.ts]"]
  node_linkedin_oidc_driver["LinkedIn OIDC<br/>provider driver"]
  node_twitter_driver["Twitter<br/>provider driver<br/>[twitter.ts]"]
  node_twitter_x_driver["Twitter X<br/>provider driver<br/>[twitter_x.ts]"]
end

subgraph group_consumers["Consumer surface"]
  node_examples_app["Examples app<br/>example app<br/>[app.ts]"]
  node_examples_config["Example config<br/>[ally.ts]"]
  node_stubs["Stubs<br/>scaffold<br/>[main.ts]"]
end

subgraph group_external["External services"]
  node_oauth_services{{"OAuth services<br/>external identity providers"}}
end

node_index_ts -->|"exports"| node_ally_provider
node_index_ts -->|"exports"| node_configure_ts
node_ally_provider -->|"binds"| node_ally_manager
node_configure_ts -->|"uses"| node_define_config
node_ally_provider -->|"loads"| node_configure_ts
node_host_app -->|"installs"| node_ally_provider
node_host_app -->|"configures"| node_configure_ts
node_ally_manager -->|"creates"| node_redirect_request
node_ally_manager -->|"shares"| node_types_ts
node_ally_manager -->|"raises"| node_errors_ts
node_ally_manager -->|"delegates"| node_oauth1
node_ally_manager -->|"delegates"| node_oauth2
node_oauth1 -->|"backs"| node_twitter_driver
node_oauth2 -->|"backs"| node_github_driver
node_oauth2 -->|"backs"| node_google_driver
node_oauth2 -->|"backs"| node_facebook_driver
node_oauth2 -->|"backs"| node_discord_driver
node_oauth2 -->|"backs"| node_spotify_driver
node_oauth2 -->|"backs"| node_linkedin_driver
node_oauth2 -->|"backs"| node_linkedin_oidc_driver
node_oauth1 -->|"backs"| node_twitter_x_driver
node_github_driver -->|"authenticates"| node_oauth_services
node_google_driver -->|"authenticates"| node_oauth_services
node_facebook_driver -->|"authenticates"| node_oauth_services
node_discord_driver -->|"authenticates"| node_oauth_services
node_spotify_driver -->|"authenticates"| node_oauth_services
node_linkedin_driver -->|"authenticates"| node_oauth_services
node_linkedin_oidc_driver -->|"authenticates"| node_oauth_services
node_twitter_driver -->|"authenticates"| node_oauth_services
node_twitter_x_driver -->|"authenticates"| node_oauth_services
node_examples_app -->|"demonstrates"| node_host_app
node_examples_config -->|"shows"| node_define_config
node_stubs -->|"scaffolds"| node_configure_ts

click node_index_ts "https://github.com/adonisjs/ally/blob/6.x/index.ts"
click node_ally_provider "https://github.com/adonisjs/ally/blob/6.x/providers/ally_provider.ts"
click node_configure_ts "https://github.com/adonisjs/ally/blob/6.x/configure.ts"
click node_define_config "https://github.com/adonisjs/ally/blob/6.x/src/define_config.ts"
click node_ally_manager "https://github.com/adonisjs/ally/blob/6.x/src/ally_manager.ts"
click node_redirect_request "https://github.com/adonisjs/ally/blob/6.x/src/redirect_request.ts"
click node_types_ts "https://github.com/adonisjs/ally/blob/6.x/src/types.ts"
click node_errors_ts "https://github.com/adonisjs/ally/blob/6.x/src/errors.ts"
click node_oauth1 "https://github.com/adonisjs/ally/blob/6.x/src/abstract_drivers/oauth1.ts"
click node_oauth2 "https://github.com/adonisjs/ally/blob/6.x/src/abstract_drivers/oauth2.ts"
click node_github_driver "https://github.com/adonisjs/ally/blob/6.x/src/drivers/github.ts"
click node_google_driver "https://github.com/adonisjs/ally/blob/6.x/src/drivers/google.ts"
click node_facebook_driver "https://github.com/adonisjs/ally/blob/6.x/src/drivers/facebook.ts"
click node_discord_driver "https://github.com/adonisjs/ally/blob/6.x/src/drivers/discord.ts"
click node_spotify_driver "https://github.com/adonisjs/ally/blob/6.x/src/drivers/spotify.ts"
click node_linkedin_driver "https://github.com/adonisjs/ally/blob/6.x/src/drivers/linked_in.ts"
click node_linkedin_oidc_driver "https://github.com/adonisjs/ally/blob/6.x/src/drivers/linked_in_openid_connect.ts"
click node_twitter_driver "https://github.com/adonisjs/ally/blob/6.x/src/drivers/twitter.ts"
click node_twitter_x_driver "https://github.com/adonisjs/ally/blob/6.x/src/drivers/twitter_x.ts"
click node_examples_app "https://github.com/adonisjs/ally/blob/6.x/examples/app.ts"
click node_examples_config "https://github.com/adonisjs/ally/blob/6.x/examples/config/ally.ts"
click node_stubs "https://github.com/adonisjs/ally/blob/6.x/stubs/main.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_host_app,node_index_ts,node_ally_provider,node_configure_ts,node_define_config toneBlue
class node_ally_manager,node_redirect_request,node_types_ts,node_errors_ts toneAmber
class node_oauth1,node_oauth2 toneMint
class node_github_driver,node_google_driver,node_facebook_driver,node_discord_driver,node_spotify_driver,node_linkedin_driver,node_linkedin_oidc_driver,node_twitter_driver,node_twitter_x_driver toneRose
class node_examples_app,node_examples_config,node_stubs toneIndigo
class node_oauth_services toneTeal
```

## Components

### Framework integration

Package entrypoints and AdonisJS lifecycle wiring.

- **AdonisJS app**: host app · Consuming application that defines routes and handles post-login behavior.
- [Package entry](https://github.com/adonisjs/ally/blob/6.x/index.ts): entrypoint · Main export surface for the Ally package. · `index.ts`
- [Ally provider](https://github.com/adonisjs/ally/blob/6.x/providers/ally_provider.ts): service provider · Registers the package with the AdonisJS container and lifecycle. · `providers/ally_provider.ts`
- [Configure](https://github.com/adonisjs/ally/blob/6.x/configure.ts): installer hook · Package configuration entry used by consuming applications. · `configure.ts`
- [Define config](https://github.com/adonisjs/ally/blob/6.x/src/define_config.ts): config schema · Builds the package configuration shape and defaults. · `src/define_config.ts`

### Core runtime

Manager, redirect flow, and shared package contracts.

- [Ally manager](https://github.com/adonisjs/ally/blob/6.x/src/ally_manager.ts): runtime coordinator · Central factory and orchestration layer for provider flows. · `src/ally_manager.ts`
- [Redirect req](https://github.com/adonisjs/ally/blob/6.x/src/redirect_request.ts): oauth request · Encapsulates the redirect step of an authentication flow. · `src/redirect_request.ts`
- [Types](https://github.com/adonisjs/ally/blob/6.x/src/types.ts): contracts · Shared public types used across manager and drivers. · `src/types.ts`
- [Errors](https://github.com/adonisjs/ally/blob/6.x/src/errors.ts): failure model · Package-specific error definitions and failure handling. · `src/errors.ts`

### OAuth protocol

Shared mechanics for OAuth1 and OAuth2 providers.

- [OAuth1 base](https://github.com/adonisjs/ally/blob/6.x/src/abstract_drivers/oauth1.ts): abstract driver · Shared OAuth 1.0 flow mechanics for legacy providers. · `src/abstract_drivers/oauth1.ts`
- [OAuth2 base](https://github.com/adonisjs/ally/blob/6.x/src/abstract_drivers/oauth2.ts): abstract driver · Shared OAuth 2.0 flow mechanics for modern providers. · `src/abstract_drivers/oauth2.ts`

### Provider drivers

Concrete integrations for each supported identity provider.

- [GitHub](https://github.com/adonisjs/ally/blob/6.x/src/drivers/github.ts): provider driver · Concrete GitHub OAuth integration. · `src/drivers/github.ts`
- [Google](https://github.com/adonisjs/ally/blob/6.x/src/drivers/google.ts): provider driver · Concrete Google OAuth integration. · `src/drivers/google.ts`
- [Facebook](https://github.com/adonisjs/ally/blob/6.x/src/drivers/facebook.ts): provider driver · Concrete Facebook OAuth integration. · `src/drivers/facebook.ts`
- [Discord](https://github.com/adonisjs/ally/blob/6.x/src/drivers/discord.ts): provider driver · Concrete Discord OAuth integration. · `src/drivers/discord.ts`
- [Spotify](https://github.com/adonisjs/ally/blob/6.x/src/drivers/spotify.ts): provider driver · Concrete Spotify OAuth integration. · `src/drivers/spotify.ts`
- [LinkedIn](https://github.com/adonisjs/ally/blob/6.x/src/drivers/linked_in.ts): provider driver · Concrete LinkedIn OAuth integration. · `src/drivers/linked_in.ts`
- [LinkedIn OIDC](https://github.com/adonisjs/ally/blob/6.x/src/drivers/linked_in_openid_connect.ts): provider driver · LinkedIn OpenID Connect integration. · `src/drivers/linked_in_openid_connect.ts`
- [Twitter](https://github.com/adonisjs/ally/blob/6.x/src/drivers/twitter.ts): provider driver · OAuth integration for Twitter. · `src/drivers/twitter.ts`
- [Twitter X](https://github.com/adonisjs/ally/blob/6.x/src/drivers/twitter_x.ts): provider driver · OAuth integration for X/Twitter variants. · `src/drivers/twitter_x.ts`

### Consumer surface

Example apps and scaffolding used by host projects.

- [Examples app](https://github.com/adonisjs/ally/blob/6.x/examples/app.ts): example app · End-to-end usage sample for host applications. · `examples/app.ts`
- [Example config](https://github.com/adonisjs/ally/blob/6.x/examples/config/ally.ts): example config · Provider configuration examples for supported services. · `examples/config/ally.ts`
- [Stubs](https://github.com/adonisjs/ally/blob/6.x/stubs/main.ts): scaffold · Installation scaffolding copied into consuming projects. · `stubs/main.ts`

### External services

Third-party identity providers reached through OAuth.

- **OAuth services**: external identity providers · External authentication endpoints reached by provider drivers.

## Connections

- Package entry → Ally provider: exports · Package entry exposes the service provider integration.
- Package entry → Configure: exports · Package entry exposes configuration helpers.
- Ally provider → Ally manager: binds · Registers the manager into the AdonisJS application container.
- Configure → Define config: uses · Configuration helper builds the package config shape.
- Ally provider → Configure: loads · Provider participates in app configuration setup.
- AdonisJS app → Ally provider: installs · Consuming app installs the package provider.
- AdonisJS app → Configure: configures · Consuming app applies package configuration.
- Ally manager → Redirect req: creates · Manager prepares redirect flow requests.
- Ally manager → Types: shares · Manager uses package contracts to normalize provider behavior.
- Ally manager → Errors: raises · Manager surfaces package failure modes.
- Ally manager → OAuth1 base: delegates · OAuth1 providers reuse the shared protocol implementation.
- Ally manager → OAuth2 base: delegates · OAuth2 providers reuse the shared protocol implementation.
- OAuth1 base → Twitter: backs · Twitter-style providers use the OAuth1 base.
- OAuth2 base → GitHub: backs · Provider implementations extend the OAuth2 base.
- OAuth2 base → Google: backs · Provider implementations extend the OAuth2 base.
- OAuth2 base → Facebook: backs · Provider implementations extend the OAuth2 base.
- OAuth2 base → Discord: backs · Provider implementations extend the OAuth2 base.
- OAuth2 base → Spotify: backs · Provider implementations extend the OAuth2 base.
- OAuth2 base → LinkedIn: backs · Provider implementations extend the OAuth2 base.
- OAuth2 base → LinkedIn OIDC: backs · OIDC variant extends the OAuth2 base.
- OAuth1 base → Twitter X: backs · X/Twitter provider variants use the OAuth1 base where needed.
- GitHub → OAuth services: authenticates · Driver exchanges credentials with the external provider.
- Google → OAuth services: authenticates · Driver exchanges credentials with the external provider.
- Facebook → OAuth services: authenticates · Driver exchanges credentials with the external provider.
- Discord → OAuth services: authenticates · Driver exchanges credentials with the external provider.
- Spotify → OAuth services: authenticates · Driver exchanges credentials with the external provider.
- LinkedIn → OAuth services: authenticates · Driver exchanges credentials with the external provider.
- LinkedIn OIDC → OAuth services: authenticates · Driver exchanges credentials with the external provider.
- Twitter → OAuth services: authenticates · Driver exchanges credentials with the external provider.
- Twitter X → OAuth services: authenticates · Driver exchanges credentials with the external provider.
- Examples app → AdonisJS app: demonstrates · Examples show how a host app integrates the package.
- Example config → Define config: shows · Example configuration illustrates the generated config shape.
- Stubs → Configure: scaffolds · Scaffold files are installed into consuming projects.
