Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customize activate_url on django-allauth?

I am using Django with Django REST framework as a backend and AngularJS on frontend.

For the user management I am using django-rest-auth which uses django-allauth for the user management. As the base I used demo from django-rest-auth.

The problem is after the sign up when you try to verify the email it sends email with activation url: 127.0.0.1:8000/account/confirm-email/yhca8kmijle0ia7k3p7ztbvnd2n1xepn9kyeuaycmlzll5xw19ubjarnvjrot7eu/

where *127.0.0.1:8000 is the Django backend.

But in my case it should send url something like localhost:9000/#/verifyEmail/akv2dcvrfnk9ex5fho9jk0xx1ggtpfazmi8sfsoi2sbscoezywfp7kzcyqnizrc0. So that this verification will be done from frontend, where localhost:9000 is my AngularJS frontend.

Is there any way of customizing activate_url on django-allauth?

like image 871
mipasov Avatar asked Jan 16 '15 13:01

mipasov


1 Answers

I have managed to make the activation from the frontend by overriding the DefaultAccountAdapter class and overriding the send_mail method as specified in the django-allauth doc :

If this does not suit your needs, you can hook up your own custom mechanism by overriding the send_mail method of the account adapter (allauth.account.adapter.DefaultAccountAdapter)

I first wrote the backend url that I use to confirm an email :

api_patterns = [
...
url(r'^verify-email/(?P<key>\w+)/$',
        confirm_email, name="account_confirm_email"),
...
]

I then specified the custom adapter and the frontend url in settings.py as follows :

ACCOUNT_ADAPTER = 'API.adapter.DefaultAccountAdapterCustom'
URL_FRONT = 'http://localhost:9000/'

And wrote this in the adapter.py from my app :

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

class DefaultAccountAdapterCustom(DefaultAccountAdapter):

    def send_mail(self, template_prefix, email, context):
        context['activate_url'] = settings.URL_FRONT + \
            'verify-email/' + context['key']
        msg = self.render_mail(template_prefix, email, context)
        msg.send()

The activation url sent in the email will now look like : http://127.0.0.1:9000/verify-email/<key>/

I changed the activate_url to the URL_FRONT string I specified in settings.py and appended the key in order to make a get request from the frontend to the url I wrote earlier (Let's say http://localhost:8000/verify-email/<key>). (Previously setting ACCOUNT_CONFIRM_EMAIL_ON_GET to True in order to confirm an email just by doing a get request)

like image 118
Storm Avatar answered Sep 24 '22 10:09

Storm