0

Description I'm encountering an issue with user authentication in my Vue.js application that communicates with a Laravel backend through an API. When attempting to log in, I receive a 405 Method Not Allowed error. The error message indicates that the server returned a "Method Not Allowed" response.

Steps Taken:

  1. My Vue.js app utilizes a fetch wrapper method to interact with the Laravel API.
  2. The fetch wrapper method is structured to handle authentication headers and response parsing.
  3. The specific error occurs when making a POST request to the /api/login endpoint.

Here's a snippet of my fetch wrapper method:



export const fetchWrapper = {
    get: request('GET'),
    post: request('POST'),
    put: request('PUT'),
    delete: request('DELETE')
};

function request(method) {
    return (url, body) => {
        const requestOptions = {
            method,
            headers: authHeader(url)
        };
        if (body) {
            requestOptions.headers['Content-Type'] = 'application/json';
            requestOptions.body = JSON.stringify(body);
        }
        return fetch(url, requestOptions).then(handleResponse);
    }
}

function authHeader(url) {
    // ... (authentication header implementation)
    // return auth header with jwt if user is logged in and request is to the api url
    const { token } = useAuthStore();
    const isLoggedIn = !!token;
    const isApiUrl = url.startsWith(//path_to_env->api_url);
    if (isLoggedIn && isApiUrl) {
        return { Authorization: `Bearer ${token}` };
    } else {
        return {};
    }

}

function handleResponse(response) {
    // ... (handle response implementation)
    return response.text().then(text => {
        if (!response.ok) {
            const { user, logout } = useAuthStore();
            if ([401, 403].includes(response.status) && user) {
                // auto logout if 401 Unauthorized or 403 Forbidden response returned from api
                logout();
            }

            const error = (text && text) || response.statusText;
            return Promise.reject(error);
        }

        try {
            const data = JSON.parse(text);
            return data;
        } catch (error) {
            // Handle parsing error for non-JSON response
            console.error('Error parsing JSON:', error);
            return text;
        }
    });
}

And here's where I use the fetch wrapper to perform a login:

async login(username, password) {
    try {
        const res = await fetchWrapper.post(`${url}/login`, {
            email: username,
            password: password,
        }, {
            headers: {
                Accept: 'application/json',
                'Content-Type': 'application/json',
            },
        });

        // ... (login response handling)
    } catch (error) {
        // ... (fetch error handling)
    }
}

Issue: Despite the correct headers being set, the API responds with a 405 Method Not Allowed error. I've ensured that the server allows the POST method for the /api/login endpoint.

Additional Information:

Laravel version: Laravel Installer 5.2.1 Vue.js version: @vue/cli 5.0.8 CORS configuration in Laravel:


    'paths' => ['api/*', 'sanctum/csrf-cookie'],

    'allowed_methods' => ['*'],

    'allowed_origins' => ['*'],

    'allowed_origins_patterns' => [],

    'allowed_headers' => ['*'],

    'exposed_headers' => [],

    'max_age' => 0,

    'supports_credentials' => false,

network configurations: LocalHost

  • Checked CORS Configuration: Ensure that the CORS configuration on the Laravel backend allows the POST method for the /api/login endpoint. I've verified that the allowed_methods in the CORS configuration include 'POST'.
  • Verified Endpoint Availability: Confirm that the /api/login endpoint is available and intended to handle POST requests on the Laravel backend.
  • Review of Fetch Wrapper Implementation: Double-checked the implementation of the fetch wrapper method to make sure that the POST request is being constructed correctly with the required headers.

*I was expecting *

  1. Successful Authentication: I expected that making a POST request to the /api/login endpoint with valid credentials would result in successful authentication, returning a token that could be used for subsequent requests.

  2. No Method Not Allowed Error: I anticipated that there would be no "Method Not Allowed" error since the CORS configuration and route definition should allow POST requests.

6
  • go to your network tab from the browser and check what the method your request is using Commented Feb 4, 2024 at 7:35
  • The error means the route you're trying to hit is not available for the requested method. Would be good to see the api.php snippet from Laravel. Commented Feb 4, 2024 at 9:26
  • ok i will check that Commented Feb 4, 2024 at 15:06
  • In the network tab the method was 'POST' Commented Feb 4, 2024 at 19:04
  • Here is the code in my api.php <?php use Illuminate\Http\Request; use App\Http\Controllers\Auth\AuthController; Route::post('/login', [AuthController::class, 'login'])->name('login'); //Route::middleware('auth:sanctum')->get('/user', function (Request $request) { //return $request->user(); //}); any feedback Commented Feb 4, 2024 at 19:05

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.