Next.js Discord

Discord Forum

getting null while accessing token from cookie

Unanswered
Northeast Congo Lion posted this in #help-forum
Open in Discord
Avatar
Northeast Congo LionOP
getting null for match while accessing token from cookies.

import axios from "axios";

export const getCsrfToken = async () => {
    try{
        const response = await axios.get('https://dummy.in/demo/production/sanctum/csrf-cookie', {
            withCredentials: true,
        });
        console.log('CSRF token fetched successfully', response.config.xsrfHeaderName);
    }catch(error){
        console.error('Failded to fetch CSRF token:', error);
        throw error;
    }
}


export const getCsrfTokenFromCookies = () => {
    const match = document.cookie.match(/XSRF-TOKEN=([^;]+)/);
    console.info('match:', match);
    return match ? decodeURIComponent(match[1]) : null;
};

0 Replies