dongocanh96

Untitled

Apr 14th, 2021
562
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.74 KB | None | 0 0
  1. def login():
  2.     auth = request.authorization
  3.  
  4.     if not auth or not auth.username or not auth.password:
  5.         return make_response("Could not verify", 401, {"WWW-Authenticate" : "Basic realm='Login required!'"})
  6.  
  7.     user = Account.query.filter_by(name=auth.username).first()
  8.  
  9.     if not user:
  10.         return make_response("Could not verify", 401, {"WWW-Authenticate" : "Basic realm='Login required!'"})
  11.  
  12.     if user.check_password(auth.password):
  13.         token = jwt.encode({"id" : user.id, "exp" : datetime.utcnow() + timedelta(minutes=30)}, app.config["SECRET_KEY"])
  14.  
  15.         return jsonify({"token" : token.decode("UTF-8")})
  16.  
  17.     return make_response("Could not verify", 401, {"WWW-Authenticate" : "Basic realm='Login required!'"})
Add Comment
Please, Sign In to add comment