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