nuxt-community / auth-module

Zero-boilerplate authentication support for Nuxt 2
https://auth.nuxtjs.org
MIT License
1.93k stars 924 forks source link

Works on localhost, can't login on server #1809

Closed sylica closed 1 year ago

sylica commented 1 year ago

Hi!

My problem is, that login/logout works perfectly on my localhost, but as soon as I deploy it on a server I got 301 and 405 errors, with the "The GET method is not supported for this route. Supported methods: POST" message and I cant figure it out why is that.

My nuxt.config.js:

  },
  auth: {
    strategies: {
      local: {
        user: {
          property: 'data'
        },
        token: {
          maxAge: 86400,
          global: true
        },
        endpoints: {
            login: { url: '/api/auth/login/', method: 'post' },
            logout: { url: '/api/auth/logout/', method: 'post' },
            user: { url: '/api/auth/user/', method: 'get' }
        }
      },
    }
  },
  build: {

My login method:

    async login() {
      this.errors = {};
      try {
          await this.$auth.loginWith('local', { data: this.loginForm });
          ...
      } catch (error) {
          if (error.response.status === 401) {
              this.inactive = error.response.data.message;
          }
          this.errors = error?.response?.data?.errors;
      }
    },

My Laravel api.php:

Route::group(['prefix' => 'auth'], function () {
  Route::post('login/', [AuthController::class, 'login']);
  Route::post('register', [AuthController::class, 'register']);
  Route::post('set-password', [AuthController::class, 'setPassword']);

  Route::group(['middleware' => ['auth:sanctum']], function () {
    Route::get('user/', [AuthController::class, 'user']);
    Route::post('logout/', [AuthController::class, 'logout']);
    Route::post('password-reset', [AuthController::class, 'passwordReset']);
  });
});

And i will attach my network tab from my browser (first is on localhost/working, second one is on a server/not working):

Screen Shot 2022-11-28 at 16 14 22

Screen Shot 2022-11-28 at 16 14 37

I don't know what I'm messing up but after several days of debugging I'm hopeless. I've emptied every possible caches on the backend side so I'm thinking thats not the problem. But hopefully somebody else will be much more clever than me and can tell me what's going on.

Thanks for reading it and helping! David

keyvangholami commented 1 year ago

Why did you use "api" twice in your route?

I suggest you use baseurl in your auth strategies and if you are using separate domains for the backend and frontend or using Nuxt proxy check HTTP headers.

@sylica ه

sylica commented 1 year ago

Just removed the / from the end of the auth endpoints and it's working like a charm.

@keyvangholami it has /api/api bc the laravel backend is in the root directories api folder (the first api) and it uses the api routes (the second api)

Thanks!