mirror of
https://github.com/Rushilwiz/SkoolOS.git
synced 2025-04-16 02:10:19 -04:00
Finished login and registration
This commit is contained in:
parent
796622de21
commit
95f16afbfc
|
@ -135,4 +135,4 @@ STATIC_URL = '/static/'
|
||||||
|
|
||||||
CRISPY_TEMPLATE_PACK = 'bootstrap4'
|
CRISPY_TEMPLATE_PACK = 'bootstrap4'
|
||||||
|
|
||||||
LOGIN_REDIRECT_URL = '/'
|
LOGIN_REDIRECT_URL = '/'
|
||||||
|
|
|
@ -12,12 +12,16 @@ router.register(r'assignments', api_views.AssignmentViewSet)
|
||||||
router.register(r'classes', api_views.ClassesViewSet)
|
router.register(r'classes', api_views.ClassesViewSet)
|
||||||
router.register(r'files', api_views.DefFilesViewSet)
|
router.register(r'files', api_views.DefFilesViewSet)
|
||||||
|
|
||||||
|
from users import views as user_views
|
||||||
|
|
||||||
# Wire up our API using automatic URL routing.
|
# Wire up our API using automatic URL routing.
|
||||||
# Additionally, we include login URLs for the browsable API.
|
# Additionally, we include login URLs for the browsable API.
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('', include(router.urls)),
|
path('api/', include(router.urls)),
|
||||||
path('api-auth/', include('rest_framework.urls')),
|
path('api-auth/', include('rest_framework.urls')),
|
||||||
path('admin/', admin.site.urls),
|
path('admin/', admin.site.urls),
|
||||||
path('login/', auth_views.LoginView.as_view(template_name="users/login.html"), name='login')
|
path('login/', auth_views.LoginView.as_view(template_name="users/login.html"), name='login'),
|
||||||
]
|
path('register/', user_views.register, name='register'),
|
||||||
|
path('create_account/', user_views.create_account, name='create_account'),
|
||||||
|
path('callback/', user_views.callback, name='callback'),
|
||||||
|
]
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
|
from .models import Token
|
||||||
|
|
||||||
# Register your models here.
|
# Register your models here.
|
||||||
|
admin.site.register(Token)
|
||||||
|
|
19
Website/users/forms.py
Normal file
19
Website/users/forms.py
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
from django import forms
|
||||||
|
from django.contrib.auth.models import User
|
||||||
|
|
||||||
|
class UserCreationForm(forms.ModelForm):
|
||||||
|
|
||||||
|
username = forms.CharField(disabled=True)
|
||||||
|
email = forms.EmailField(disabled=True)
|
||||||
|
first_name = forms.CharField(disabled=True)
|
||||||
|
last_name = forms.CharField(disabled=True)
|
||||||
|
password = forms.PasswordInput()
|
||||||
|
confirm_password = forms.PasswordInput()
|
||||||
|
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super(UserCreationForm, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = User
|
||||||
|
fields = ['username', 'email', 'first_name', 'password', 'confirm_password']
|
26
Website/users/migrations/0001_initial.py
Normal file
26
Website/users/migrations/0001_initial.py
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
# Generated by Django 3.0.7 on 2020-06-13 08:25
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
initial = True
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='Token',
|
||||||
|
fields=[
|
||||||
|
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||||
|
('username', models.TextField()),
|
||||||
|
('email', models.TextField()),
|
||||||
|
('first_name', models.TextField()),
|
||||||
|
('last_name', models.TextField()),
|
||||||
|
('isStudent', models.BooleanField()),
|
||||||
|
('token', models.CharField(max_length=255)),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
]
|
|
@ -1,3 +1,21 @@
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
|
|
||||||
# Create your models here.
|
# Create your models here.
|
||||||
|
|
||||||
|
class Token(models.Model):
|
||||||
|
username = models.TextField()
|
||||||
|
email = models.TextField()
|
||||||
|
first_name = models.TextField()
|
||||||
|
last_name = models.TextField()
|
||||||
|
isStudent = models.BooleanField()
|
||||||
|
token = models.CharField(max_length=255)
|
||||||
|
|
||||||
|
def save(self, *args, **kwargs):
|
||||||
|
if not self.token:
|
||||||
|
self.token = uuid4()
|
||||||
|
return super(Token, self).save(*args, **kwargs)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return f"{self.username}'s Token";
|
||||||
|
|
13
Website/users/templates/users/create_password.html
Normal file
13
Website/users/templates/users/create_password.html
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
{% extends "users/base.html" %}
|
||||||
|
{% block content %}
|
||||||
|
<div class="login-page">
|
||||||
|
<div class="form">
|
||||||
|
<form class="register-form">
|
||||||
|
<input type="text" placeholder="name"/>
|
||||||
|
<input type="password" placeholder="password"/>
|
||||||
|
<input type="text" placeholder="email address"/>
|
||||||
|
<button>create</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{% endblock %}
|
|
@ -8,9 +8,9 @@
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
{{ form | crispy }}
|
{{ form | crispy }}
|
||||||
<button type="submit">login</button>
|
<button type="submit">login</button>
|
||||||
<p class="message">Not registered? <a href="#">Create an account with Ionreg</a></p>
|
<p class="message">Not registered? <a href="{% url 'register' %}">Create an account with Ion</a></p>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
|
@ -3,13 +3,13 @@
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<div class="login-page">
|
<div class="login-page">
|
||||||
<div class="form">
|
<div class="form">
|
||||||
<form class="login-form" method="POST">
|
<div class="content-section">
|
||||||
{% csrf_token %}
|
<a href="{{ authorization_url }}" title="Ion" class="border border-dark p-3 btn btn-block btn-lg mx-auto" style="background: black; color: white;">
|
||||||
{{ form | crispy }}
|
<img src="https://ion.tjhsst.edu/static/img/favicon.png" style="filter: invert(1);">
|
||||||
<button type="submit">login</button>
|
Register with Ion
|
||||||
<p class="message">Not registered? <a href="#">Create an account with Ionreg</a></p>
|
</a>
|
||||||
</form>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
|
@ -1,3 +1,80 @@
|
||||||
from django.shortcuts import render
|
import json
|
||||||
|
import requests
|
||||||
|
|
||||||
|
from django.shortcuts import render, redirect
|
||||||
|
|
||||||
|
from requests_oauthlib import OAuth2Session
|
||||||
|
from django.contrib import messages
|
||||||
|
|
||||||
|
from .models import Token
|
||||||
|
|
||||||
|
from django.contrib.auth import authenticate
|
||||||
|
from django.contrib.auth import login as auth_login
|
||||||
|
from django.contrib.auth import logout as auth_logout
|
||||||
|
from django.contrib.auth.models import User
|
||||||
|
from django.contrib.auth.decorators import login_required
|
||||||
|
|
||||||
# Create your views here.
|
# Create your views here.
|
||||||
|
# Thanks Django, what would I do without this comment
|
||||||
|
|
||||||
|
client_id = r'QeZPBSKqdvWFfBv1VYTSv9iFGz5T9pVJtNUjbEr6'
|
||||||
|
client_secret = r'0Wl3hAIGY9SvYOqTOLUiLNYa4OlCgZYdno9ZbcgCT7RGQ8x2f1l2HzZHsQ7ijC74A0mrOhhCVeZugqAmOADHIv5fHxaa7GqFNtQr11HX9ySTw3DscKsphCVi5P71mlGY'
|
||||||
|
redirect_uri = 'http://localhost:8000/callback/'
|
||||||
|
token_url = 'https://ion.tjhsst.edu/oauth/authorize/'
|
||||||
|
scope=["read"]
|
||||||
|
|
||||||
|
def register(request):
|
||||||
|
oauth = OAuth2Session(client_id=client_id, redirect_uri=redirect_uri, scope=scope)
|
||||||
|
authorization_url, state = oauth.authorization_url("https://ion.tjhsst.edu/oauth/authorize/")
|
||||||
|
|
||||||
|
return render(request,"users/register.html", {"authorization_url": authorization_url})
|
||||||
|
|
||||||
|
def callback (request):
|
||||||
|
if request.method == "GET":
|
||||||
|
code = request.GET.get('code')
|
||||||
|
state = request.GET.get("state")
|
||||||
|
# Then if we get a response from Ion with the authorization code
|
||||||
|
if code is not None and state is not None:
|
||||||
|
print ("made it")
|
||||||
|
# We send it back to fetch the acess_token
|
||||||
|
payload = {'grant_type':'authorization_code','code': code,'redirect_uri':redirect_uri,'client_id':client_id,'client_secret':client_secret, 'csrfmiddlewaretoken': state}
|
||||||
|
token = requests.post("https://ion.tjhsst.edu/oauth/token/", data=payload).json()
|
||||||
|
headers = {'Authorization': f"Bearer {token['access_token']}"}
|
||||||
|
print(token)
|
||||||
|
|
||||||
|
# And finally get the user's profile!
|
||||||
|
profile = requests.get("https://ion.tjhsst.edu/api/profile", headers=headers).json()
|
||||||
|
print(profile)
|
||||||
|
username = profile['ion_username']
|
||||||
|
email = profile['tj_email']
|
||||||
|
first_name = profile['first_name']
|
||||||
|
last_name = profile['last_name']
|
||||||
|
isStudent = profile['is_student']
|
||||||
|
|
||||||
|
if User.objects.filter(username=username).count() != 0:
|
||||||
|
messages.success(request, "This user already exists!")
|
||||||
|
return redirect('register')
|
||||||
|
else:
|
||||||
|
token = Token(username = username, email = email, first_name = first_name, last_name = last_name, isStudent = isStudent)
|
||||||
|
token.save()
|
||||||
|
tokenHash = token.token
|
||||||
|
print(f'/create_account/?token={tokenHash}')
|
||||||
|
return redirect(f'/create_account/?token={tokenHash}')
|
||||||
|
|
||||||
|
|
||||||
|
messages.warning(request, "Invalid Callback Response")
|
||||||
|
return redirect('register')
|
||||||
|
|
||||||
|
|
||||||
|
def create_account (request):
|
||||||
|
if request.method == "GET" and Token.objects.filter(token=request.GET.get('token')).count() == 1:
|
||||||
|
token = Token.objects.get(token=request.GET.get('token'))
|
||||||
|
username = token.username
|
||||||
|
email = token.email
|
||||||
|
first_name = token.first_name
|
||||||
|
last_name = token.last_name
|
||||||
|
isStudent = token.isStudent
|
||||||
|
|
||||||
|
|
||||||
|
else:
|
||||||
|
return redirect('/register/')
|
||||||
|
|
Loading…
Reference in New Issue
Block a user