manchenkoff/nuxt-auth-sanctum

[Question] Bearer Token

Closed this issue · 1 comments

Hi @manchenkoff how can I add a bearer token for /api/user

after logging in it will execute that endpoint then I got an Unauthenticated error from the API since I need to pass to token first in the Bearer Token before making the request.

nuxt.config.ts

sanctum: {
    baseUrl: 'http://localhost:8000',
    mode: 'cookie',
    userStateKey: 'sanctum.user.identity',
    endpoints: {
      csrf: 'http://localhost:8000/sanctum/csrf-cookie',
      login: 'http://localhost:8000/api/login',
    },
    csrf: {
      cookie: 'XSRF-TOKEN',
      header: 'X-XSRF-TOKEN'
    },
    redirect: {
      onLogin: '/dashboard',
      onLogout: '/',
    }
  },

useAuth.ts

import { FetchError } from 'ofetch';

export const useAuth = () => {
  const { login: sanctumLogin } = useSanctumAuth()
  const { $toast } = useNuxtApp()
  


  const login = async(args: { email: string, password: string}) => {
    await sanctumLogin(args)
      .catch((e: FetchError) => {
        if (e.response?.status === 401) {
          $toast.error(e.response?._data.message, { 
            // position: 'top-right',
            dismissible: true,
            pauseOnHover: true
          })
        }
        throw e
      })
  }

  return {
    login
  }
}

Error message
Screenshot 2024-08-13 at 3 14 08 PM

Screenshot 2024-08-13 at 3 14 23 PM

Postman
Screenshot 2024-08-13 at 3 15 06 PM

Hey @kusman28, according to your nuxt.config.ts you are using cookie mode instead of token one, please check the module documentation. Feel free to re-open the issue if problem persists.