fix everything

This commit is contained in:
Administrator 2023-10-10 00:10:08 +03:00
parent 57031ff64b
commit 71ee91340f

View File

@ -7,7 +7,6 @@ from web.models import CustomUser
class YandexAuthView(BaseView):
required_login = False
endpoint = "yandex_auth"
def get(self):
@ -19,26 +18,26 @@ class YandexAuthView(BaseView):
'code': code
})
if response.status_code != 200:
raise Exception("Cant access, json: " + str(response.json()))
print("Cant access, json: " + str(response.json()))
return '/welcome'
access_token = response.json().get('access_token')
if access_token is None:
raise Exception('no access token')
print('no access token')
return '/welcome'
info_response = get('https://login.yandex.ru/info', headers={
'Authorization': f'OAuth {access_token}'
})
if info_response.status_code != 200:
raise Exception("Cant access, json: " + str(response.json()))
print("Cant access, json: " + str(response.json()))
return '/welcome'
data = info_response.json()
yandex_id = data['id']
if self.request.user.is_authenticated:
raise Exception('Got yandex_id %s writing to db' + str(yandex_id) + " " + type(yandex_id))
print('Got yandex_id %s writing to db' + str(yandex_id) + " " + type(yandex_id))
self.request.user.yandex_id = yandex_id
self.request.user.save()
else:
raise Exception('Got yandex_id %s logging in' + str(yandex_id) + " " + type(yandex_id))
print('Got yandex_id %s logging in' + str(yandex_id) + " " + type(yandex_id))
user = CustomUser.objects.filter(yandex_id=yandex_id).first()
if user is not None:
login(self.request, user)