useStrapiToken composable is broken in 2.1.1
Version
@nuxtjs/strapi: 2.1.1 nuxt: 4.0.3
Steps to reproduce
Nuxt config
// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
strapi: {
token: 'A TOKEN',
}
});
Make a composable
export default async () => {
const token = useStrapiToken();
console.log('TOKEN', token.value)
}
Run the app
What is Expected?
Expected log to see A TOKEN
What is actually happening?
Received undefined
If reverting to @nuxtjs/strapi 2.1.0 it's working as expected.
I think the bug was introduced by this revert commit: https://github.com/nuxt-modules/strapi/commit/96ffbada3b4dd092314976526f0f2107787a8cfd
Oh My Godness, I've lost a whole night on that until read the first issue. First, thanks the module creator(s), second, thank you man to write a issue about that! I installed this 2.1.0 version and problem solved.
np @luylucas10, Im used to read source code and got an initial issue about strapi token from a previous version that needed an already fix PR to merge :)
I maybe need to find some time to contribute on this Nuxt module. Because I don't really know how active this repo community are nowadays.
it is definetly broken
exaclty this composable useStrapiToken
import { ref } from "vue";
import { useCookie, useNuxtApp, useRuntimeConfig } from "#imports";
export const useStrapiToken = () => {
const nuxt = useNuxtApp();
const config = import.meta.server ? useRuntimeConfig() : useRuntimeConfig().public;
nuxt._cookies = nuxt._cookies || {};
if (nuxt._cookies[config.strapi.cookieName]) { <<--------------
return nuxt._cookies[config.strapi.cookieName];
}
const cookie = useCookie(config.strapi.cookieName, config.strapi.cookie);
nuxt._cookies[config.strapi.cookieName] = cookie;
if (!cookie.value && config.strapi.token) {
return ref(config.strapi.token);
}
console.info("Setting Strapi token cookie from stored cookie");
return cookie;
};
if (nuxt._cookies[config.strapi.cookieName]) { this code evaluates as TRUE because it returns some object (i was not able to debug what was that...
i fixed that temporarily with this
import { ref } from "vue";
import { useCookie, useNuxtApp, useRuntimeConfig } from "#imports";
export const useStrapiToken = () => {
const nuxt = useNuxtApp();
const config = import.meta.server ? useRuntimeConfig() : useRuntimeConfig().public;
return ref(config.strapi.token);
};