Beispiel #1
0
def create_token(data, user_type):
    if not User.check_user(user_type):
        return INVALID_USER_TYPE
    token = jwt.encode(
        {
            'data': data,
            'date': str(date.today()),
            'type': user_type
        },
        'asfjoew@23r8wjfosdfn',
        algorithm='HS256')
    token = token.decode()
    return token
Beispiel #2
0
def login(request):

    if request.method == 'POST':

        try:

            res = json.loads(request.body.decode())

            data = User.check_user(res)

            if data == USER_EXISTS:

                if "phone_number" in res:
                    data = User.get_id_from_phone_number(
                        res["phone_number"], res["type"])
                else:
                    data = User.get_id_from_email(res["email_id"], res["type"])

                token = create_token(data, res["type"])

                return HttpResponse(json.dumps({
                    "status": "success",
                    "token": token
                }),
                                    content_type='application_json')

            else:
                return HttpResponseBadRequest(json.dumps({"message": data}),
                                              content_type='application/json')

        except Exception as e:
            return HttpResponseServerError(json.dumps({'message': str(e)}),
                                           content_type='application/json')

    else:

        return HttpResponseBadRequest(json.dumps(
            {"message": "only post method"}),
                                      content_type='application/json')
Beispiel #3
0
def login(request):
    try:
        
        if request.method=='POST':
            
            user_structure = json.loads(request.body.decode())

            if "username" not in user_structure or "password" not in user_structure:
                return HttpResponseBadRequest(json.dumps({
                    "message": "invalid data format"
                }), content_type='application/json')

            res = User.check_user(user_structure["username"],
                                user_structure["password"])

            if res==True:
                token = create_token(user_structure["username"])

                return HttpResponse(json.dumps({
                    "token": token
                }), content_type='application/json')

            else:
                return HttpResponseBadRequest(json.dumps({
                    "message": "user doesnot exists"
                }), content_type='application/json')
        else:
            return HttpResponseBadRequest(json.dumps({
                "message": "only post method"
            }), content_type='application/json')
        

    except Exception as e:
        print(e)
        return HttpResponseServerError(json.dumps({
                'message': str(e)
            }), content_type='application/json')