예제 #1
0
    def post(self):
        data = login_parser.parse_args()

        # Finding User from the database
        if re.match(r"[^@]+@[^@]+\.[^@]+", data["loginName"]):
            current_user = User.find_by_email(data['loginName'])
        else:
            current_user = User.find_by_username(data['loginName'])

        if not current_user:
            return {
                'message': 'User {} doesn\'t exist'.format(data['loginName'])
            }, 203

        # Checking password, if correct, it makes tokens to log the User in
        if User.verify_hash(data["password"], current_user.user_password):
            access_token = create_access_token(identity=current_user.user_id)

            whitelist_token = WhiteTokenModel(jti=get_jti(access_token))
            whitelist_token.add()

            return {
                'message': 'Logged in as {}'.format(current_user.user_name),
                'access_token': access_token
            }, 202
        else:
            return {'message': 'Wrong email or password'}, 203
예제 #2
0
 def post(self):
     jti = get_raw_jwt()['jti']
     try:
         whitelist_token = WhiteTokenModel(jti=jti)
         whitelist_token.remove(jti)
         return {'message': 'Access token has been revoked'}, 200
     except Exception as err:
         return {'message': 'Something went wrong', "error": str(err)}, 500
예제 #3
0
    def post(self):
        data = registration_parser.parse_args()

        # Checking if the email is already in our database, returns message if it is. Countinues if not.
        if User.find_by_email(data['email']):
            return {
                'message':
                'User with email {} already exists'.format(data['email']),
                'emailExists':
                True
            }, 203

        if User.find_by_username(data['username']):
            return {
                'message':
                'Bruker {} finnes allerede'.format(data['username']),
                'usernameExists': True
            }, 203

        # TODO: Check username

        if not re.match(r"^[a-zA-Z0-9]*$", data["username"]):
            return {
                'message':
                'Brukernavn er ugyldig, kan kun inneholde alfanumeriske tegn',
                "usernameInvalid": True
            }, 205

        if not re.match(r"[^@]+@[^@]+\.[^@]+", data["email"]):
            return {'message': 'Eposten er ugyldig', "emailInvalid": True}, 205

        data["password"] = User.generate_hash(data["password"])

        #TODO: Improve this \/
        uid = random.randint(10000000, 99999999)
        while User.find_by_uid(uid):
            if uid >= 99999999:
                uid = 10000000
            else:
                uid += 1

        # Making a new model with the email and password provided
        new_user = User(user_id=uid,
                        user_email=data["email"],
                        user_password=data["password"],
                        user_name=data["username"],
                        user_phone=data["phone"])

        try:
            # Saving the new user to the database. the method is located in models.py
            new_user.save_to_db()

            # Making tokens so the User is logged in
            access_token = create_access_token(identity=uid)

            whitelist_token = WhiteTokenModel(jti=get_jti(access_token))
            whitelist_token.add()

            return {
                'message': 'User {} was created'.format(data['email']),
                'access_token': access_token
            }, 201
        except Exception as err:
            return {'message': 'Something went wrong', "error": str(err)}, 500