Djoser use a different domain in emails - django

I have a VueJS/Django rest framework application and working on the confirmation email when a user signup.
My frontend is on another URL than my backend so I try to configure djoser to put the activation link with the good domain.
I finally managed to do it kind of adding DOMAIN and SITE_NAME informations but the result is not as expected because my domain name is surrounded by brackets.
In my settings Django I have:
DOMAIN = 'localhost:8080',
SITE_NAME = 'Frontend',
DJOSER = {
'PASSWORD_RESET_CONFIRM_URL': '#/password/reset/confirm/{uid}/{token}',
'USERNAME_RESET_CONFIRM_URL': '#/username/reset/confirm/{uid}/{token}',
'ACTIVATION_URL': 'activate/{uid}/{token}',
'SEND_ACTIVATION_EMAIL': True,
'SERIALIZERS': {},
}
But the result in the email is:
You're receiving this email because you need to finish activation process on ('Frontend',).
Please go to the following page to activate account:
http://('localhost:8080',)/activate/MzE/an7e2w-73af66245317921904307cc266f4983e
Thanks for using our site!
The ('Frontend',) team
Does anyone have an idea why these brackets pop here?

Instead of:
DOMAIN = 'localhost:8080',
SITE_NAME = 'Frontend',
try without comma.
DOMAIN = 'localhost:8080'
SITE_NAME = 'Frontend'
A comma form a tuple.

Related

Customized Password Reset URL for Django Rest API Password Reset

I am new to Django and have implemented a password reset endpoint for my rest API using the django-rest-passwordreset library. It works fantastic, but I want to customize the url returned in my email to include "https://" and domain name before the path and token so that the full URL is shown in the email.
As of now, the email send me a path like this:
/password-reset/?token=random_string_of_characters_are_here
I'd like it to email me something like this:
https://mywebsite.com/password-reset/?token=random_string_of_characters_are_here
what i do is add the url in context :
from django.conf import settings
context.update({"FRONT_URL": settings.FRONT_URL})
content = render_to_string("emails/reset.html".format(template), context)
I populate FRONT_URL in settings.py and i can use {{ FRONT_URL }} in my template.
I use context.update so i can just add FRONT_URL to other context values

Django social allauth with JWT token

I made registration via social networks using the allauth library.
Added the necessary settings:
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_AUTHENTICATION_METHOD = "email".
and applications:
"allauth", #registration
"allauth.account", # registration
"allauth.socialaccount", # registration
"allauth.socialaccount.providers.vk", # registration via VK.
and in urls.py I also wrote:
url(r"^accounts/", include("allauth.urls"))
The problem is that sometimes the provider after registration may not provide an email.
And my users are identified using mail.
Signup:
I want the user to be redirected to a page with an email entry and send a confirmation link to this email after the suppliers confirm the entered data is correct.
Signin:
I want to return the JWT token to the user after confirming the providers that the entered data is correct
How to implement a signin/signup system in Django DRF JWT with allauth library?
(apparently I need to write custom views, but I tried - unsuccessfully)

Django All_auth/rest_auth e-mail address validation using HTTP GET request

