jaynarayan's avatar

401 error for sanctum auth ,even if succesfully loged in.

My login code is working because when I log in ,I get loged in user in response ,however when i trying to display user on dashboard component.

api.php

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

my env settings

SESSION_DRIVER=cookie
SESSION_LIFETIME=120
SESSION_DOMAIN=http://myspa.loc 
SANCTUM_STATEFUL_DOMAINS=http://myspa.loc

my bootstrap.js

window._ = require('lodash');
window.axios = require('axios');
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';

cors.php

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

    'allowed_methods' => ['*'],

    'allowed_origins' => ['*'],

    'allowed_origins_patterns' => [],

    'allowed_headers' => ['*'],

    'exposed_headers' => [],

    'max_age' => 0,

    'supports_credentials' => true,
]

in my dashboard component

<script>
    export default {
        data() {
           return {
           	user:[]
           }
        },
        mounted(){
        	axios.get('/api/user').then((res)=>{
        		this.user = res.data;
        	})
        }
    }
</script>
0 likes
2 replies
jaynarayan's avatar

Thanks for your help . your suggestions helped me. I found that there was a mistake in my login code too. I forgot to do following

axios.get('/sanctum/csrf-cookie').then(response => {
    // Login...
});

After using this code now I can can fetch loged in user data, however I am getting following warning in console

Cookie “TF9oSxYxS977w2RzEP466WNtbKVUI2YwQhTwFTEO” has been rejected because it is already expired.

Please or to participate in this conversation.