GoTrue Documentation

repository·master·Indexed 26 days ago

https://github.com/netlify/gotrue

GoTrue is an open-source API written in Golang for user management, registration, and authentication in Jamstack projects, utilizing OAuth2 and JWT. It provides endpoints for user signup, invitation, password recovery, and token exchange, as well as support for external providers like GitHub, Google, GitLab, and Bitbucket. The documentation covers environment variable configuration, database migrations for MySQL, webhook setup, and administrative CLI commands for user management.

Tokens
3.3K
Snippets
14
Records
27
Agent score
88%

What's inside GoTrue

  1. Configure GoTrue Database settings and run migrations

    master

    GoTrue requires a database connection. Currently, only mysql is supported.

    • DB_DRIVER (string, required): Must be mysql.
    • DATABASE_URL or DB_DATABASE_URL (string, required): Connection string for the database.
    • DB_NAMESPACE (string): Prefix added to all table names.

    Important: Migrations are not applied automatically. You must run them after building GoTrue.

    GOTRUE_DB_DRIVER=mysql
    DATABASE_URL=root@localhost/gotrue

    Run migrations locally

    ./gotrue migrate

    Run migrations using Docker

    docker run --rm gotrue gotrue migrate
  2. Configure GoTrue via environment variables

    master
    GoTrue can be configured using a .env file or environment variables. Environment variables prefixed with GOTRUE_ take precedence over values in a configuration file. Configuration is categorized into Top-Level, API, Database, Logging, Opentracing, JWT, External Providers, and E-Mail settings.
  3. Configure GoTrue API settings

    master

    Use these settings to control the API server behavior:

    • GOTRUE_API_HOST (string): Hostname to listen on.
    • PORT or API_PORT (number): Port number to listen on. Defaults to 8081.
    • GOTRUE_API_ENDPOINT (string): Multi-instance mode only. Controls the endpoint Netlify accesses.
    • REQUEST_ID_HEADER (string): Specify the name of the header to inherit a request ID from.
    • GOTRUE_API_EXPORT_SECRET (string): Secret used to allow exporting users for service migration.
    GOTRUE_API_HOST=localhost
    PORT=9999
  4. Configure GoTrue Webhooks

    master

    Webhooks allow you to receive notifications for specific events:

    • WEBHOOK_URL (string): The endpoint to receive webhook calls.
    • WEBHOOK_SECRET (string): Shared secret used to sign the request (JSON Web Signature). Use this to verify request integrity.
    • WEBHOOK_RETRIES (number): Number of retry attempts for failed hooks.
    • WEBHOOK_TIMEOUT_SEC (number): Time between retries in seconds.
    • WEBHOOK_EVENTS (list): Comma-separated list of events to trigger webhooks. Supported events: validate, signup, login, userdeleted, usermodified.
  5. Configure GoTrue E-Mail (SMTP)

    master

    Email is highly recommended for password recovery. Required settings include:

    • SMTP_HOST (string, required): Mail server hostname.
    • SMTP_PORT (number, required): Mail server port.
    • SMTP_ADMIN_EMAIL (string, required): The From address for all emails.
    • SMTP_USER (string): Username for authentication.
    • SMTP_PASS (string): Password for authentication.
    • SMTP_MAX_FREQUENCY (number): Minimum seconds between sending signup/reset emails. Defaults to 900.
    • SMTP_RESERVED_DOMAINS (string): Comma-separated list of domains that cannot be used as the admin_email sender to prevent spoofing.
    • MAILER_AUTOCONFIRM (bool): If true, email confirmation is not required. Defaults to false.

    Email Templates and Subjects:

    • MAILER_SUBJECTS_CONFIRMATION, MAILER_SUBJECTS_RECOVERY, etc.: Custom subjects for different email types.
    • MAILER_TEMPLATES_CONFIRMATION, MAILER_TEMPLATES_RECOVERY, etc.: URL paths to custom HTML templates. Available variables in templates: {{ .SiteURL }}, {{ .Email }}, {{ .ConfirmationURL }}, {{ .NewEmail }}.
    GOTRUE_SMTP_HOST=smtp.mandrillapp.com
    GOTRUE_SMTP_PORT=587
    GOTRUE_SMTP_USER=smtp-delivery@example.com
    GOTRUE_SMTP_PASS=correcthorsebatterystaple
    GOTRUE_SMTP_ADMIN_EMAIL=support@example.com
    GOTRUE_MAILER_SUBJECTS_CONFIRMATION="Please confirm"
  6. Configure External Authentication Providers

    master

    GoTrue supports bitbucket, github, gitlab, and google. For each provider X, configure the following:

    • EXTERNAL_X_ENABLED (bool): Enables the provider.
    • EXTERNAL_X_CLIENT_ID (string, required): OAuth2 Client ID.
    • EXTERNAL_X_SECRET (string, required): OAuth2 Client Secret.
    • EXTERNAL_X_REDIRECT_URI (string, required for gitlab): The URI for OAuth2 redirection.
    • EXTERNAL_X_URL (string): Base URL for authorization/token requests. Used by gitlab only. Defaults to https://gitlab.com.
    GOTRUE_EXTERNAL_GITHUB_CLIENT_ID=myappclientid
    GOTRUE_EXTERNAL_GITHUB_SECRET=clientsecretvaluessssh
  7. Configure GoTrue Logging

    master

    Control the output verbosity and destination of logs:

    • LOG_LEVEL (string): Log level. Options: panic, fatal, error, warn, info, or debug. Defaults to info. (Note: This variable does not use the GOTRUE_ prefix).
    • GOTRUE_LOG_FILE (string): Path to a valid file where logs should be written.
    LOG_LEVEL=debug
    GOTRUE_LOG_FILE=/var/log/go/gotrue.log
  8. Configure GoTrue JSON Web Tokens (JWT)

    master

    Settings for signing and validating JWTs:

    • JWT_SECRET (string, required): The secret used to sign tokens.
    • JWT_EXP (number): Token validity duration in seconds. Defaults to 3600 (1 hour).
    • JWT_AUD (string): Default JWT audience.
    • JWT_ADMIN_GROUP_NAME (string): Name of the admin group. Defaults to admin.
    • JWT_DEFAULT_GROUP_NAME (string): The default group assigned to all new users.
    GOTRUE_JWT_SECRET=supersecretvalue
    GOTRUE_JWT_EXP=3600
    GOTRUE_JWT_AUD=netlify
  9. Configure GoTrue Opentracing (Datadog)

    master

    GoTrue supports Datadog for tracing. Enable it using these settings:

    • GOTRUE_TRACING_ENABLED (bool): Enables tracing. Defaults to false.
    • GOTRUE_TRACING_HOST (string): The tracing destination host.
    • GOTRUE_TRACING_PORT (number): The port for the tracing host.
    • GOTRUE_TRACING_TAGS (string): Comma-separated list of key:value pairs to add to all spans.
    • GOTRUE_SERVICE_NAME (string): The name of the service.
    GOTRUE_TRACING_ENABLED=true
    GOTRUE_TRACING_HOST=127.0.0.1
    GOTRUE_TRACING_PORT=8126
    GOTRUE_TRACING_TAGS="tag1:value1,tag2:value2"
    GOTRUE_SERVICE_NAME="gotrue"
  10. Configure Top-Level GoTrue settings

    master

    Use these settings for core service behavior:

    • GOTRUE_SITE_URL (string, required): The base URL of your site. Used to construct email URLs.
    • OPERATOR_TOKEN (string): Multi-instance mode only. Shared secret used to verify requests proxied through an operator.
    • DISABLE_SIGNUP (bool): If true, new users can only be created via invites. Defaults to false.
    • GOTRUE_RATE_LIMIT_HEADER (string): The header used to rate limit the /token endpoint.
    GOTRUE_SITE_URL=https://example.netlify.com/
  11. Register a new user with POST /signup

    master

    Register a new user by providing an email and password.

    Request Body:

    {
      "email": "email@example.com",
      "password": "secret"
    }

    Response: Returns the user object including id, email, confirmation_sent_at, created_at, and updated_at.

  12. Exchange credentials for tokens with POST /token

    master

    An OAuth2 endpoint that supports password, refresh_token, and authorization_code grant types.

    Grant Types:

    • password: grant_type=password&username=email@example.com&password=secret
    • refresh_token: grant_type=refresh_token&refresh_token=my-refresh-token

    Authentication: Once you have an access_token, include it in subsequent requests using the header: Authorization: Bearer YOUR_ACCESS_TOKEN_HERE.

    Response: Returns an object with access_token, token_type, expires_in, and refresh_token.

    grant_type=password&username=email@example.com&password=secret