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 method of the TokenValidationController 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

1
# config/routes.rb
2
Rails.application.routes.draw do
3
...
4
mount_devise_token_auth_for 'User', at: 'auth', controllers: {
5
token_validations: 'overrides/token_validations'
6
}
7
end
8
9
# app/controllers/overrides/token_validations_controller.rb
10
module Overrides
11
class TokenValidationsController < DeviseTokenAuth::TokenValidationsController
12
13
def validate_token
14
# @resource will have been set by set_user_by_token concern
15
if @resource
16
render json: {
17
data: @resource.as_json(methods: :calculate_operating_thetan)
18
}
19
else
20
render json: {
21
success: false,
22
errors: ["Invalid login credentials"]
23
}, status: 401
24
end
25
end
26
end
27
end
Copied!

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:

1
mount_devise_token_auth_for 'User', at: 'auth', controllers: {
2
confirmations: 'devise_token_auth/confirmations',
3
passwords: 'devise_token_auth/passwords',
4
omniauth_callbacks: 'devise_token_auth/omniauth_callbacks',
5
registrations: 'devise_token_auth/registrations',
6
sessions: 'devise_token_auth/sessions',
7
token_validations: 'devise_token_auth/token_validations'
8
}
Copied!
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:
1
class Custom::RegistrationsController < DeviseTokenAuth::RegistrationsController
2
3
def create
4
super do |resource|
5
resource.do_something(extra)
6
end
7
end
8
9
end
Copied!
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:
1
rails generate devise_token_auth:install_views
Copied!
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.
Note: if you choose to modify these templates, do not modify the link_to blocks unless you absolutely know what you are doing.
Last modified 11mo ago