Authentication

更新时间:
复制 MD 格式

The RDS Supabase authentication module is a full-featured user management system that supports multiple authentication methods and integrates into your application as a standalone module.

Basic operations

Create a new user

  1. Sign up with email.

    To enable email sign-up, configure your mail server (SMTP) information in the Supabase Auth settings on the Alibaba Cloud console. Configure the following parameters:

    Basic configuration parameters:

    GOTRUE_SMTP_PORT                 # SMTP port: The SMTP server port number used to send emails (for example, 587 or 465).
    GOTRUE_EXTERNAL_EMAIL_ENABLED    # Enable external email sign-in: Enables user sign-up and sign-in with email.
    GOTRUE_SMTP_SENDER_NAME          # Mail sender name: The sender name displayed in emails (for example, "Supabase Support Team").
    GOTRUE_SMTP_USER                 # SMTP username: The username for signing in to the SMTP server (usually an email address).
    GOTRUE_SMTP_PASS                 # SMTP password: The password or app-specific password for signing in to the SMTP server.
    GOTRUE_SMTP_ADMIN_EMAIL          # Admin email: The administrator's recipient or sender email address for system emails.
    GOTRUE_SMTP_HOST                 # SMTP host: The SMTP server address (for example, smtp.gmail.com or smtp.mailgun.org).
    GOTRUE_MAILER_AUTOCONFIRM        # Email auto-confirmation: Skips email verification and automatically confirms users. This is often used in development environments.
    GOTRUE_SITE_URL                  # Site URL: The URL of the front-end page where users are redirected after an action, such as a password reset.
    API_EXTERNAL_URL                 # API external URL: The externally accessible API URL used for callbacks or notifications (for example, https://api.xxx.com).

    Email template parameters:

    GOTRUE_MAILER_TEMPLATES_MAGIC_LINK       # Magic link email template: The template for the magic link email sent when a user signs in.
    GOTRUE_MAILER_TEMPLATES_RECOVERY         # Password recovery email template: The template for the password reset email sent when a user requests a password reset.
    GOTRUE_MAILER_TEMPLATES_CONFIRMATION     # Confirmation email template: The template for the confirmation email used to verify a new user's email address after they sign up.
    GOTRUE_MAILER_TEMPLATES_EMAIL_CHANGE     # Email change email template: The template for the email sent to the new email address to confirm an email change request.
    GOTRUE_MAILER_TEMPLATES_INVITE           # Invitation email template: The template for the invitation email sent when an administrator invites a new user to join a project or organization.
    GOTRUE_MAILER_TEMPLATES_REAUTHENTICATION # Reauthentication email template: The template for the reauthentication email sent when a user needs to re-verify their identity, for example, before performing a sensitive operation.
    Note

    For details on configuring email template parameters, see RDS Supabase email authentication and email template configuration.

    After you configure the settings, call the following method:

    const { data, error } = await supabase.auth.signUp({
      email: 'example@email.com',
      password: 'example-password',
    })
  2. Sign up with a phone number.

    Phone number sign-up supports two methods: SMS webhook and Alibaba Cloud SMS Provider.

    SMS webhook: Configure a webhook for your SMS service. Supabase calls this hook to send verification SMS messages. Set the following parameters in the Supabase Auth settings on the Alibaba Cloud console:

    GOTRUE_HOOK_SEND_SMS_ENABLED     # Enables the SMS sending feature.
    GOTRUE_HOOK_SEND_SMS_URI         # The HTTPS service URL for sending confirmation SMS messages.
    GOTRUE_HOOK_SEND_SMS_SECRETS     # The hook secret, which must be a Base64-encoded string prefixed with `v1,whsec_`.
    GOTRUE_SMS_AUTOCONFIRM=false     # Specifies whether to automatically confirm the SMS message.
    GOTRUE_SMS_OTP_EXP=60            # The validity period of the SMS one-time password (OTP), in seconds.

    Alibaba Cloud SMS Provider: Configure the sign name and template for Alibaba Cloud SMS, and provide an AccessKey pair (AccessKey ID and AccessKey secret) with the required SMS permissions (AliyunDysmsFullAccess and AliyunDypnsFullAccess).

    GOTRUE_SMS_PROVIDER=aliyun                 # The SMS provider. Set to aliyun.
    GOTRUE_SMS_ALIYUN_ACCESS_KEY_ID=xxx        # The ID of your Alibaba Cloud AccessKey.
    GOTRUE_SMS_ALIYUN_ACCESS_KEY_SECRET=xxx    # The secret of your Alibaba Cloud AccessKey.
    GOTRUE_SMS_ALIYUN_REGION_ID=cn-hangzhou    # The region for Alibaba Cloud SMS. For information about supported regions, see the note below.
    GOTRUE_SMS_ALIYUN_SIGN_NAME=MySignName     # The sign name for Alibaba Cloud SMS.
    GOTRUE_SMS_ALIYUN_TEMPLATE_CODE=100001     # The template code for Alibaba Cloud SMS.
    GOTRUE_SMS_ALIYUN_IS_TEST=true             # Set this parameter to true if you use the default sign name and template from Alibaba Cloud SMS. If you use a custom sign name and template, omit this parameter or set it to false.
    GOTRUE_SMS_AUTOCONFIRM=false               # Specifies whether to automatically confirm the SMS message.
    GOTRUE_SMS_OTP_EXP=60                      # The validity period of the SMS OTP, in seconds.
    Note
    • When both the SMS webhook and the Alibaba Cloud SMS Provider are configured, the SMS webhook method takes effect because the SMS webhook has a higher priority.

    • For a list of supported Alibaba Cloud SMS regions, see Service Endpoints.

    After you configure the settings, call the following method:

    const { data, error } = await supabase.auth.signUp({
      phone: '1381111****',
      password: 'example-password',
      options: {
        channel: 'sms'
      }
    })
  3. Alipay authentication.

    To enable Alipay sign-in, set the following parameters in the Supabase Auth settings on the Alibaba Cloud console:

    GOTRUE_EXTERNAL_ALIPAY_ENABLED      # Enables or disables Alipay sign-in.
    GOTRUE_EXTERNAL_ALIPAY_CLIENT_ID    # The AppID of your Alipay application.
    GOTRUE_EXTERNAL_ALIPAY_SECRET       # The AppSecret of your Alipay application.
    GOTRUE_EXTERNAL_ALIPAY_REDIRECT_URI # The redirect URI. Format: http(s)://your-supabase-url/auth/v1/callback

    After you configure the settings, call the following method:

    const { data, error } = await supabase.auth.signInWithOAuth({
      provider: 'alipay',
    })
  4. WeChat authentication.

    To enable WeChat sign-in, set the following parameters in the Supabase Auth settings on the Alibaba Cloud console:

    GOTRUE_EXTERNAL_WECHAT_ENABLED      # Enables or disables WeChat sign-in.
    GOTRUE_EXTERNAL_WECHAT_CLIENT_ID    # The AppID of your WeChat application.
    GOTRUE_EXTERNAL_WECHAT_SECRET       # The AppSecret of your WeChat application.
    GOTRUE_EXTERNAL_WECHAT_REDIRECT_URI # The redirect URI. Format: http(s)://your-supabase-url/auth/v1/callback

    After you configure the settings, call the following method:

    const { data, error } = await supabase.auth.signInWithOAuth({
      provider: 'wechat',
    })
  5. Google authentication.

    To enable Google sign-in, set the following parameters in the Supabase Auth settings on the Alibaba Cloud console:

    GOTRUE_EXTERNAL_GOOGLE_ENABLED      # Enables or disables Google sign-in.
    GOTRUE_EXTERNAL_GOOGLE_CLIENT_ID    # The Google OAuth client ID.
    GOTRUE_EXTERNAL_GOOGLE_SECRET       # The Google OAuth client secret.
    GOTRUE_EXTERNAL_GOOGLE_REDIRECT_URI # The redirect URI. Format: http(s)://your-supabase-url/auth/v1/callback

    After you configure the settings, call the following method:

    const { data, error } = await supabase.auth.signInWithOAuth({
      provider: 'google',
    })
  6. GitHub authentication.

    To enable GitHub sign-in, set the following parameters in the Supabase Auth settings on the Alibaba Cloud console:

    GOTRUE_EXTERNAL_GITHUB_ENABLED      # Enables or disables GitHub sign-in.
    GOTRUE_EXTERNAL_GITHUB_CLIENT_ID    # The client ID of your GitHub OAuth App.
    GOTRUE_EXTERNAL_GITHUB_SECRET       # The client secret of your GitHub OAuth App.
    GOTRUE_EXTERNAL_GITHUB_REDIRECT_URI # The redirect URI. Format: http(s)://your-supabase-url/auth/v1/callback

    After you configure the settings, call the following method:

    const { data, error } = await supabase.auth.signInWithOAuth({
      provider: 'github',
    })

