使用dj-rest-auth处理邮件验证

huangapple go评论61阅读模式
英文:

Handling mail verification using dj-rest-auth

问题

我能够收到发送到新注册的电子邮件帐户的验证邮件。我也能够验证新电子邮件,但不能直接从邮件中发送的链接进行验证。原因是确认链接应该使用 post 方法调用,如 文档 中所述。

我的 url.py 文件中的 urlpatterns 列表如下:

urlpatterns = [
...,
...,
path('registration/', RegisterView.as_view(), name='account_signup'),
re_path(r'^account-confirm-email/', VerifyEmailView.as_view(),
 name='account_email_verification_sent')
]

提供信息,我正在使用 Django 作为后端和一个前端框架。

我希望电子邮件验证能够重定向到前端的页面,然后从那里发送 post 请求。有办法实现这一点吗?如何发送自定义电子邮件?如何拥有自定义的注册和确认电子邮件视图?

英文:

I am able to get a verification email to the newly signed up email account. I also can verify the new email but not directly from the link sent in the email. The reason that is the confirmation link should be called with a post method, as mentioned in the documentation.

My urlpatterns list in url.py file has the two elements below:

urlpatterns = [
...,
...,
path('registration/', RegisterView.as_view(), name='account_signup'),
re_path(r'^account-confirm-email/', VerifyEmailView.as_view(),
 name='account_email_verification_sent')
]

For information I am using Django as backend with a frontend framework.

What I want is for the email verification to redirect to a page in the Frontend and from there send the post request. Is there a way to achieve this ? How to send custom email ? how to have a custom register and confirm email view ?

答案1

得分: 1

你可以通过覆盖get_email_confirmation_url来自定义验证链接,即电子邮件中的URL

示例:

from allauth.account.adapter import DefaultAccountAdapter

class CustomAccountAdapter(DefaultAccountAdapter):

    def get_email_confirmation_url(self, request, emailconfirmation):

        """
            将确认URL更改为适应我们正在操作的域
        """

        url = (
            "https://example.com/verify-account/"
            + emailconfirmation.key
        )
        return url

同时,在settings.py文件中添加以下行:

ACCOUNT_ADAPTER = "users.views.CustomAccountAdapter"

其中,users是在Django中实现了custom Acount adapter的应用程序的名称。有关更多详细信息,请查看文档

英文:

You can customize the verification link - the URL in the email - by overwriting the get_email_confirmation_url

Example:

from allauth.account.adapter import DefaultAccountAdapter

class CustomAccountAdapter(DefaultAccountAdapter):

    def get_email_confirmation_url(self, request, emailconfirmation):

        """
            Changing the confirmation URL to fit the domain that we are working on
        """

        url = (
            "https://example.com/verify-account/"
            + emailconfirmation.key
        )
        return url

Also add the following line the settings.py file:

ACCOUNT_ADAPTER = "users.views.CustomAccountAdapter"

the users is the name of the app in Django that has the custom Acount adapter implemented. Documentation here for more details.

huangapple
  • 本文由 发表于 2023年3月31日 23:23:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75900209.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定