> ## Documentation Index
> Fetch the complete documentation index at: https://dokumentation.websale.de/llms.txt
> Use this file to discover all available pages before exploring further.

# $wsOptIn - Opt-in processes

> Reference for $wsOptIn: implement token-based opt-in flows for email verification, newsletter confirmation and password reset in templates.

With the `$wsOptIn` module, you can implement token-based opt-in processes in the frontend. Typical use cases are email verification, newsletter confirmation, or password reset. In this section, you will learn how to create opt-in links and check token validity.

***

## Module overview

**Example / excerpt of** `$wsOptIn`

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{= $wsOptIn | json }}
```

**JSON output**

```json theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{
  "current": {
    "valid": true,
    "token": "..."
  },
  "createTokenUrl": "ƒ()"
}
```

Note: `ƒ()` denotes a function.

**Variables and methods overview:**

| **Variable**       | **Type** | **Description**                                                                   |
| ------------------ | -------- | --------------------------------------------------------------------------------- |
| `current`          | map      | Returns a map with info about the active token, if one was passed in the request. |
| `valid`            | bool     | Returns `true` if the passed token is valid.                                      |
| `token`            | string   | Returns the passed token as text.                                                 |
| `createTokenUrl()` | string   | Creates an opt-in URL with a secure token.                                        |

***

## Templates

The `$wsOptIn` module is typically used for account functions such as resetting a password or creating an account.

***

## Variables

### \$wsOptIn.current

Contains information about the token passed in the current URL. Is `null` if no token is present in the URL.

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{ if $wsOptIn.current }}
    // Token was passed
{{ /if }}
```

#### \$wsOptIn.current.valid

Returns `true / false` if the passed token is valid / invalid.

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{ if $wsOptIn.current.valid }}
    // Token is valid
{{ else }}
    // Token is invalid or expired
{{ /if }}
```

#### \$wsOptIn.current.token

Returns the passed token.

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
Token: {{= $wsOptIn.current.token }}
```

***

## Methods

### \$wsOptIn.createTokenUrl()

Creates an opt-in URL with a secure token. The token is generated automatically and appended to the specified URL. This URL can then be sent to the customer by email.

**Signature**\
`$wsOptIn.createTokenUrl(url, tokenName)`

**Return value**\
`string` - URL with appended token parameter.

**Parameters**

| **Name**    | **Type** | **Required** | **Description**                           |
| ----------- | -------- | ------------ | ----------------------------------------- |
| `url`       | string   | yes          | Base URL to which the token is appended.  |
| `tokenName` | string   | yes          | Name of the token (e.g. `"verifyEmail"`). |

**Example** that creates a URL with token for email verification.

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{= $wsOptIn.createTokenUrl($wsViews.viewUrl('account/verify.htm'), 'verifyEmail') }}
```

***

## Actions

No actions are available for `$wsOptIn`.

***

## Examples for data access

### Confirming the email address via opt-in link

After creating a user account in the shop, the user receives an email to confirm their email address, e.g.

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
<a href="{{= $wsOptIn.createTokenUrl($wsViews.viewUrl('account/emailVerify.htm', {}, 'absolute'), 'verifyEmail') }}">
  Confirm email address
</a>
```

The confirmation link contains a token that ensures only the recipient can perform the confirmation.

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
https://www.beispielshop.de/account/emailVerify.htm?token=abc123xyz
```

When the user clicks the link and the confirmation page (`emailVerify.htm`) is opened, it checks whether the token is valid. If so, they can complete the verification with a button click.

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{ if $wsOptIn.current }}
    {{ if $wsOptIn.current.valid }}
      Button to confirm
    {{ else }}
      This link is no longer valid.
    {{ /if }}
{{ else }}
    Invalid access to the confirmation page.
{{ /if }}
```

***

## Related links

* [Actions](/en/frontend/referenz/aktionen)
