Fakultas Ilmu Komputer UI

Commit b6f586d1 authored by Muhammad Rafif Elfazri's avatar Muhammad Rafif Elfazri
Browse files

[GREEN] Implement create_phone_number function and refactor create_google_user

parent 920d46da
Pipeline #64756 passed with stages
in 27 minutes and 9 seconds
......@@ -9,6 +9,8 @@ from django.contrib.auth.base_user import BaseUserManager
from django.contrib.auth.hashers import make_password
from django.contrib.auth.models import User
from django.conf import settings
from django.db import IntegrityError
from registrasi.models import BisaGoUser
......@@ -55,23 +57,36 @@ def request_token(request):
def _create_google_user(email, name):
user = User()
user.username = email
# provider random default password
user.password = make_password(BaseUserManager().make_random_password())
user.email = email
user.is_active = True
user.last_name = name
user.save()
random_generated_phone_number = 'x'.join([str(random.randint(0, 9)) for i in range(8)])
user = None
try:
return User.objects.get(username=email)
except User.DoesNotExist:
user = User()
user.username = email
# provider random default password
user.password = make_password(BaseUserManager().make_random_password())
user.email = email
user.is_active = True
user.last_name = name
user.save()
random_generated_phone_number = _create_random_phone_number()
BisaGoUser.objects.create(user=user, phone_number=random_generated_phone_number)
return user
def _create_random_phone_number():
phone_number = 'x'.join([str(random.randint(0, 9)) for i in range(8)])
try:
BisaGoUser.objects.get(phone_number=phone_number)
return _create_random_phone_number()
except BisaGoUser.DoesNotExist:
return phone_number
@csrf_exempt
def validate_google_token(access_token):
payload = {'access_token': access_token} # validate the token
req = requests.get('https://www.googleapis.com/oauth2/v2/userinfo', params=payload, proxies=settings.PROXIES)
req = requests.get('https://www.googleapis.com/oauth2/v2/userinfo',
params=payload, proxies=settings.PROXIES)
data = json.loads(req.text)
if 'error' in data or 'email' not in data:
content = {'message': 'wrong google token / this google token is already expired.'}
......
Supports Markdown
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