POST http://localhost:8000/login 401 (Unauthorized) laravel 9 vue 3

Viewed 34

iam trying to make login in spa vue 3 laravel 9 but iam following a course but on old ver so i have a problem 401 error when u $this->middleware('auth')and no login and 302 when i use $this->middleware('guest') but there is login iam sure that i did excally env ->SANCTUM_STATEFUL_DOMAINS=localhost bootstarjs axios.defaults.withCredentials = true; and config cores 'supports_credentials' => true,

same method in my course but no thing new

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;

class LoginController extends Controller
{
    

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = RouteServiceProvider::HOME;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auths')->except('logout');
    }
    protected function authenticated(Request $request, $user)
    {
        if ($request ->isXmlHttpRequest()){
            return response(null , 204);
        }
    }

 protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
            

        ],

        'api' => [
             \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
            'throttle:api',
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],
    ];
import axios from 'axios';
window.axios = axios;

window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
axios.defaults.withCredentials = true;

Auth::routes();



Route::middleware('auth')->get('/user', function (Request $request) {
    return $request->user();
});

Route::get('{any?}', function () {
    return view('welcome');
})->where('any' ,'^(?!api\/)[\/\w\.-]*');


import './bootstrap';

import { createApp } from 'vue/dist/vue.esm-bundler.js'

 import {router} from './routes'

 import Index from './index.vue'
 import verror from './bookables/share/valid.vue'
 import fatel from './bookables/share/fatel.vue'
 import storeD from './bookables/share/store'
 import { createStore } from 'vuex'

 
const store = new createStore(storeD)


  


const app =createApp({

    components:{
        "index": Index
    },
    async beforeCreate(){
        this.$store.dispatch('loadLastSearch');
        await axios.get('/sanctum/csrf-cookie');
        await axios.post('/login', {
            email : 'huel.maybell@example.org',
            password : 'password'
        });
        await axios.get('/user');
    }
   
   
})
app.use(router)
app.use(store)
app.mount('#app')
app.component('verror',verror)
app.component('fatel',fatel)
0 Answers
Related