Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement discord login #17

Merged
merged 1 commit into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
237 changes: 235 additions & 2 deletions web/poetry.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions web/project/app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"summoners/search/", search_summoners_by_name, name="search-summoners-by-name"
),
path("profile/summoner", save_summoner, name="save-summoner-info"),
path("accounts/", include("allauth.urls")),
]

if settings.DEBUG:
Expand Down
4 changes: 1 addition & 3 deletions web/project/auth/urls.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from django.urls import path
from .views import signin, signout, signup
from .views import signout

app_name = "auth"
urlpatterns = [
path("signin/", signin, name="signin"),
path("signup/", signup, name="signup"),
path("signout/", signout, name="signout"),
]
50 changes: 2 additions & 48 deletions web/project/auth/views.py
Original file line number Diff line number Diff line change
@@ -1,54 +1,8 @@
from django.contrib.auth import authenticate, login, logout
from django.shortcuts import render, redirect, reverse
from .forms import SigninForm, SignupForm


def signin(request):
_next = request.GET.get("next")

if request.user.is_authenticated:
if _next:
return redirect(_next)
else:
return redirect("home")

if request.method == "POST":
form = SigninForm(request.POST)
if form.is_valid():
username = form.cleaned_data["username"]
password = form.cleaned_data["password"]
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
if _next:
return redirect(_next)
else:
return redirect("home")
else:
form.add_error(None, "Invalid username or password")

return render(request, "auth/signin.html", {"next": _next})
from django.contrib.auth import logout
from django.shortcuts import redirect


def signout(request):
logout(request)

return redirect("home")


def signup(request):
_next = request.GET.get("next")

if request.user.is_authenticated:
if _next:
return redirect(_next)
else:
return redirect("home")

if request.method == "POST":
form = SignupForm(request.POST)
if form.is_valid():
form.save()
return redirect(reverse("auth:signin") + f"?next={_next}")

return render(request, "auth/signup.html", {"next": _next})
28 changes: 28 additions & 0 deletions web/project/project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,15 @@
"users",
"rest_framework",
"debug_toolbar",
"django.contrib.sites",
"allauth",
"allauth.account",
"allauth.socialaccount",
"allauth.socialaccount.providers.discord",
]

SITE_ID = 1

MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
Expand All @@ -67,6 +74,12 @@
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"project.middleware.user_activity_logger.UserActivityLoggerMiddleware",
"debug_toolbar.middleware.DebugToolbarMiddleware",
"allauth.account.middleware.AccountMiddleware",
]

AUTHENTICATION_BACKENDS = [
"django.contrib.auth.backends.ModelBackend",
"allauth.account.auth_backends.AuthenticationBackend",
]

ROOT_URLCONF = "project.urls"
Expand All @@ -91,6 +104,9 @@
"APP_DIRS": True,
"OPTIONS": {
"environment": "jinja2conf.environment",
"context_processors": [
"django.template.context_processors.request",
],
},
},
]
Expand Down Expand Up @@ -184,3 +200,15 @@
INTERNAL_IPS = [
"127.0.0.1",
]

SOCIALACCOUNT_PROVIDERS = {
"discord": {
"APP": {
"client_id": env("DISCORD_CLIENT_ID"),
"secret": env("DISCORD_CLIENT_SECRET"),
"key": "",
}
}
}

SOCIALACCOUNT_LOGIN_ON_GET = True
1 change: 1 addition & 0 deletions web/project/static/images/discord.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 0 additions & 11 deletions web/project/static/js/home.js
Original file line number Diff line number Diff line change
@@ -1,11 +0,0 @@
const signin = () => {
window.location.href = "/auth/signin/";
}

const signout = () => {
window.location.href = "/auth/signout/";
}

const users_profile = () => {
window.location.href = "/users/profile/";
}
32 changes: 20 additions & 12 deletions web/project/templates/base/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,15 @@
</button>
</form>
{% else %}
<button onclick="signin()"
class="inline-block text-sm px-4 py-2 leading-none border rounded text-white border-white hover:border-transparent hover:text-stone-500 hover:bg-white mt-4 lg:mt-0">
로그인하기
</button>
<a href="{{ url('discord_login') }}?next={{ request.path }}">
<button class="inline-flex items-center justify-center text-sm px-4 py-2 leading-none border rounded transition-colors text-white bg-[#5865F2] border-[#5865F2] hover:bg-white hover:text-[#5865F2] mt-4 lg:mt-0">
<svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" class="w-6 h-6 mr-2 fill-current transition-colors hover:fill-[#5865F2]">
<title>Discord</title>
<path d="M20.317 4.3698a19.7913 19.7913 0 00-4.8851-1.5152.0741.0741 0 00-.0785.0371c-.211.3753-.4447.8648-.6083 1.2495-1.8447-.2762-3.68-.2762-5.4868 0-.1636-.3933-.4058-.8742-.6177-1.2495a.077.077 0 00-.0785-.037 19.7363 19.7363 0 00-4.8852 1.515.0699.0699 0 00-.0321.0277C.5334 9.0458-.319 13.5799.0992 18.0578a.0824.0824 0 00.0312.0561c2.0528 1.5076 4.0413 2.4228 5.9929 3.0294a.0777.0777 0 00.0842-.0276c.4616-.6304.8731-1.2952 1.226-1.9942a.076.076 0 00-.0416-.1057c-.6528-.2476-1.2743-.5495-1.8722-.8923a.077.077 0 01-.0076-.1277c.1258-.0943.2517-.1923.3718-.2914a.0743.0743 0 01.0776-.0105c3.9278 1.7933 8.18 1.7933 12.0614 0a.0739.0739 0 01.0785.0095c.1202.099.246.1981.3728.2924a.077.077 0 01-.0066.1276 12.2986 12.2986 0 01-1.873.8914.0766.0766 0 00-.0407.1067c.3604.698.7719 1.3628 1.225 1.9932a.076.076 0 00.0842.0286c1.961-.6067 3.9495-1.5219 6.0023-3.0294a.077.077 0 00.0313-.0552c.5004-5.177-.8382-9.6739-3.5485-13.6604a.061.061 0 00-.0312-.0286zM8.02 15.3312c-1.1825 0-2.1569-1.0857-2.1569-2.419 0-1.3332.9555-2.4189 2.157-2.4189 1.2108 0 2.1757 1.0952 2.1568 2.419 0 1.3332-.9555 2.4189-2.1569 2.4189zm7.9748 0c-1.1825 0-2.1569-1.0857-2.1569-2.419 0-1.3332.9554-2.4189 2.1569-2.4189 1.2108 0 2.1757 1.0952 2.1568 2.419 0 1.3332-.946 2.4189-2.1568 2.4189Z"/>
</svg>
Discord로 시작하기
</button>
</a>
{% endif %}
</div>
</div>
Expand Down Expand Up @@ -102,17 +107,20 @@
location.href = "{{ url('home') }}";
}

