Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update documentation on endpoint paths starting with / #782

Closed
spras opened this issue Jul 1, 2024 · 8 comments · Fixed by #796
Closed

Update documentation on endpoint paths starting with / #782

spras opened this issue Jul 1, 2024 · 8 comments · Fixed by #796
Assignees
Labels
documentation A change to the documentation p4 Important Issue

Comments

@spras
Copy link

spras commented Jul 1, 2024

Environment

Reproduction

here is my nuxt.config.ts

  auth: {
    baseURL: process.env.CUSTOM_API_URL + "/",
    globalAppMiddleware: {
      isEnabled: true,
    },
    session: {
      enableRefreshPeriodically: false,
      enableRefreshOnWindowFocus: true,
    },
    provider: {
      type: "local",
      token: {
        maxAgeInSeconds: 18000,
        sameSiteAttribute: "strict",
        secureCookieAttribute: true,
      },
      endpoints: {
        getSession: { path: "/api/session", method: "get" },
      },
      pages: {
        login: "/connexion",
      },
    },
  },

Describe the bug

I'm currently testing upgrade from 0.7.2 to 0.8.0-rc

This break my config : local provider and auth.baseURL defined to a custom base url ex : https://example.com/

Login is posted to the nuxt base URL , not to my custom base url

As a workaround I had to redefine all the endpoints and removing the / prefix .

endpoints: { 
       signIn: { path: 'login', method: 'post' },        
       signOut: { path: 'logout', method: 'post' },        
       signUp: { path: 'register', method: 'post' },        
       getSession: { path: 'session', method: 'get' }    
}

Perhaps it has to become the default values ?

It seems to be linked to the modifications for this issue #742 ,
https://github.com/sidebase/nuxt-auth/pull/750/files , joinPathToApiURL function

Additional context

No response

Logs

No response

@spras spras added bug A bug that needs to be resolved pending An issue waiting for triage labels Jul 1, 2024
@phoenix-ru
Copy link
Collaborator

Hi @spras, it looks correct to me that you redefine your endpoints because of the custom base url.
Using / prefix for endpoints always means to use Nuxt's internal routing - this is intended.

This behaviour was mostly inspired by how URL's are resolved in the browser. For example, assuming you are on https://example.com/subpage and you have <a href="/another">, you will be navigated to https://example.com/another.
I wanted to replicate it, but in the context of Nuxt. Our module assumes that URLs starting with / are local to Nuxt, therefore it avoids extra overhead of crossing the domain boundary.

The fact that using { path: 'login' } satisfies your usecase should be considered a bug rather than a desired result. Similar to browser navigation analogy, I would expect the navigation to be same-level (e.g. { path: 'three' } switching from /one/two to /one/three). But since I don't foresee anyone ever wanting same-level navigation, it is how it is.

I would suggest you to use fully-specified URLs in path configs, because your usecase (I assume) involves going away from Nuxt to an external custom URL.

@Ray0907
Copy link

Ray0907 commented Jul 5, 2024

I faced the similar issue. Here is my nuxt.config file. Ensure that the origin ends with a ‘/’. This worked for me.

auth: {
		provider: {
			type: 'authjs',
		},
		origin: '/prefix/',
	},

@spras
Copy link
Author

spras commented Jul 5, 2024

@phoenix-ru your explaination make sense , it'OK for me. I think documentation should be more detailed and explicit on it.

@phoenix-ru
Copy link
Collaborator

Thank you for the feedback @spras, we will adjust the documentation accordingly

@phoenix-ru phoenix-ru changed the title Local provider, baseURL to a custom base URL breaks in 0.8.0-rc Update documentation on endpoint paths starting with / Jul 5, 2024
@phoenix-ru phoenix-ru added p4 Important Issue documentation A change to the documentation and removed bug A bug that needs to be resolved pending An issue waiting for triage labels Jul 5, 2024
@coreyshuman
Copy link

I've been struggling with this as well when using an external auth server on @sidebase/[email protected]. Here are some of the configurations I've tried and their unexpected outcomes:

Example 1 - Incorrect URL

auth: {
  baseURL: 'http://base-url',
  provider: {
    endpoints: {
      signIn: {path: 'http://api-url/login'},
    },
}

The sign in path becomes: http://base-url/api/auth/http://api-url/login

Example 2 - Incorrect URL

Add the ending slash on baseURL.

auth: {
  baseURL: 'http://base-url/',
  provider: {
    endpoints: {
      signIn: {path: 'http://api-url/login'},
    },
}

The sign in path becomes: http://base-url/http://api-url/login

Example 3 - Incorrect URL

Delete baseURL.

auth: {
  baseURL: '',
  provider: {
    endpoints: {
      signIn: {path: 'http://api-url/login'},
    },
}

The sign in path becomes: http://localhost:3000/api/auth/http://api-url/login

The only solution which works is to define baseURL as my external URL with ending slash, and then use no leading slash on the path config, as seen below. This is the configuration which @phoenix-ru said should be considered a bug.

Example 4 - Correct URL

auth: {
  baseURL: 'http://api-url/',
  provider: {
    endpoints: {
      signIn: {path: 'login'},
    },
}

The sign in path becomes: http://api-url/login, allowing me to use my external endpoint.

I think a more sophisticated solution may be required. Perhaps checking if the endpoint path begins with a scheme (https://) in which case the baseURL is dropped, allowing a fully-specified URL to override the baseURL.

@zoey-kaiser
Copy link
Member

Hi everyone 👋

I took the following steps to address this issue:

I hope that these steps address this issue for now and that we can find a better solution in #797 to improve the module in the long run! 🥳

@phoenix-ru
Copy link
Collaborator

@coreyshuman Thank you for reporting that, it was definitely overlooked. As @zoey-kaiser outlined, we will do a proper fix so that URLs work as expected and don't feel hacky anymore.
As you already commented on #797, I would use that issue when addressing the fix

@mtzrmzia
Copy link

@phoenix-ru @zoey-kaiser I tried everything but doesn't work for me, still using the default baseURL
#802 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation A change to the documentation p4 Important Issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants