kratos icon indicating copy to clipboard operation
kratos copied to clipboard

Different Email templates based on identity traits (created_at and updated_at are always the same)

Open Quasarman opened this issue 2 years ago • 4 comments

Preflight checklist

Describe the bug

As mentioned in this discussion: https://github.com/ory/kratos/discussions/2061 I hoped to have different email templates (supported with advanced email templates in >= v0.8) based on the state of the Identity. Foremost I wanted to display a "welcome message" when a user first registered and some sort of "email changed" message when the user decides to change their email address and receive the verification link again. As suggested in the discussion by @vinckr I tried the following:

{{- if eq .Identity.created_at .Identity.updated_at -}}
  <h1>Welcome</h1>
{{- else -}}
  <h1>Email Changed</h1>
{{- end -}}

I noticed that this always results in the first block being displayed even when changing the email address (multiple times). Then I had a look at the identity object and noticed no matter how often I would change the email address the created_at and updated_at fields always stayed the same.

If the issue is not reproducable I would also be happy with a suggestion how to differentiate a registration and a settings flow to have different email templates shown 😁

Reproducing the bug

  1. Spin up a kratos version >= 0.8 (I used v0.8.2-alpha.1)
  2. Configure at least the email trait as a credential password identifier (see configuration below)
  3. Provide a simple email template that has the condition I showed above
  4. Register a new Identity
  5. Check if the welcome message is displayed in the email
  6. Try to change the email
  7. The welcome message will still be displayed in the email

Expected behavior would be that the email contains the "email changed" message.

Relevant log output

No response

Relevant configuration

{
        "$id": "user.v1",
        "$schema": "http://json-schema.org/draft-07/schema#",
        "title": "User (v1)",
        "type": "object",
        "properties": {
          "traits": {
            "type": "object",
            "properties": {
              "email": {
                "type": "string",
                "format": "email",
                "title": "E-Mail",
                "minLength": 3,
                "ory.sh/kratos": {
                  "credentials": {
                    "password": {
                      "identifier": true
                    }
                  },
                  "verification": {
                    "via": "email"
                  },
                  "recovery": {
                    "via": "email"
                  }
                }
              },
              "username": {
                "type": "string",
                "title": "Username",
                "pattern": "^[0-9\\._\\-]*[a-z]{3}[a-z0-9\\._\\-]*$",
                "minLength": 3,
                "maxLength": 30,
                "ory.sh/kratos": {
                  "credentials": {
                    "password": {
                      "identifier": true
                    }
                  }
                }
              },
              "first_name": {
                "type": "string",
                "title": "First Name",
                "minLength": 2,
                "maxLength": 30
              },
              "last_name": {
                "type": "string",
                "title": "Last Name",
                "minLength": 2,
                "maxLength": 30
              },
              "birthday": {
                "type": "string",
                "title": "Birthday",
                "format": "date"
              }
            },
            "required": [
              "email",
              "username",
              "tos_accepted"
            ],
            "additionalProperties": false
          }
        }
      }

Version

v0.8.2-alpha.1

On which operating system are you observing this issue?

Windows

In which environment are you deploying?

Kubernetes with Helm

Additional Context

I only mentioned the usage for created_at and updated_at inside email templates but I think this issue goes beyond a stylistic improvement.

Quasarman avatar May 20 '22 05:05 Quasarman

Hi team! @aeneasr @vinckr any updates about this? I'm using Kratos 0.13.0 and the issue is still there, I don't see that updated_at date is being changed for a PATCH or PUT request to change the identity...

bcordobaq avatar Jul 27 '23 13:07 bcordobaq

I confirm this still the case in Kratos v 1.0.0. My use case is settings flow and settings page UI. The updated_at value is never (nomen omen) updated when modifying traits of password so it's now meaningless to display this information.

jpogorzelski avatar Dec 06 '23 15:12 jpogorzelski

still the case in Kratos v1.1.0

knaoe avatar Mar 08 '24 09:03 knaoe

Workaround: I'm using like metadata_public.profile_updated_at to achieve this.

{{- if .Identity.metadata_public.profile_updated_at -}}
  <h1>Email Changed</h1>
{{- else -}}
  <h1>Welcome</h1>
{{- end -}}

knaoe avatar Mar 08 '24 11:03 knaoe