# Custom Controller/Email Overrides

## Custom Controller Overrides

The built-in controllers can be overridden with your own custom controllers.

For example, the default behavior of the [`validate_token`](https://github.com/lynndylanhurley/devise_token_auth/blob/8a33d25deaedb4809b219e557e82ec7ec61bf940/app/controllers/devise_token_auth/token_validations_controller.rb#L6) method of the [`TokenValidationController`](https://github.com/lynndylanhurley/devise_token_auth/blob/8a33d25deaedb4809b219e557e82ec7ec61bf940/app/controllers/devise_token_auth/token_validations_controller.rb) is to return the `User` object as json (sans password and token data). The following example shows how to override the `validate_token` action to include a model method as well.

#### Example: controller overrides

```ruby
# config/routes.rb
Rails.application.routes.draw do
  ...
  mount_devise_token_auth_for 'User', at: 'auth', controllers: {
    token_validations:  'overrides/token_validations'
  }
end

# app/controllers/overrides/token_validations_controller.rb
module Overrides
  class TokenValidationsController < DeviseTokenAuth::TokenValidationsController

    def validate_token
      # @resource will have been set by set_user_by_token concern
      if @resource
        render json: {
          data: @resource.as_json(methods: :calculate_operating_thetan)
        }
      else
        render json: {
          success: false,
          errors: ["Invalid login credentials"]
        }, status: 401
      end
    end
  end
end
```

## Overriding rendering methods

To customize json rendering, implement the following protected controller methods, for success methods, assume that the @resource object is available:

### Registrations Controller

* render\_create\_error\_missing\_confirm\_success\_url
* render\_create\_error\_redirect\_url\_not\_allowed
* render\_create\_success
* render\_create\_error
* render\_update\_success
* render\_update\_error
* render\_update\_error\_user\_not\_found
* render\_destroy\_success
* render\_destroy\_error

### Sessions Controller

* render\_new\_error
* render\_create\_success
* render\_create\_error\_not\_confirmed
* render\_create\_error\_bad\_credentials
* render\_destroy\_success
* render\_destroy\_error

### Passwords Controller

* render\_create\_error\_missing\_email
* render\_create\_error\_missing\_redirect\_url
* render\_create\_error\_not\_allowed\_redirect\_url
* render\_create\_success
* render\_create\_error
* render\_update\_error\_unauthorized
* render\_update\_error\_password\_not\_required
* render\_update\_error\_missing\_password
* render\_update\_success
* render\_update\_error

### Token Validations Controller

* render\_validate\_token\_success
* render\_validate\_token\_error

### Confirmations Controller

* render\_create\_error\_missing\_email
* render\_create\_success
* render\_not\_found\_error

#### Example: all :controller options with default settings:

```ruby
mount_devise_token_auth_for 'User', at: 'auth', controllers: {
  confirmations:      'devise_token_auth/confirmations',
  passwords:          'devise_token_auth/passwords',
  omniauth_callbacks: 'devise_token_auth/omniauth_callbacks',
  registrations:      'devise_token_auth/registrations',
  sessions:           'devise_token_auth/sessions',
  token_validations:  'devise_token_auth/token_validations'
}
```

**Note:** Controller overrides must implement the expected actions of the controllers that they replace.

## Passing blocks to Controllers

It may be that you simply want to *add* behavior to existing controllers without having to re-implement their behavior completely. In this case, you can do so by creating a new controller that inherits from any of DeviseTokenAuth's controllers, overriding whichever methods you'd like to add behavior to by passing a block to `super`:

```ruby
class Custom::RegistrationsController < DeviseTokenAuth::RegistrationsController

  def create
    super do |resource|
      resource.do_something(extra)
    end
  end

end
```

Your block will be performed just before the controller would usually render a successful response.

## Email Template Overrides

You will probably want to override the default email templates for email sign-up and password-reset confirmation. Run the following command to copy the email templates into your app:

```bash
rails generate devise_token_auth:install_views
```

This will create two new files:

* `app/views/devise/mailer/reset_password_instructions.html.erb`
* `app/views/devise/mailer/confirmation_instructions.html.erb`

These files may be edited to suit your taste. You can customize the e-mail subjects like [this](https://devise-token-auth.gitbook.io/devise-token-auth/config/devise).

**Note:** if you choose to modify these templates, do not modify the `link_to` blocks unless you absolutely know what you are doing.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://devise-token-auth.gitbook.io/devise-token-auth/usage/overrides.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
