Skip to content

Instantly share code, notes, and snippets.

@royteusink
Last active December 31, 2020 13:21
Show Gist options
  • Save royteusink/62cbf3ef01216840851a9f85c0e2ec4e to your computer and use it in GitHub Desktop.
Save royteusink/62cbf3ef01216840851a9f85c0e2ec4e to your computer and use it in GitHub Desktop.
vue cli + laravel sanctum

CORS issues with local development setup Laravel Sanctum with Vue3 cli

app.yourdomain.test

vue app created with vue cli.

axios.create({
  baseURL: process.env.VUE_APP_API_URL,
  withCredentials: true
});

.env

VUE_APP_API_URL="https://api.yourdomain.test/"

package.json

{
   "serve": "vue-cli-service serve --host app.yourdomain.test --https",
}
yarn serve

api.yourdomain.test

Laravel 8 + Laravel sanctum

cd api.yourdomain
valet link
valet secure

.env

SANCTUM_STATEFUL_DOMAINS="app.yourdomain.test:8080"
SESSION_DOMAIN=".yourdomain.test"
SESSION_DRIVER=database
SESSION_LIFETIME=120

config/cors.php

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

logout route

public function logout(Request $request)
{
    Auth::guard('web')->logout();

    $request->session()->flush();
    $request->session()->invalidate();
    $request->session()->regenerateToken();

    return response()->json(['message' => 'Successfully logged out'], 200);
}

session table

php artisan session:table
php artisan migrate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment