Flask JWT Extended: CSRF double submit tokens do not match

I’ve created a NextJS / Flask application that leverages Flask_JWT_Extended for authentication. The application uses a function called set_jwt_cookies to set an access_token and refresh_token as httpOnly cookies. The csrf_token is set as non-httpOnly to allow access via Javascript.

The application successfully sets these cookies upon login and they’re visible in the browser. The problem that I’ve encountered is that attempting to refresh the access_token results in an error message “CSRF double submit tokens do not match”. Despite several approaches, I’ve been unable to resolve this issue. I’ve simplified the code below.

# authentication/routes.py
@authentication.route('/refresh', methods=['POST'])
@jwt_required(refresh=True)
def refresh():

    # Log the received CSRF token
    received_csrf_token = request.headers.get('X-CSRF-TOKEN')
    logging.info(f"Received CSRF token: {received_csrf_token}")

    # Log the expected CSRF token
    expected_csrf_token = request.cookies.get('csrf_token')
    logging.info(f"Expected CSRF token (from cookie): {expected_csrf_token}")

    if received_csrf_token != expected_csrf_token:
        logging.error("CSRF double submit tokens do not match")
        return jsonify({"msg": "CSRF double submit tokens do not match"}), 400

    # Get the identity of the user from the refresh token
    identity = get_jwt_identity()

    # Create a new access token
    access_token = create_access_token(identity=identity, expires_delta=timedelta(minutes=60))

    # Create a response object
    response = jsonify({'access_token': access_token})

    # Set the access token as a cookie
    set_access_cookies(response, access_token)

    return response
// utils/getCookie.ts
export const getCookie = (name: string): string | undefined => {
  const value = `; ${document.cookie}`;
  const parts = value.split(`; ${name}=`);
  if (parts.length === 2) return parts.pop()?.split(";").shift();
};
// refresh/page.tsx
"use client";

import { useEffect, useState } from "react";
import { getCookie } from "@/utils/getCookie";

const HomePage = () => {
  const [status, setStatus] = useState<string | null>(null);
  const [loading, setLoading] = useState<boolean>(true);

  const refreshStatus = async () => {
    try {
      const apiUrl = process.env.NEXT_PUBLIC_FLASK_API_URL;
      if (!apiUrl) {
        console.error("API URL is not defined.");
        return;
      }

      const csrfToken = getCookie("csrf_token");
      console.log("CSRF Token:", csrfToken); // Debugging CSRF token

      const response = await fetch(`${apiUrl}/refresh`, {
        method: "POST",
        credentials: "include",
        headers: {
          "Content-Type": "application/json",
          "X-CSRF-TOKEN": csrfToken || "",
        },
      });

      console.log("Response Status:", response.status); // Debugging response status
      const text = await response.text(); // Get the response as text to see what is returned
      console.log("Response Text:", text); // Debugging response text

      // Attempt to parse JSON if the content type is application/json
      try {
        const data = JSON.parse(text);
        console.log("Response Data:", data); // Debugging response data
        setStatus(data.user || "");
      } catch (jsonError) {
        console.error("Failed to parse JSON", jsonError);
        setStatus(null);
      }
    } catch (error) {
      console.error("An error occurred while refreshing tokens", error);
      setStatus(null);
    } finally {
      setLoading(false);
    }
  };

  useEffect(() => {
    refreshStatus();
  }, []);

  if (loading) {
    return <div>Loading...</div>;
  }

  return (
    <div>
      <h1>Home Page</h1>
      {status ? (
        <p>User Status: {status}</p>
      ) : (
        <p>Failed to refresh user status. Please log in again.</p>
      )}
    </div>
  );
};

export default HomePage;

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật