Giter Site home page Giter Site logo

vue-3-spa-laravel-breeze-api's Introduction

Vue 3 SPA for Laravel Breeze API

Vue 3 SPA

Vue 3 SPA boilerplate for consume all of Laravel Breeze API default installation (sign in, create account, forgot password, reset password, get user data) with auth-guarded dashboard and all redirects.

Features

  • Sign in, crete account, forgot password, reset password and get user data functionality
  • Page guards for authenticated/guest only users with redirect to their default endpoint if status mismatches
  • Simple dashboard for authenticated users
  • Form sending with CSRF and force logout logic when sending create account, forgot password and reset password forms (prevents Laravel's RedirectIfAuthenticated issue)
  • Displays field errors and success messages
  • Automatic session expiration check when page focused after long inactivity period (with logout if session was expired)
  • Page loading animation and form sending animation
  • Adaptive main menu
  • Markup and styles styles from Amethyst Lite

Includes

  • TailwindCSS
  • Pinia
  • Axios

Screenshots

Index page Index page Sign in page Login page Form sending Form sending Form errors Form errors Dashboard Dashboard

Installation

Install Laravel Breeze (API version), do all that standart stuff with migrations and database connection. Serve it (assumed it's run on http://127.0.0.1:8000/)

Copy this project to folder of your choice

gh repo clone volkar/vue-3-spa-laravel-breeze-api

Go to folder

cd vue-3-spa-laravel-breeze-api

Install dependencies

npm install

Run it

npm run dev

It will run on http://127.0.0.1:3000 by default. Open it in browser and you good to go!

Avoid 401 error in browser's console

By default Laravel will return 401 header if guest requests guarded route (/api/user as example) which will lead to browser's console error. Which is fine, but if you want to keep console clear you should return unauthenticated status as 200 response for all guarded routes.

Modify /app/Exceptions/Handler.php and add this in function register:

$this->renderable(function (\Illuminate\Auth\AuthenticationException $e, $request) {
    if ($request->is('api/*')) {
        return response()->json([
            'status_code' => 401,
            'authenticated' => false,
            'message' => 'Unauthenticated.'
        ], 200);
    }
});

Also add two custom routes in Laravel's api.php (and delete default /api/user route if you don't need it anymore):

Route::middleware(['auth:sanctum'])->get('/check-session', function () {
    return response()->json(['authenticated' => \Illuminate\Support\Facades\Auth::check()], 200);
});
Route::middleware(['auth:sanctum'])->get('/get-user', function (Request $request) {
    return $request->user();
});

And modify two functions in auth.js to use new separated routes we just added:

async isAuthenticated() {
    // Check if user authenticated on the backend
    let userIsAuthenticated = false
    try {
        const response = await axios.get("/api/check-session")
        if (response.status === 200 && response.data.authenticated) {
            // Data valid, user authenticated
            userIsAuthenticated = true
        }
    } catch (error) {
        // Do nothing for now
    }
    return userIsAuthenticated
},
async getUserData() {
    // Get authenticated user data from backend
    let userData = false
    try {
        const response = await axios.get("/api/get-user")
        if (response.status === 200 && response.data.id) {
            // Data valid
            userData = response.data
        }
    } catch (error) {
        // Do nothing for now
    }
    return userData
},

Contact me

You always welcome to write me

vue-3-spa-laravel-breeze-api's People

Contributors

volkar avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.