- Improved api return content
- Check if user exists already
This commit is contained in:
parent
6cbef534de
commit
26348b1aa6
@ -16,7 +16,7 @@ def users():
|
|||||||
for i in User.query.all():
|
for i in User.query.all():
|
||||||
res.append(i.as_dict())
|
res.append(i.as_dict())
|
||||||
|
|
||||||
return jsonify(res)
|
return jsonify({"status": 200, "data": res})
|
||||||
|
|
||||||
|
|
||||||
@api.route('/login', methods=['POST'])
|
@api.route('/login', methods=['POST'])
|
||||||
@ -27,15 +27,16 @@ def login():
|
|||||||
|
|
||||||
user = User.filter_by(username=username).first()
|
user = User.filter_by(username=username).first()
|
||||||
if check_password(user.password, password):
|
if check_password(user.password, password):
|
||||||
return True # TODO Return token
|
# TODO Return token
|
||||||
|
return jsonify({"status": 200, "text": "Successfully logged in"})
|
||||||
else:
|
else:
|
||||||
return False
|
return jsonify({"status": 500, "text": "Unable to login"})
|
||||||
|
|
||||||
|
|
||||||
@api.route('/logout', methods=['GET'])
|
@api.route('/logout', methods=['GET'])
|
||||||
def logout():
|
def logout():
|
||||||
# TODO
|
# TODO
|
||||||
pass
|
return jsonify({"status": 200, "text": "Successfully logged out"})
|
||||||
|
|
||||||
|
|
||||||
@api.route('/register', methods=['POST'])
|
@api.route('/register', methods=['POST'])
|
||||||
@ -44,12 +45,16 @@ def register():
|
|||||||
username = request_data['username']
|
username = request_data['username']
|
||||||
password = request_data['password']
|
password = request_data['password']
|
||||||
|
|
||||||
user = User(
|
if User.filter_by(username=username).first() is None:
|
||||||
username=username,
|
# Username doesn't exist yet
|
||||||
password=hash_password(password),
|
user = User(
|
||||||
admin=False
|
username=username,
|
||||||
)
|
password=hash_password(password),
|
||||||
db.session.add(user)
|
admin=False
|
||||||
db.session.commit()
|
)
|
||||||
|
db.session.add(user)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
pass
|
return jsonify({"status": 200, "text": "Successfully registered user", "data": user.as_dict()})
|
||||||
|
else:
|
||||||
|
return jsonify({"status": 500, "text": "Username already exist"})
|
||||||
|
Loading…
Reference in New Issue
Block a user