class LoginRCS(View):
template_name = 'userprofiles/login.html'
def get(self, request, *args, **kwargs):
return render(request, self.template_name )
def post(self, request, *args, **kwargs):
# inputBp and inputPassword come from login form
if 'inputBp' in request.POST and 'inputPassword' in request.POST:
inputBp = request.POST['inputBp']
inputPassword = request.POST['inputPassword']
# URL to get token from api user
payload = {'username': inputBp, 'password': inputPassword}
headers = {'content-type': 'application/json'}
r =
requests.post(URL_API, data=json.dumps(payload), headers=headers)
if r.status_code == requests.codes.ok:
# I have a token
data = json.loads(r.text)
token_auth = 'token ' + data['token']
payload_login = {'username': inputBp, 'password': inputPassword}
headers_login = {
'content-type': 'application/json',
'Authorization': token_auth }
r_user = requests.get(URL_CHECK_USER, data=json.dumps(payload_login), headers=headers_login)
if r_user.status_code == request.codes.ok:
# I have a user info (unsername, first_name, last_name, email)
data_user = json.loads(r_user.text)
#################################
# here need authentication
#################################
# user = authenticate(username=my_user, password=my_bp)
else:
print 'Error'
return render(request, self.template_name)