예제 #1
0
    def post(self):
        parser.add_argument('name', type=str)
        parser.add_argument('last_name', type=str)
        parser.add_argument('email', type=str)
        parser.add_argument('password', type=str)
        data = parser.parse_args()

        already_exists = User.get_user_by_email(self, data['email'])
        if already_exists:
            return {'message': 'Usuário já existe'}, 400

        new_user = User()
        new_user.public_id = 100
        new_user.name = data['name']
        new_user.last_name = data['last_name']
        new_user.email = data['email']
        new_user.password = pwd_context.encrypt(data['password'])
        new_user.admin = False

        try:
            User.save(new_user)
            return {'message': 'Usuário criado'}
        except Exception as e:
            print(str(e))
            return {'message': 'Ocorreu um erro'}, 500
예제 #2
0
 def post(self):
     parser.add_argument('email', type=str)
     parser.add_argument('password', type=str)
     data = parser.parse_args()
     current_user = User.get_user_by_email(self, data['email'])
     if not current_user:
         return {'message': 'Usuário não encontrado'}
     if self.verify_password(data['password'], current_user.password):
         access_token = create_access_token(identity=data['email'])
         refresh_token = create_refresh_token(identity=data['email'])
         return {
             'message': 'Usuário logado com sucesso',
             'access_token': access_token,
             'refresh_token': refresh_token
         }
     else:
         return {'message': 'Senha inválida'}