Overview

Simple Django email-auth app

This app provide email authentication and user options app.

Uses:

  • install django-email-auth app - python setup.py install
  • add 'emailuser' app to INSTALLED_APP
  • add 'emailuser.auth.EmailAuthBackend' to AUTHENTICATION_BACKENDS
  • run python manage.py syncdb

Add views for login/register. Example:

from django.contrib.auth.models import User
from emailuser.models import EmailUser, EmailUserForm, manual_create_email_user

def login_view(request):
    if request.method == "POST": 
        form = EmailUserForm(request.POST) 
        if form.is_valid(): # All validation rules pass
            email = form.cleaned_data['email']
            try:
                user = User.objects.get(email=email)
            except User.DoesNotExist:
                user = User.objects.create_user(email=form.cleaned_data["email"], 
                            username=form.cleaned_data["email"],
                        password='some unesable password') 
                        # do not forget to change this password for some unusable
                user.save()
            try:    
                token = user.emailuser.generate_token()
            except: # FIXME: we add try/except here for case if user object already exists 
                manual_create_email_user(user)
                token = user.emailuser.generate_token()
            send_mail(u"Registration", u"Login: http://example.com/auth/%s/%s/" % (user.pk, token), 
                      "from@example.com", [email], fail_silently=False)
            sended = True
    else:
        form = EmailUserForm()
    return render(request, "user/login.html", {'form':form, 'sended':sended})
Example login.html:
{% extends "base.html" %}
{% block content %}
{% if user.is_authenticated %}
  Welcome {{ user }}!
{% else %}
{% if sended %}
   We just sent you link for login to our site.
{% else %}
        <form action="" method="post">{% csrf_token %}
           {{ form.as_p }}
        <input type="submit" value="Login/Register" />
       </form>
    {% endif %}
{% endif %}
{% endblock %}

Example authentication view:

def auth(request, user_id, token):
    get_user = get_object_or_404(User, pk=user_id)
    user = authenticate(email=get_user.email, token=token)
    if user is not None:
        if user.is_active:
            login(request, user)
    return render(request, "user/auth.html", {"user": user})
Next in your code you can set:
def some_view(request):
    user = request.user
    user.emailuser.set_option('some_option', 'some_value') # set option
And get user options:
def another_view(request):
    value = user.emailuser.get_option('some_option') # get option, value = 'some_value'