Hey everyone, I'm currently trying to implement us...
# ory-selfhosting
c
Hey everyone, I'm currently trying to implement user authentication with phone number using SMS sent code. I tried ory-copilot, but the issues never end. So, can you please help? My Kratos phone schema is as follows:
Copy code
{
  "$schema": "<http://json-schema.org/draft-07/schema#>",
  "title": "Phone-Schema-v0",
  "type": "object",
  "properties": {
    "traits": {
      "type": "object",
      "properties": {
        "phone": {
          "type": "string",
          "format": "tel",
          "title": "Phone Number",
          "<http://ory.sh/kratos|ory.sh/kratos>": {
            "credentials": {
              "code": {
                "identifier": true,
                "via": "sms"
              }
            },
            "verification": {
              "via": "sms"
            }
          }
        }
      }
    }
  }
}
My kratos config is as follows (I tried adding templates wherever I can):
Copy code
...
selfservice:
  ...
  methods:
    ...
    code:
      enabled: true
      passwordless_enabled: true
      template: test
      templates:
        sms:
          body: <base64://ZnVuY3Rpb24oY3R4KSB7CiAgImNoYW5uZWwiOiAic21zIiwKICAicHJpb3JpdHk>iOiAxLAogICJyZWNpcGllbnRfbGlzdCI6IGN0eC5yZWNpcGllbnQsCiAgInRlbXBsYXRlX2lkIjogIjU5ZGVlMzZkLWQ4NDEtNDE1ZS1hMjkzLTAwYzE2NjljMDY1NSIsCiAgInRlbXBsYXRlX3BhcmFtZXRlcnMiOiB7CiAgICAiY29kZSI6IGN0eC50ZW1wbGF0ZV9kYXRhLlZlcmlmaWNhdGlvbkNvZGUKICB9Cn0=
      config:
        type: sms
        sms:
          send_otp_url: "https://<my-sms--sending-service>/send"
          template: test
      otp:
        expiry: 600
  flows:
    ...
    registration:
      lifespan: 10m
      ui_url: <http://localhost:4455/registration>
      after:
        ...
        code:
          hooks:
            - hook: session
            - hook: show_verification_ui
identity:
  default_schema_id: "phone_schema_v0"
  schemas:
    - id: email_schema_v0
      url: file:///etc/config/kratos/identity.email_schema_v0.json
    - id: phone_schema_v0
      url: file:///etc/config/kratos/identity.phone_schema_v0.json

courier:
  templates:
    verification_code:
      valid:
        email:
          body:
            plaintext: <base64://ZnVuY3Rpb24oY3R4KSB7CiAgImNoYW5uZWwiOiAic21zIiwKICAicHJpb3JpdHk>iOiAxLAogICJyZWNpcGllbnRfbGlzdCI6IGN0eC5yZWNpcGllbnQsCiAgInRlbXBsYXRlX2lkIjogIjU5ZGVlMzZkLWQ4NDEtNDE1ZS1hMjkzLTAwYzE2NjljMDY1NSIsCiAgInRlbXBsYXRlX3BhcmFtZXRlcnMiOiB7CiAgICAiY29kZSI6IGN0eC50ZW1wbGF0ZV9kYXRhLlZlcmlmaWNhdGlvbkNvZGUKICB9Cn0=
        sms:
          body: 
            plaintext: <base64://ZnVuY3Rpb24oY3R4KSB7CiAgImNoYW5uZWwiOiAic21zIiwKICAicHJpb3JpdHk>iOiAxLAogICJyZWNpcGllbnRfbGlzdCI6IGN0eC5yZWNpcGllbnQsCiAgInRlbXBsYXRlX2lkIjogIjU5ZGVlMzZkLWQ4NDEtNDE1ZS1hMjkzLTAwYzE2NjljMDY1NSIsCiAgInRlbXBsYXRlX3BhcmFtZXRlcnMiOiB7CiAgICAiY29kZSI6IGN0eC50ZW1wbGF0ZV9kYXRhLlZlcmlmaWNhdGlvbkNvZGUKICB9Cn0=
  smtp:
    connection_uri: <smtp://smtp:1025>
  sms:
    request_config:
      url: https://<my-sms-sending-service>/send
      method: POST
      body: <base64://ZnVuY3Rpb24oY3R4KSB7CiAgImNoYW5uZWwiOiAic21zIiwKICAicHJpb3JpdHk>iOiAxLAogICJyZWNpcGllbnRfbGlzdCI6IGN0eC5yZWNpcGllbnQsCiAgInRlbXBsYXRlX2lkIjogIjU5ZGVlMzZkLWQ4NDEtNDE1ZS1hMjkzLTAwYzE2NjljMDY1NSIsCiAgInRlbXBsYXRlX3BhcmFtZXRlcnMiOiB7CiAgICAiY29kZSI6IGN0eC50ZW1wbGF0ZV9kYXRhLlZlcmlmaWNhdGlvbkNvZGUKICB9Cn0=
      headers:
        Content-Type: application/json
      auth:
        type: api_key
        config:
          name: Authorization
          value: Basic <my-token>
          in: header
        # type: basic_auth
        # config:
        #   user: <my-client-id>
        #   password: <my-client-secret>
