def authenticate(self, request, username=None, password=None): print('using custom authenticator') authClient = AuthClient(ApiGatewayClient()) loginResponse = authClient.login(username, password) json = loginResponse.json() status_code = loginResponse.status_code if status_code == 401 and json['code'] == 'UNVERIFIED_LOGIN': raise ValidationError(message="User not yet verified.", code=401) if status_code == 403 and json['code'] == 'UNAUTHORIZED_ORIGIN': raise ValidationError(message="Unauthorized access.", code=401) if status_code == 200: try: user = User.objects.get(username=username) return user except User.DoesNotExist: # Create a new user. user = User.objects.create_user(username=username, password=password) group = Group.objects.get(name='Seller') user.groups.add(group) return user return None
def signUpWithEmail(request): username = request.POST['username'] password = request.POST['password'] confirmPassword = request.POST['confirm-password'] authClient = AuthClient(ApiGatewayClient()) response = authClient.register("email", username) json = response.json() print(response.json()) if (response.status_code == 200): json = response.json() response = authClient.login(json['clientId'], json['clientSecret']) print(response) self = SignUpPage.objects.get(slug='sign') if (not request.user.is_authenticated): return render(request, 'sign_up/sign_up_page.html', { 'self': self, })
def continueSignup(request): if (request.POST['reg-type'] == 'phone'): user = User.objects.create_user(username=request.POST['phone-number'], email=request.POST['phone-number'], password=request.POST['password']) group = Group.objects.get(name='Seller') user.groups.add(group) new_user = authenticate( username=request.POST['phone-number'], password=request.POST['password'], ) # call api here login(request, new_user) HttpResponseRedirect('/') else: email = request.POST['email-address'] password = request.POST['password'] reenterPassword = request.POST['password-reenter'] clientId = request.POST['clientId'] clientSecret = request.POST['clientSecret'] if password != reenterPassword: return showErrorMessage(request, "password must be the same") authClient = AuthClient(ApiGatewayClient()) # login account to get token for password setup loginResponse = authClient.login(clientId, clientSecret) jsonLogin = loginResponse.json() print("code %s - jsonLogin %s", loginResponse.status_code, jsonLogin) # checker for user is not confirmed if (loginResponse.status_code == 401 and jsonLogin['code'] == 'UNVERIFIED_LOGIN'): messages.error(request, "Please activate your email first") self = SignUpPage.objects.get(slug='sign') return render( request, 'sign_up/sign_up_page.html', { 'self': self, 'username': email, 'clientId': clientId, 'clientSecret': clientSecret, 'visibility': "show active" }) # TODO: validate inpunt on client side first if validateInput(email, password, reenterPassword) # call api here if success create a user else show necessary errors authClient = AuthClient(ApiGatewayClient()) response = authClient.register("email", request.POST['email-address']) # TODO: Add button to resend verification link json = response.json() print(response.json()) if (response.status_code == 200): json = response.json() response = authClient.login(json['clientId'], json['clientSecret']) if (response.status_code == 200): json = response.json() if (json['message'] == "User is not confirmed."): return showErrorMessage(request, json['message']) # print("Status: %s - %s",response.status_code, response.json()) # response = authClient.setupPassword(json['clientId'], json['clientSecret'], request.POST['password']) # print(response.json()) # user = User.objects.create_user( # username=request.POST['email-address'], # email=request.POST['email-address'], # password=request.POST['password'] # ) # group = Group.objects.get(name='Seller') # user.groups.add(group) # new_user = authenticate( # username=request.POST['email-address'], # password=request.POST['password'], # ) # login success # ogin(request, new_user) return render(request, 'sign_up/sign_up_page_landing.html', { 'request': request, }) else: return showErrorMessage(request, json['details'])