Django all auth: How to override the confirmation email url
M

2

7

What I want to do is to override confirmation email url in templates/account/email/email_confirmation_message.txt.

I want to change this part

To confirm this is correct, go to {{ activate_url }}

to something like

http://localhost:8080/confirm_email/{{ key }}

However, I couldn't figure out where {{ activate_url }} comes from. I want to send the key to the endpoint made by rest-auth.

How can I rewrite the url link on email? Or if it's too complitcated, what is the easy way to verify the email on frontend?

Marine answered 9/4, 2019 at 15:13 Comment(8)
What do you mean "verify the email on frontend"? The activate_url is the url containing the confirmation token that allows allauth to verify this is associated with the correct email. What's key?Kauri
@Kauri Sorry I misunderstand something. key is what is needed to verify email. Please see the source code. I'm using Django as rest API. So I don't wanna use the default url. The part of {{ activate_url }} is keyMarine
key is also a context variable passed to the template. Just write your own template.Kauri
@Kauri email_confirmation_message.txt doesn't refer to the key. The part of {{ activate_url }} is keyMarine
Well it is in the context, so you can use it. Look at send_confirmation_mail() method in allauth/account/adapter.py. This allows you to add in your template something like "if the above link doesn't work, go to mysite.com/confirm-email and insert this key there", presenting an extra form where they can paste the key.Kauri
@Kauri Sorry I didn't notice that.. I checked views.py.. Honestly I still don't get how this adapter is used.. Thanks I think I can solve the problem now.Marine
@Kauri Can I ask you one more question? In adapter.py the template is referred as email_template = 'account/email/email_confirmation' but there is not this template. Where email_confirmation_message.txt is used?Marine
The adapter uses a clever scheme that automatically checks for multiple templates. So when you see email_confirmation, it uses email_confirmation_message.txt and email_confirmation_message.html for the body and email_confirmation_subject.txt for the subject. It automatically sends a multipart message if the .html template is present, otherwise it only sends the text message. This is done in the render_mail methodKauri
K
6

The template is rendered with a context containing user, current_site, activate_url and key (see the send_confirmation_mail() method in allauth/account/adapter.py).

So you can just override the template and use key (and probably also current_site to make an absolute URI) to create your URL in the template.

Kauri answered 9/4, 2019 at 17:18 Comment(3)
how to convert http to https?Cochise
@HemanthSP depends what you're doing. The activate_url uses the same scheme as the request that is currently processed (e.g. user registering), so it should automatically be https (if you use that for user registration). If you build the url yourself in the template, just hard-code https or http depending on whether debug is true for example.Kauri
@Cochise this you can use using allauth setting ACCOUNT_DEFAULT_HTTP_PROTOCOL = 'https'Brambling
F
4

To fix this problem u may just need to override send_email function.

from allauth.account.adapter import DefaultAccountAdapter
from django.conf import settings

class CustomAllauthAdapter(DefaultAccountAdapter):
    def send_mail(self, template_prefix, email, context):
    account_confirm_email = '/api/v1/auth/register/account-confirm-email/'
    context['activate_url'] = (
        settings.BASE_URL + account_confirm_email + context['key']
    )
    msg = self.render_mail(template_prefix, email, context)
    msg.send()
Frey answered 3/5, 2019 at 7:53 Comment(2)
Where do I place this within a file, and where on the filesystem?Vertigo
in the settings.py file, u can use this as a default email backend adapter.Frey

© 2022 - 2024 — McMap. All rights reserved.