Beispiel #1
0
 def generate_token(user):
     """ token 生成器 """
     try:  # generate the auth token
         auth_token = User.encode_auth_token(user.id)  # token编码
         data = dict(Authorization=auth_token.decode())
         msg.set_msg(code=CREATED,
                     msg="Successfully registered.",
                     data=data)
         return msg.body
     except Exception as e:
         logs.debug(e)
         msg.set_msg(code=UNAUTHORIZED,
                     msg="Some error occurred. Please try again.")
         return msg.body
 def login(data):  # 登录校验
     try:  # fetch the user data
         user = User.query.filter_by(phone=data["phone"]).first()  # 判断身份唯一性
         if user and user.check_password(data.get("password")):  # 校验用户密码
             auth_token = User.encode_auth_token(user.id)
             if auth_token:  # 校验通过
                 success = MsgBody()  # 初始化响应消息
                 data = dict(Authorization=auth_token.decode())
                 success.set_msg(
                     code=SUCCESS, msg="Successfully logged in.", data=data
                 )
                 return success.body
         else:
             msg.set_msg(code=UNAUTHORIZED, msg="phone or password does not match.")
     except Exception as e:
         logs.error(e)
         msg.set_msg(code=INTERNAL_SERVER_ERROR, msg="Try again")
     return msg.body
 def create_token(data):  # 登录校验
     try:  # fetch the user data
         user = User.query.filter_by(phone=data["username"]).first()  # 判断身份唯一性
         if user and user.check_password(data.get("password")):  # 校验用户密码
             auth_token = User.encode_auth_token(user.id)
             if auth_token:  # 校验通过
                 success = MsgBody()  # 初始化响应消息
                 success.set_msg(
                     code=SUCCESS, msg="Successfully logged in."
                 )
                 success.add_fields(access_token=auth_token.decode(), token_type="bearer")  # access_token
                 return success.body
         else:
             msg.set_msg(code=UNAUTHORIZED, msg="phone or password does not match.")
     except Exception as e:
         logs.error(e)
         msg.set_msg(code=INTERNAL_SERVER_ERROR, msg="Try again")
     return msg.body