Sign in a user

Sign in users with an email and password, a phone number and password, or a third-party login provider.

// Sign in with email
const { data, error } = await supabase.auth.signInWithPassword({
  email: 'example@email.com',
  password: 'example-password',
})

// Sign in with a phone number
const { data, error } = await supabase.auth.signInWithPassword({
  phone: '+1381111****',
  password: 'some-password',
})

// Sign in with an OAuth third-party provider
const { data, error } = await supabase.auth.signInWithOAuth({
  provider: 'provider_name'
})
// Supported values for 'provider':
// 'alipay'  - Alipay sign-in
// 'wechat'  - WeChat sign-in  
// 'google'  - Google sign-in
// 'github'  - GitHub sign-in

Update a user

You can update a signed-in user's profile information. The user must be signed in to call the updateUser() method.

// Update email
const { data, error } = await supabase.auth.updateUser({
  email: 'new****@email.com'
})

// Update password
const { data, error } = await supabase.auth.updateUser({
  password: 'new password'
})

Sign out a user

Sign out the currently signed-in user.

const { error } = await supabase.auth.signOut()

References

  • For more information about Supabase authentication, see Auth.

  • For details on the Supabase JavaScript SDK, see JavaScript.

  • For details on signing in with a phone number, see phone-login.

  • For details on the SMS webhook, see SMS Hook.