But when I try to send this registration request:
Copy code
curl --location '<http://localhost:4455/.ory/kratos/public/self-service/registration?flow=4aafa73b-7163-40d5-98af-6fdf8ea94645>' --header 'Content-Type: application/json' --header 'Accept: application/json' --data-raw '{"method": "code","traits": {"phone": "+201234567890"}}'
I get the following response:
Copy code
{
  "error": {
    "code": 500,
    "status": "Internal Server Error",
    "reason": "Expected sms template but got <nil>",
    "message": "An internal server error occurred, please contact the system administrator"
  }
}
What do you think?
@magnificent-energy-493 @steep-lamp-91158 can you please help me fix this issue?
m
You may have forgotten to set
registration_code
in
courier/templates
c
Thanks @most-train-86113 for your suggestion.
registration_code
is not a valid field in my
courier/templates
config. If it's valid in your config, which Kratos version are you using? I'm currently using
oryd/kratos:v1.2.0
.
c
Copy code
courier:
  smtp:
    connection_uri: <smtps://test:test@mailslurper:1025/?skip_ssl_verify=true>
  channels:
    - id: sms
      type: http
      request_config:
        url: <https://url_to_sms/sms/send>
        method: "POST"
        headers: {}
        body: <base64://ZnVuY3Rpb24oY3R4KSB7CiAgVG86IGN0eC5yZWNpcGllbnQsCiAgQm9keTogY3R>4LmJvZHksCn0=
  templates:
    verification_code:
      valid:
        email:
          body:
            plaintext: "<base64://WW91ciB2ZXJpZmljYXRpb24gY29kZSBpczoge3sgLlZlcmlmaWNhdGlvbkNvZGU>gfX0="
        sms:
          body:
            plaintext: "<base64://WW91ciB2ZXJpZmljYXRpb24gY29kZSBpczoge3sgLlZlcmlmaWNhdGlvbkNvZGU>gfX0="
    login_code:
      valid:
        email:
          body:
            plaintext: "<base64://WW91ciBsb2dpbiBjb2RlIGlzOiB7eyAuTG9naW5Db2RlIH19>"
        sms:
          body:
            plaintext: "<base64://WW91ciBsb2dpbiBjb2RlIGlzOiB7eyAuTG9naW5Db2RlIH19>"
try this
courier
config @clean-piano-39737, this worked for me. (please modify base64 content according your needs)
c
Thanks @colossal-airline-19852 I resorted to handling OTP generation/verification and SMS sending outside Ory, and then logging in identities to Ory with generated passwords I'll definitely try your solution when I have time though.