Automatically register users

This commit is contained in:
Yohann D'ANELLO 2020-11-16 04:00:23 +01:00
parent 358f8d446a
commit fc18f93ff8
5 changed files with 48 additions and 8 deletions

View File

@ -1,12 +1,34 @@
from django.contrib.auth.models import User
from django.contrib.auth.models import AbstractUser
from django.db import models
from django.utils.translation import gettext_lazy as _
from polymorphic.models import PolymorphicModel
class DiscordUser(AbstractUser):
discord_id = models.CharField(
max_length=32,
primary_key=True,
verbose_name=_("discord id"),
)
discriminator = models.CharField(
max_length=4,
verbose_name=_("discriminator"),
)
avatar_id = models.CharField(
max_length=32,
verbose_name=_("avatar id"),
)
@property
def avatar_url(self):
return f"https://cdn.discordapp.com/avatars/{self.discord_id}/{self.avatar_id}.png"
class Player(PolymorphicModel):
user = models.OneToOneField(
User,
DiscordUser,
on_delete=models.CASCADE,
verbose_name=_("user"),
)

View File

@ -2,9 +2,12 @@ from authlib.integrations.base_client import OAuthError
from authlib.integrations.django_client import OAuth
from authlib.oauth2.rfc6749 import OAuth2Token
from django.conf import settings
from django.contrib.auth import login
from django.shortcuts import redirect
from django.utils.deprecation import MiddlewareMixin
from lg.models import DiscordUser
class OAuthMiddleware(MiddlewareMixin):
@ -37,6 +40,7 @@ class OAuthMiddleware(MiddlewareMixin):
if request.session.get('token', None) is not None:
current_user = self.get_current_user(sso_client, request)
login(request, current_user)
if current_user is not None:
return self.get_response(request)
@ -54,13 +58,21 @@ class OAuthMiddleware(MiddlewareMixin):
return None
if not OAuth2Token.from_dict(token).is_expired() and 'user' in request.session:
return request.session['user']
return DiscordUser.objects.get(discord_id=request.session['user'])
try:
res = sso_client.get(settings.OAUTH_CLIENT['userinfo_endpoint'], token=OAuth2Token(token))
if res.ok:
request.session['user'] = res.json()
return res.json()
user_json = res.json()
discord_id = user_json['id']
user, _ = DiscordUser.objects.get_or_create(discord_id=discord_id)
user.username = user_json['username']
user.discriminator = user_json['discriminator']
user.email = user_json['email']
user.avatar_id = user_json['avatar']
user.save()
request.session['user'] = user.discord_id
return user
raise Exception(res, str(res.__dict__))
except OAuthError as e:
print(e)

View File

@ -141,8 +141,11 @@ CRISPY_TEMPLATE_PACK = 'bootstrap4'
DJANGO_TABLES2_TEMPLATE = 'django_tables2/bootstrap4.html'
AUTH_USER_MODEL = 'lg.discorduser'
# OAuth Settings
OAUTH_URL_WHITELISTS = []
OAUTH_URL_WHITELISTS = [
]
OAUTH_CLIENT_NAME = 'discord'

View File

@ -49,6 +49,11 @@
<body class="d-flex w-100 h-100 flex-column">
<main class="mb-auto">
<nav class="navbar navbar-expand-md navbar-light bg-light fixed-navbar shadow-sm">
<div class="container-fluid">
<div class="navbar-nav ml-auto">
{{ user.username }}#{{ user.discriminator }} <img src="{{ user.avatar_url }}" alt="avatar" style="width: 32px; height: 32px;" />
</div>
</div>
</nav>
{% block fullcontent %}
<div class="{% block containertype %}container{% endblock %} my-3">

View File

@ -6,6 +6,4 @@
{% block content %}
Historique de loup-garou
{{ request.session.user }}
{% endblock %}