added try except in case user cancels oauth

This commit is contained in:
Danhia 2022-02-18 11:11:34 +01:00
parent 06593b26f9
commit 7b2552f0b5
1 changed files with 31 additions and 28 deletions

View File

@ -10,43 +10,46 @@ import os
oauth = OAuth() oauth = OAuth()
oauth.register( oauth.register(
name='discord', name='discord',
client_id=os.getenv('OAUTH2_DISCORD_CLIENT_ID'), client_id=os.getenv('OAUTH2_DISCORD_CLIENT_ID'),
client_secret=os.getenv('OAUTH2_DISCORD_CLIENT_SECRET'), client_secret=os.getenv('OAUTH2_DISCORD_CLIENT_SECRET'),
access_token_url='https://discord.com/api/oauth2/token', access_token_url='https://discord.com/api/oauth2/token',
authorize_url='https://discord.com/api/oauth2/authorize', authorize_url='https://discord.com/api/oauth2/authorize',
client_kwargs={'scope': 'identify'}, client_kwargs={'scope': 'identify'},
api_base_url='https://discord.com/api/' api_base_url='https://discord.com/api/'
) )
@login_required @login_required
@require_POST @require_POST
def connect(request): def connect(request):
if request.user.userprofileinfo.discord_id: if request.user.userprofileinfo.discord_id:
return bad_request(request, "Already connected") return bad_request(request, "Already connected")
site = Site.objects.get_current() site = Site.objects.get_current()
redirect_uri = reverse('accounts:connections-connect-discord-authorize') redirect_uri = reverse('accounts:connections-connect-discord-authorize')
redirect_uri = "https://" + site.domain + redirect_uri[3:] # remove language code redirect_uri = "https://" + site.domain + redirect_uri[3:] # remove language code
print(redirect_uri) print(redirect_uri)
return oauth.discord.authorize_redirect(request, redirect_uri) return oauth.discord.authorize_redirect(request, redirect_uri)
@login_required @login_required
def authorize(request): def authorize(request):
if request.user.userprofileinfo.discord_id: if request.user.userprofileinfo.discord_id:
return bad_request(request, "Already connected") return bad_request(request, "Already connected")
token = oauth.discord.authorize_access_token(request) try:
response = oauth.discord.get('users/@me', token=token) token = oauth.discord.authorize_access_token(request)
response = response.json() except:
discord_id = response['id'] bad_request(request, "Connection refused")
request.user.userprofileinfo.discord_id = discord_id response = oauth.discord.get('users/@me', token=token)
request.user.userprofileinfo.save() response = response.json()
return redirect('accounts:edit') discord_id = response['id']
request.user.userprofileinfo.discord_id = discord_id
request.user.userprofileinfo.save()
return redirect('accounts:edit')
@login_required @login_required
@require_POST @require_POST
def disconnect(request): def disconnect(request):
if not request.user.userprofileinfo.discord_id: if not request.user.userprofileinfo.discord_id:
return bad_request(request, "Already disconnected") return bad_request(request, "Already disconnected")
request.user.userprofileinfo.discord_id = None request.user.userprofileinfo.discord_id = None
request.user.userprofileinfo.save() request.user.userprofileinfo.save()
return redirect('accounts:edit') return redirect('accounts:edit')