I use Django all_auth and rest_auth for a backend service of a mobile app.
I integrated the registration and login API and all works fine.
Now I have to integrate the e-mail address validation logic.
After the registration (without social), I have to send an e-mail with the link that the user will use to validate your account.
I added this configurations into my Django settings:
ACCOUNT_EMAIL_VERIFICATION = 'mandatory'
SOCIALACCOUNT_EMAIL_VERIFICATION = 'none'
Also this works fine. I'm able to receive the e-mail after the registration of a new account. In the received e-mail I have the link to validate the account also.
I would like to have the validation of the e-mail when the user simply will click on the link.
So, I would like to use only the GET HTTP method.
I added, as suggested into the documentation, this setting also:
ACCOUNT_CONFIRM_EMAIL_ON_GET = True
I use this url linked to the all_auth views.
from allauth.account.views import ConfirmEmailView
url(r'^account-confirm-email/', ConfirmEmailView.as_view(), name='account_email_verification_sent'),
url(r'^account-confirm-email/(?P<key>[-:\w]+)/$', ConfirmEmailView.as_view(), name='account_confirm_email'),
But, if I try to click on the link from the received mail, I obtain this error:
KeyError at /account-confirm-email/NzU:1hjl8A:z5Riy8Bjv_h0zJQtoYKuTkKvRLk/
'key'
/allauth/account/views.py in get
self.object = self.get_object() ...
▶ Local vars
/allauth/account/views.py in get_object
key = self.kwargs['key'] ...
▶ Local vars
This seams that setting is not sufficient to have the possibility to use the e-mail validation with GET method.
Have I to overwrite the custom Django view for this?
Looks like you're using the same view two times where you should use another view class. Following change should fix it:
from allauth.account.views import ConfirmEmailView, EmailVerificationSentView
# ...
url(
r'^account-confirm-email/',
EmailVerificationSentView.as_view(), # This is changed
name='account_email_verification_sent',
),
url(
r'^account-confirm-email/(?P<key>[-:\w]+)/$',
ConfirmEmailView.as_view(),
name='account_confirm_email',
),
# ...

python social auth: get email from Google OAuth2

I configured the login with google in a Django project. I'm able to get the name and last name but the user is saved with an empty email. I configured the scope in this way:
SOCIAL_AUTH_GOOGLE_OAUTH2_SCOPE = [
'profile',
'email'
]
But the email continues saving as "". Am I writing the scopes in bad way?
Try this
SOCIAL_AUTH_GOOGLE_OAUTH2_SCOPE = [
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/userinfo.profile'
]
it will collect user email and profile name store it into USER model.

How to skip user sign up form presented by django-allauth when signing up through social sites?

Recently, I ran into something unusual. When i tried to sign up on my django site through Linked In it logged me in right away and redirected me to whatever the login redirect URL i had set. But now when I finnaly deployed my site on AWS, django-allauth takes me to this user sign up page asking for username before sign up. I would like to skip this and use the email instead of "username". Here are my django all auth specific settings from settings.py:
ACCOUNT_AUTHENTICATION_METHOD = "email"
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_EMAIL_VERIFICATION = "none"
ACCOUNT_LOGOUT_REDIRECT_URL = "/user/"
ACCOUNT_SIGNUP_PASSWORD_VERIFICATION = True
ACCOUNT_UNIQUE_EMAIL = True
Moreover, I have also tried setting SOCIALACCOUNT_AUTO_SIGNUP = True .
Yes, it seems that I overlooked their documentation or perhaps they recently updated it, the problem was i was using "sign up flow" to sign in an already registered user and thus allauth used to present this signup-form. You can avoid this by writing:
login with facebook
and your sign up link will look like:
sign up with facebook
and to connect multiple accounts to same email you'll need to define an adapter like this:
class SocialLoginAdapter(DefaultSocialAccountAdapter):
def pre_social_login(self, request, sociallogin):
# This is tested and should work
try:
user = User.objects.get(email=sociallogin.account.user.email)
sociallogin.connect(request, user)
# Create a response object
response = HttpResponse()
raise ImmediateHttpResponse(response)
except Exception:
pass
and do mention this in your settings.py
SOCIALACCOUNT_ADAPTER = 'path_to_your_adapter_module.SocialLoginAdapter'
And this is how my final settings.py looked like (allauth part)
ACCOUNT_AUTHENTICATION_METHOD = "email"
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_EMAIL_VERIFICATION = "none" #later change to mandatory
ACCOUNT_SIGNUP_PASSWORD_VERIFICATION = True
ACCOUNT_UNIQUE_EMAIL = True
ACCOUNT_USERNAME_REQUIRED = False
SOCIALACCOUNT_EMAIL_REQUIRED = True
SOCIALACCOUNT_AUTO_SIGNUP = True
# custom adapter to override login behavior and associate different social profiles with same email,with same user
SOCIALACCOUNT_ADAPTER = 'path_to_your_adapter_module.SocialLoginAdapter'