0
@app.route('/login', methods=['POST'])
def login():
if request.method == 'POST':

    username = request.form['username']
    password = request.form['password']
    if not username:
        return jsonify({'message':'Missing username'}), 400

    if not password:
        return jsonify({'message': 'Missing password'}), 400
    registered_user = Users.get(
        Users.username == username)
    password_pass = check_password_hash(registered_user.password_harsh, password)
    
    if registered_user:

        if password_pass:
            access_token = create_access_token(identity=registered_user)
            return {"access_token": access_token}, 200

    return jsonify({'message':'Invalid Login Info'}), 400

return jsonify({'message':"Please provide an email and password"}), 400

i have a message that the error is supposed to return but I get an error. I have the setting to return jsonify({'message':'Invalid Login Info'}), 400 when user does not exist. Its a python flask app.

File "/Users/user/PythonProject/venv/lib/python3.8/site-packages/peewee.py", line 
6973, in get
    raise self.model.DoesNotExist('%s instance matching query does '
model.UsersDoesNotExist: <Model: Users> instance matching query does not exist:
SQL: SELECT `t1`.`id`, `t1`.`fullname`, `t1`.`username`, `t1`.`email`, 
`t1`.`password_harsh`, `t1`.`birthday`, `t1`.`gender` FROM `users` AS `t1` WHERE 
(`t1`.`username` = %s) LIMIT %s OFFSET %s
Params: ['kennyy', 1, 0]
icanbe444
  • 11
  • 3
  • maybe you should run `check_password_hash` after `if registered_user:` – furas May 11 '22 at 02:45
  • do you have this user in database? It raises error because user doesn't exists - so it raises when user is invalid. I don't know what you expected. – furas May 11 '22 at 02:46
  • Well i expect it to return jsonify({'message':'Invalid Login Info'}), 400 – icanbe444 May 11 '22 at 09:32
  • I don't have code with peewee but maybe it is natural that it raises error when it can't find object in database - so you have to use `try/except` to catch it. – furas May 11 '22 at 12:37
  • I found in peewee documentation for [SELECT](https://docs.peewee-orm.com/en/latest/peewee/querying.html#selecting-a-single-record) that it is natural that it raises error when it can't find object - so you have to use `try/except` to catch it and send json with error message – furas May 11 '22 at 12:39

1 Answers1

0

Peewee documentation for Selecting a single record shows that it raises error when it can't find object.

So you should use try/except to catch it.

Something like this:

@app.route('/login', methods=['POST'])
def login():
    if request.method == 'POST':
    
        username = request.form['username']
        password = request.form['password']
        
        if not username:
            return jsonify({'message':'Missing username'}), 400
    
        if not password:
            return jsonify({'message': 'Missing password'}), 400
        
        try:
            registered_user = Users.get(Users.username == username)
        except peewee.DoesNotExist:
            return jsonify({'message':'Invalid Login Info'}), 400
            
        password_pass = check_password_hash(registered_user.password_harsh, password)
    
        if password_pass:
            access_token = create_access_token(identity=registered_user)
            return {"access_token": access_token}, 200

    return jsonify({'message':"Please provide an email and password"}), 400
furas
  • 134,197
  • 12
  • 106
  • 148