function signin() {
location.href = "{{ url('auth:signin') }}?next={{ request.path }}";
}

function signup() {
location.href = "{{ url('auth:signup') }}?next={{ request.path }}";
}

function signout() {
location.href = "{{ url('auth:signout') }}";
}

module.exports = {
theme: {
extend: {
},
},
variants: {
fill: ['hover', 'focus'], // this line does the trick
},
plugins: [],
}
</script>
{% block js %}
{% endblock js %}
Expand Down
12 changes: 10 additions & 2 deletions web/project/templates/recommend/ai.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,17 @@
<div class="flex justify-center items-center mt-64 flex-col">
{% if not user.is_authenticated %}
<h1 class="text-4xl font-bold">안녕하세요, 방문자님</h1>
<a href="{{ url('discord_login') }}?next={{ request.path }}" class="mt-8">
<button class="inline-flex items-center justify-center text-sm px-4 py-2 leading-none border rounded transition-colors text-white bg-[#5865F2] border-[#5865F2] hover:bg-white hover:text-[#5865F2] mt-4 lg:mt-0">
<svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" class="w-6 h-6 mr-2 fill-current transition-colors hover:fill-[#5865F2]">
<title>Discord</title>
<path d="M20.317 4.3698a19.7913 19.7913 0 00-4.8851-1.5152.0741.0741 0 00-.0785.0371c-.211.3753-.4447.8648-.6083 1.2495-1.8447-.2762-3.68-.2762-5.4868 0-.1636-.3933-.4058-.8742-.6177-1.2495a.077.077 0 00-.0785-.037 19.7363 19.7363 0 00-4.8852 1.515.0699.0699 0 00-.0321.0277C.5334 9.0458-.319 13.5799.0992 18.0578a.0824.0824 0 00.0312.0561c2.0528 1.5076 4.0413 2.4228 5.9929 3.0294a.0777.0777 0 00.0842-.0276c.4616-.6304.8731-1.2952 1.226-1.9942a.076.076 0 00-.0416-.1057c-.6528-.2476-1.2743-.5495-1.8722-.8923a.077.077 0 01-.0076-.1277c.1258-.0943.2517-.1923.3718-.2914a.0743.0743 0 01.0776-.0105c3.9278 1.7933 8.18 1.7933 12.0614 0a.0739.0739 0 01.0785.0095c.1202.099.246.1981.3728.2924a.077.077 0 01-.0066.1276 12.2986 12.2986 0 01-1.873.8914.0766.0766 0 00-.0407.1067c.3604.698.7719 1.3628 1.225 1.9932a.076.076 0 00.0842.0286c1.961-.6067 3.9495-1.5219 6.0023-3.0294a.077.077 0 00.0313-.0552c.5004-5.177-.8382-9.6739-3.5485-13.6604a.061.061 0 00-.0312-.0286zM8.02 15.3312c-1.1825 0-2.1569-1.0857-2.1569-2.419 0-1.3332.9555-2.4189 2.157-2.4189 1.2108 0 2.1757 1.0952 2.1568 2.419 0 1.3332-.9555 2.4189-2.1569 2.4189zm7.9748 0c-1.1825 0-2.1569-1.0857-2.1569-2.419 0-1.3332.9554-2.4189 2.1569-2.4189 1.2108 0 2.1757 1.0952 2.1568 2.419 0 1.3332-.946 2.4189-2.1568 2.4189Z"/>
</svg>
Discord로 로그인
</button>
</a>
<p class="mt-8">
<a href="{{ url('auth:signin') }}" class="text-stone-500">로그인</a>을
하시면 AI 추천을 받아보실 수 있습니다.
로그인을 통해 AI 추천을 받아보실 수 있습니다.
</p>
{% elif not user.summoner %}
<h1 class="text-4xl font-bold">안녕하세요, {{ user.username }}님</h1>
Expand Down
1 change: 1 addition & 0 deletions web/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ markdown = "^3.5.2"
django-filter = "^24.1"
djangorestframework = "^3.14.0"
django-debug-toolbar = "^4.3.0"
django-allauth = "^0.61.1"


[build-system]
Expand Down
Loading