santeri pohjaranta
santeri pohjaranta

Reputation: 29

Passing jwt tokken in VUE to api on pyhton and it provide me error 422

I try to send access_tokken from VUE template to Python api but it always provide me error 422. What could be problem? To send jwt token in vue I use this:

const response = await axios.get('http://127.0.0.1:5000/user_info', {
   headers: {
      Authorization: `JWT ${Cookies.get('access_token')}`
   }
});

Pyhton api where is send JWT token

@app.route('/user_info', methods=['GET'])
@jwt_required()
def user_info():
    current_user = get_jwt_identity()
    app.logger.info(f"Current user: {current_user}") 
    db = connection_pool.get_connection()
    cursor = db.cursor(dictionary=True)

    try:
        cursor.execute("SELECT name, surname, email FROM client_data WHERE id = %s", (current_user['id'],))
        user_info = cursor.fetchone()
        app.logger.info(f"User info: {user_info}")  # Log the user info for debugging
        return jsonify(user_info), 200
    except mysql.connector.Error as err:
        app.logger.error(f"Error fetching user info: {err}")  # Log the error for debugging
        return jsonify({"error": str(err)}), 500
    finally:
        cursor.close()
        db.close()

Part of VUE code which send JWT token

let token = Cookies.get('access_token');
if (token) {
   token = token.toString();
   console.log('Token:', token);
   try {
      const response = await 
      axios.get('http://127.0.0.1:5000/user_info', {
            headers: {
              Authorization: `Bearer ${token}`
            }
          });
          this.user = response.data;
     } catch (error) {
          console.error('Error fetching user info:', error);
  }
}

Upvotes: 0

Views: 64

Answers (1)

santeri pohjaranta
santeri pohjaranta

Reputation: 29

I found problem. I did not added expire time for token. Because of that when I tried to send it to api it did not understood what it was.

app.config['JWT_ACCESS_TOKEN_EXPIRES'] = timedelta(days=7)

Another option why it was no working because I did not added required extension get_jwt

from flask_jwt_extended import JWTManager, create_access_token, jwt_required, get_jwt_identity, get_jwt

Upvotes: 0

Related Questions