devise_token_auth icon indicating copy to clipboard operation
devise_token_auth copied to clipboard

Email Template Directory For devise_token_auth

Open SugiKent opened this issue 4 years ago • 6 comments

Version: devise_token_auth (1.1.3)

I want to use app/views/devise_token_auth/*.html.erb files as email templates if there are any files in the directory.

Because when I want to use both devise and devise_token_auth, this gem use app/views/devise/mailer/*.html.erb files as email templates. It causes a problem that confirmation link in confirmation email is always confirmation_url even if developers want to another controller like Api::V1::ConfirmationsController.

For example, devise: Registrations → app/views/devise/mailer/*.html.erbconfirmation_url in email DTA: Api::V1::Registrations → app/views/devise_token_auth/*.html.erbapi_v1_confirmation_url in email

If you have any ideas about this issue, please let me know. Thanks!

ADDITIONAL

I know that DeviseTokenAuth has config option DeviseTokenAuth.default_confirm_success_url, But I was confused when I overwritten email template in app/views/devise/mailer/*.html.erb.

SugiKent avatar Aug 15 '20 18:08 SugiKent

I found a code that sends a confirmation_instruction email.

https://github.com/lynndylanhurley/devise_token_auth/blob/387306a180e955da077a67a6b77b1feda9497202/app/models/devise_token_auth/concerns/user.rb#L57-L66

Unfortunately, Is send_devise_notification method implemented in devise? So, this is not just a issue of devise_token_auth.

SugiKent avatar Aug 16 '20 05:08 SugiKent

I tried to switch the confirmation url in the template by Class name like this.

confirmation_instructions.html.slim

= controller.controller_name == Api::V1::RegistrationsController : api_v1_user_confirmation_url : confirmation_url 

But it caused error.

[16] pry(#<#<Class:0x000055bef54980e0>>)> controller.controller_name
NoMethodError: undefined method `controller_name' for #<Devise::Mailer:0x00007f94540f2400>
from (pry):15:in `_app_views_devise_mailer_confirmation_instructions_html_slim__4327844298565262421_70137521114820'

Does anyone know how to switch the url in the template ?

SugiKent avatar Aug 16 '20 05:08 SugiKent

~Temporally I resolved by using instance variable to pass the confirmation url by each RegistrationsController, I don't know why I used such easy way such easy way :) However I suggestion that DTA has the email template directory option.~

I couldn't pass an instance variable from registrations controller to a devise email template.

SugiKent avatar Aug 16 '20 09:08 SugiKent

Not sure if you have control over when you call the send_confirmation_instructions method on the model, but if you do, you can do it like this

resource.send_confirmation_instructions(
  template_path: ['devise_token_auth/mailer'],
)

Anyway, look around here and you might have something you can override. That method receives a template_path option.

I do think it would be nice for this gem to use a different directory for email templates, that is precisely what we do on our gem to make it easier to work in parallel with standard devise mails

mcelicalderon avatar Aug 21 '20 23:08 mcelicalderon

@mcelicalderon I could use the email template in specific directory by using template_path option, thanks!

@resource.skip_confirmation_notification!
if @resource.save
  @resource.send_confirmation_instructions(
     template_path: ['devise_token_auth/mailer']
   )
end

I had to skip confirmation notification manually.

SugiKent avatar Aug 22 '20 05:08 SugiKent

I want to know how do some contributors think about this issue.

SugiKent avatar Aug 22 '20 05:08 SugiKent