Fakultas Ilmu Komputer UI

Commit a018ab1e authored by Syahrul Findi's avatar Syahrul Findi
Browse files

Merge branch 'passwordless-spike' into 'master'

Passwordless spike

See merge request !10
parents cdc4f246 483a8795
Pipeline #24542 passed with stage
in 1 minute and 42 seconds
from django.contrib import admin
# Register your models here.
from django.apps import AppConfig
class AccountsConfig(AppConfig):
name = 'accounts'
import sys
from accounts.models import ListUser, Token
class PasswordlessAuthenticationBackend(object):
def authenticate(self, uid):
print('uid', uid, file=sys.stderr)
if not Token.objects.filter(uid=uid).exists():
print('no token found', file=sys.stderr)
return None
token = Token.objects.get(uid=uid)
print('got token', file=sys.stderr)
user = ListUser.objects.get(email=token.email)
print('got user', file=sys.stderr)
return user
except ListUser.DoesNotExist:
print('new user', file=sys.stderr)
return ListUser.objects.create(email=token.email)
def get_user(self, email):
return ListUser.objects.get(email=email)
# -*- coding: utf-8 -*-
# Generated by Django 1.11.24 on 2019-11-12 13:28
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
('auth', '0008_alter_user_username_max_length'),
operations = [
('password', models.CharField(max_length=128, verbose_name='password')),
('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
('email', models.EmailField(max_length=254, primary_key=True, serialize=False)),
('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.Group', verbose_name='groups')),
('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.Permission', verbose_name='user permissions')),
'abstract': False,
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('email', models.EmailField(max_length=254)),
('uid', models.CharField(max_length=255)),
from django.db import models
from django.contrib.auth.models import (
AbstractBaseUser, BaseUserManager, PermissionsMixin
class Token(models.Model):
email = models.EmailField()
uid = models.CharField(max_length=255)
class ListUserManager(BaseUserManager):
def create_user(self, email):
def create_superuser(self, email, password):
class ListUser(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(primary_key=True)
#REQUIRED_FIELDS = ['email', 'height']
objects = ListUserManager()
def is_staff(self):
return self.email == 'harry.percival@example.com'
def is_active(self):
return True
<h1>Email sent</h1>
Check your email, you'll find a message with a link that will log you into
the site.
from django.test import TestCase
# Create your tests here.
from django.conf.urls import url
from accounts import views
urlpatterns = [
url(r'^send_email$', views.send_login_email, name='send_login_email'),
url(r'^login$', views.login, name='login'),
url(r'^logout$', views.logout, name='logout'),
import uuid
import sys
from django.contrib.auth import authenticate
from django.contrib.auth import login as auth_login, logout as auth_logout
from django.shortcuts import redirect, render
from django.core.mail import send_mail
from accounts.models import Token
def send_login_email(request):
email = request.POST['email']
uid = str(uuid.uuid4())
Token.objects.create(email=email, uid=uid)
url = request.build_absolute_uri('/accounts/login?uid=%s' % uid)
'Your login link for Superlists',
'Use this link to log in:\n\n%s' % url,
return render(request, 'login_email_sent.html')
def login(request):
uid = request.GET.get('uid')
user = authenticate(uid=uid)
if user is not None:
auth_login(request, user)
return redirect('/')
def logout(request):
return redirect('/')
......@@ -43,6 +43,12 @@ INSTALLED_APPS = [
AUTH_USER_MODEL = 'accounts.ListUser'
......@@ -126,3 +132,9 @@ PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.abspath(os.path.join(BASE_DIR, '../static'))
STATIC_URL = '/static/'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'syahrul.findi@gmail.com'
......@@ -13,9 +13,11 @@ Including another URLconf
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
from django.conf.urls import url
from django.conf.urls import include, url
from latihan2.views import home_page
from accounts import urls as accounts_urls
urlpatterns = [
url(r'^$', home_page, name='home'),
url(r'^accounts/', include(accounts_urls)),
......@@ -10,6 +10,17 @@
<div class="container">
<div class="navbar">
{% if user.is_authenticated %}
<p>Logged in as {{ user.email}}</p>
<p><a id="id_logout" href="{% url 'logout' %}">Log out</a></p>
{% else %}
<form method="POST" action="{% url 'send_login_email' %}">
Enter email to log in: <input name="email" type="text" />
{% csrf_token %}
{% endif %}
<div class="row">
<div class="col-md-6 offset-md-3 jumbotron">
<div class="text-center">
......@@ -25,6 +36,11 @@
{% csrf_token %}
{% block errors %} {% if error %}
<p class="has-error">
{{ error }}
{% endif %} {% endblock %}
......@@ -6,6 +6,8 @@ from latihan2.models import Item
def home_page(request):
if request.method == 'POST':
if request.POST['item_text'] == '':
return render(request, 'home.html', {'error': "You can't have an empty list item"})
return redirect('/')
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment