Commit fc18f93f authored by ynerant's avatar ynerant
Browse files

Automatically register users

parent 358f8d44
Pipeline #4765 failed with stages
in 1 minute and 9 seconds
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"),
)
......
......@@ -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)
......
......@@ -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'
......
......@@ -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">
......
......@@ -6,6 +6,4 @@
{% block content %}
Historique de loup-garou
{{ request.session.user }}
{% endblock %}
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