PhoenixKit.Users.OAuthConfig (phoenix_kit v1.7.206)

Copy Markdown View Source

Runtime OAuth configuration management using database credentials.

This module provides functions to configure OAuth providers at runtime by reading credentials from the database and updating the application configuration dynamically.

Summary

Functions

Configures a specific OAuth provider from database settings.

Configures all OAuth providers from database settings.

Tests OAuth connection for a specific provider.

Tests OAuth credentials for a specific provider against an explicit credentials map, instead of reading from the database.

Validates OAuth credentials for a specific provider.

Functions

configure_provider(provider)

Configures a specific OAuth provider from database settings.

Examples

iex> PhoenixKit.Users.OAuthConfig.configure_provider(:google)
:ok

configure_providers()

Configures all OAuth providers from database settings.

This function reads OAuth credentials from the database and updates the application configuration at runtime. It should be called:

  • On application startup
  • After updating OAuth credentials via admin UI

Skips configuration if OAuth is disabled in settings (oauth_enabled = false).

Examples

iex> PhoenixKit.Users.OAuthConfig.configure_providers()
:ok

test_connection(provider)

Tests OAuth connection for a specific provider.

This function validates the credentials format but does not make actual API calls. For true connection testing, OAuth flow needs to be initiated through the browser.

Examples

iex> PhoenixKit.Users.OAuthConfig.test_connection(:google)
{:ok, "Google OAuth credentials are properly formatted"}

test_connection(provider, credentials)

Tests OAuth credentials for a specific provider against an explicit credentials map, instead of reading from the database.

Use this to validate unsaved form values (e.g. an admin "Test Credentials" button) before the settings are saved — test_connection/1 would otherwise validate the stale, already-persisted credentials.

Examples

iex> PhoenixKit.Users.OAuthConfig.test_connection(:google, %{client_id: "x", client_secret: "y"})
{:ok, "Google OAuth credentials are properly formatted. Initiate OAuth flow to test actual connection."}

validate_credentials(provider)

Validates OAuth credentials for a specific provider.

Returns {:ok, provider} if credentials are valid, or {:error, reason} if not. Uses direct database read for accurate validation.

Examples

iex> PhoenixKit.Users.OAuthConfig.validate_credentials(:google)
{:ok, :google}