This repository is an implementation of the Laravel API Breeze application / authentication starter kit frontend in Nuxt. All of the authentication boilerplate is already written for you - powered by Laravel Sanctum, allowing you to quickly begin pairing your beautiful Nuxt frontend with a powerful Laravel backend.
First, create a Nuxt compatible Laravel backend by installing Laravel API Breeze into a fresh Laravel application and installing Breeze's API scaffolding:
# Create the Laravel application...
laravel new nuxt-backend
cd nuxt-backend
# Install Breeze and dependencies...
composer require laravel/breeze --dev
php artisan breeze:install api
# Run database migrations...
php artisan migrateNext, ensure that your application's APP_URL and FRONTEND_URL environment variables are set to http://localhost:8000 and http://localhost:3000, respectively.
After defining the appropriate environment variables, you may serve the Laravel application using the serve Artisan command:
# Serve the application...
php artisan serveNext, clone this repository and install its dependencies with yarn install or npm install. Then, copy the .env.example file to .env and supply the URL of your backend:
NUXT_PUBLIC_BACKEND_URL=http://localhost:8000
Finally, run the application via npm run dev. The application will be available at http://localhost:3000:
npm run dev
Note: Currently, we recommend using
localhostduring local development of your backend and frontend to avoid CORS "Same-Origin" issues.
This Nuxt application contains a custom auth middleware, designed to abstract all authentication logic away from your pages. In addition, the middleware can be used to access the currently authenticated user:
// ExamplePage.vue
<script lang="ts" setup>
definePageMeta({
middleware: ['auth'],
});
const { user, logout } = useAuthStore();
</script>
<template>
<div>
<p>{{ user?.name }}</p>
<button @click="logout()">Sign out</button>
</div>
</template>
<style scoped></style>Note: You will need to use optional chaining (
user?.nameinstead ofuser.name) when accessing properties on the user object to account for Nuxt's initial server-side render.
Contribution is open. Create Pull-request and I'll add it to the project if it's good enough.
Laravel API Breeze Nuxt 3 is open-sourced software licensed under the MIT license.