Created
August 23, 2013 11:10
-
-
Save ratpik/6318129 to your computer and use it in GitHub Desktop.
A simple way to log-in a user via an ajax request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from annoying.decorators import ajax_request | |
from django.contrib.auth import authenticate, login | |
from django.views.decorators.http import require_http_methods | |
@ajax_request | |
@require_http_methods(["POST"]) | |
def ajax_login(request): | |
username = request.POST['username'] | |
password = request.POST['password'] | |
user = authenticate(username=username, password=password) | |
if user is not None: | |
if user.is_active: | |
login(request, user) | |
return {"status" : "true"} | |
else: | |
return {"status" : "false", "reason" : "You need to activate your account. Please check your email"} | |
else: | |
return {"status" : "false", "reason" : "Invalid username/password"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment