Esempio n. 1
0
 def authenticate(self, username, password):
     """
     用户登录,登录成功返回token,写将登录时间写入数据库;登录失败返回失败原因
     :param password:
     :return: json
     """
     userInfo = UsersModel.query.filter_by(username=username).first()
     if (userInfo is None):
         return pretty_result(code.OK, data='', msg='找不到用户')
     else:
         if (UsersModel.check_password(UsersModel, userInfo.password,
                                       password)):
             login_time = int(time.time())
             userInfo.login_time = login_time
             UsersModel.update(UsersModel)
             token = self.encode_auth_token(userInfo.id, login_time)
             return pretty_result(code.OK, data=token.decode(), msg='登录成功')
         else:
             return pretty_result(code.OK, data='', msg='密码不正确')
Esempio n. 2
0
 def post(self, username, password):
     """
     用户登录创建token
     """
     userInfo = UsersModel.query.filter_by(username=username).first()
     if (userInfo is None):
         return pretty_result(code.ERROR, data='', msg='找不到用户')
     else:
         if (UsersModel.check_password(UsersModel, userInfo.password,
                                       password)):
             login_time = int(time.time())
             userInfo.login_time = login_time
             UsersModel.update(UsersModel)
             user_id = userInfo.id
             token, refresh_token = self._generate_tokens(user_id)
             return pretty_result(code.OK,
                                  data={
                                      'access_token': token,
                                      'refresh_token': refresh_token
                                  },
                                  msg='登录成功')
         else:
             return pretty_result(code.ERROR, data='', msg='密码不正确')