Skip to content

Commit

Permalink
Added ACCOUNT_EMAIL_CONFIRMATION_AUTO_LOGIN feature
Browse files Browse the repository at this point in the history
Fixes #130.
  • Loading branch information
brosner committed Oct 18, 2016
1 parent 7f38b49 commit c6f6b5a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ version with these. Your code will need to be updated to continue working.
* fixed migration with language codes to dynamically set
* added password expiration
* added password stripping by default
* added `ACCOUNT_EMAIL_CONFIRMATION_AUTO_LOGIN` feature (default is `False`)

## 1.3.0

Expand Down
1 change: 1 addition & 0 deletions account/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class AccountAppConf(AppConf):
EMAIL_CONFIRMATION_REQUIRED = False
EMAIL_CONFIRMATION_EMAIL = True
EMAIL_CONFIRMATION_EXPIRE_DAYS = 3
EMAIL_CONFIRMATION_AUTO_LOGIN = False
EMAIL_CONFIRMATION_ANONYMOUS_REDIRECT_URL = "account_login"
EMAIL_CONFIRMATION_AUTHENTICATED_REDIRECT_URL = None
EMAIL_CONFIRMATION_URL = "account_confirm_email"
Expand Down
11 changes: 10 additions & 1 deletion account/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,10 @@ def post(self, *args, **kwargs):
self.object = confirmation = self.get_object()
confirmation.confirm()
self.after_confirmation(confirmation)
if settings.ACCOUNT_EMAIL_CONFIRMATION_AUTO_LOGIN:
self.user = self.login_user(confirmation.email_address.user)
else:
self.user = self.request.user
redirect_url = self.get_redirect_url()
if not redirect_url:
ctx = self.get_context_data()
Expand Down Expand Up @@ -491,7 +495,7 @@ def get_context_data(self, **kwargs):
return ctx

def get_redirect_url(self):
if self.request.user.is_authenticated():
if self.user.is_authenticated():
if not settings.ACCOUNT_EMAIL_CONFIRMATION_AUTHENTICATED_REDIRECT_URL:
return settings.ACCOUNT_LOGIN_REDIRECT_URL
return settings.ACCOUNT_EMAIL_CONFIRMATION_AUTHENTICATED_REDIRECT_URL
Expand All @@ -503,6 +507,11 @@ def after_confirmation(self, confirmation):
user.is_active = True
user.save()

def login_user(self, user):
user.backend = "django.contrib.auth.backends.ModelBackend"
auth.login(self.request, user)
return user


class ChangePasswordView(PasswordMixin, FormView):

Expand Down

0 comments on commit c6f6b5a

Please sign in to comment.