DEV Community

Nasimul Hasan Deep
Nasimul Hasan Deep

Posted on

Laravel + Nuxt Authentication Made easy

To build a Laravel API for Nuxt.js authentication, you can follow these steps:

  1. In your Laravel project, set up a new API route group by adding the following code to your routes/api.php file:
Route::middleware('auth:api')->group(function () {
    // Your API routes go here
});
Enter fullscreen mode Exit fullscreen mode
  1. In your Nuxt.js project, install the @nuxtjs/auth module by running the command npm install @nuxtjs/auth.

  2. In your Nuxt.js project, configure the @nuxtjs/auth module by adding the following code to your nuxt.config.js file:

modules: [
    '@nuxtjs/auth',
],
auth: {
    strategies: {
        local: {
            endpoints: {
                login: { url: '/api/login', method: 'post', propertyName: 'access_token' },
                logout: { url: '/api/logout', method: 'post' },
                user: { url: '/api/user', method: 'get', propertyName: 'user' }
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode
  1. In your Laravel project, create a new controller to handle the login and logout routes. This controller should use the Auth facade to authenticate and log out users.

  2. In your Nuxt.js project, create a new page or component to handle the login and logout functionality. This page or component should use the $auth object to authenticate and log out users.

6.Test the authentication by running your Nuxt.js project and trying to login and logout.

Top comments (0)