# AnyLogin

Provides a development-only account switcher for Phoenix applications.

The runtime is provided by the package itself. The generator only wires the
shared runtime into an application and does not create application-specific
modules.

## Generator

Run the generator from a Phoenix application:

```sh
mix phx.gen.any_login Accounts users
```

The arguments are an existing context module and the users table name. The
application's `UserAuth` module must expose `log_in_user/2` (as generated by
`mix phx.gen.auth`).

Useful options:

```sh
mix phx.gen.any_login Accounts users --web MyAppWeb --auth MyAppWeb.UserAuth
```

For a non-standard schema location, pass its full module name explicitly:

```sh
mix phx.gen.any_login Accounts users --schema MyApp.Accounts.User
```

It updates the application automatically:

- configures the repository, schema, and authentication module
- adds the AnyLogin Plug to the browser pipeline
- adds the account-switcher route under `/dev`
- renders the component in the root layout

The integration is idempotent, so rerunning the generator does not duplicate
these additions. Pass `--no-inject` to print manual integration instructions
without modifying the project.

## Generated Integration

For local development, add the package using a path dependency:

```elixir
def deps do
  [
    {:any_login, path: "../any_login"}
  ]
end
```

Then fetch dependencies and run the generator from the Phoenix application:

```sh
mix deps.get
mix phx.gen.any_login Accounts users
```

The generator adds this development configuration:

```elixir
config :any_login,
  repo: MyApp.Repo,
  schema: MyApp.Accounts.User,
  auth: MyAppWeb.UserAuth
```

That command performs the complete integration. The resulting browser pipeline
contains:

```elixir
pipeline :browser do
  plug :accepts, ["html"]
  plug :fetch_session
  plug :fetch_live_flash
  plug :put_root_layout, html: {MyAppWeb.Layouts, :root}
  plug :protect_from_forgery
  plug :put_secure_browser_headers
  plug :fetch_current_scope_for_user
  plug AnyLogin.Plug,
    enabled: Application.compile_env(:my_app, :dev_routes, false)
end
```

The development-only route is added as:

```elixir
if Application.compile_env(:my_app, :dev_routes, false) do
  scope "/dev", AnyLogin do
    pipe_through :browser

    post "/account-switcher", Controller, :switch
  end
end
```

The shared component is added to the root layout:

```heex
<%= if assigns[:any_login_enabled] do %>
  <AnyLogin.Component.account_switcher
    users={@any_login_users}
    current_user={assigns[:current_scope] && assigns[:current_scope].user}
    return_to={@any_login_return_to}
  />
<% end %>
```


Start the application normally:

```sh
mix phx.server
```

The switcher appears in the bottom-left corner only when `:dev_routes` is
enabled.

## Installation

If [available in Hex](https://hex.pm/docs/publish), the package can be installed
by adding `any_login` to your list of dependencies in `mix.exs`:

```elixir
def deps do
  [
    {:any_login, "~> 0.1.0"}
  ]
end
```

Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
be found at <https://hexdocs.pm/any_login>.
