Skip to content

Conversation

@BracketJohn
Copy link
Contributor

@BracketJohn BracketJohn commented Dec 1, 2022

Closes #65

This PR:

  • adds a plugin to maintain the session life cycle
  • adds a global auth-middleware
  • makes useSession sync
  • adds lastRefreshedAt as a public property
  • cleans up the structure of the module a bit:
    • add utils folder,
    • factor all useState into a single place to make it easier to maintain + update

Still to do:

  • document plugin usage / puropose
  • showcase full useSession properties in docs
  • update configuration section
  • adapt useSession usage everywhere
  • make guest mode clearer / more documented -> removed it
  • rename refetchInterval

for later work to update https://github.com/sidebase/nuxt-auth-example:

  • bump to new version once released
  • remove inline middleware
  • be aware that getCsrfToken changed

for new issues / out of scope:

Checklist:

  • issue number linked above after pound (#)
    • replace "Closes " with "Contributes to" or other if this PR does not close the issue
  • manually checked my feature / checking not applicable
  • wrote tests / testing not applicable
  • attached screenshots / screenshot not applicable

…nt it, make useSession data read-only, improve docs by expanding getting started + configuration section + credit @JoaoPedroAS51 + fix type check command, add lastRefrehsedAt to app.vue, add a new example page to the playground that will always be unprotected, remove now() util in favor of a real date, make `getCsrfToken` return the token right away
@BracketJohn BracketJohn marked this pull request as ready for review December 1, 2022 13:58
Copy link
Contributor

@JoaoPedroAS51 JoaoPedroAS51 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!!! Nice work!! 😃

I just pointed out a thing here and there.

import { useRequestEvent } from '#app'
import { useRuntimeConfig, navigateTo as _navigateTo } from '#imports'

const _getBasePath = () => parseURL(useRuntimeConfig().public.auth.url).pathname
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason for we use two different funcs to get the url? I'm just concerned about causing a mismatch between _getBasePath and getRequestUrl

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Originally, I did this to ensure that when the origin is set, it's strictly used on the app- and server-side. However, that doesn't make any sense as on the app-side anyone can edit the js/html/... anyways.

I'll use the useRequestEvent here but keep using the origin in the auth handler.

Copy link
Contributor

@JoaoPedroAS51 JoaoPedroAS51 Dec 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can create a func that uses the origin defined by the user and if not defined fallback to requrl. Makes sense? I believe this would fix the host/port issue

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought on the server-side we need it for security reasons, but you may be right that we can drop it. Looking through the next auth docs and code I cannot find a place that requires it, only the secret / NEXTAUTH_SECRET.

Let's investige + remove this in another issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've opened #72 foer this (:

const { data, lastRefreshedAt } = useSessionState()
const { getSession } = useSession()

await getSession()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we are not using Broadcast yet, we can add a check here to avoid an unnecessary request in client side if the session was already fetched in server side.

Suggested change
await getSession()
// Fetch the session only if not initialized yet.
if (data.value === undefined) {
await getSession()
}

return
}

await signIn(undefined, { callbackUrl: to.path }, { error: 'SessionRequired' })
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can use return here instead of await

status,
data
const getters = {
status: readonly(status),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As status is now computed, we don't need to add readonly

@BracketJohn BracketJohn merged commit a1522c5 into main Dec 1, 2022
@BracketJohn BracketJohn deleted the make_useSession_sync branch December 1, 2022 16:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Proposal to improve the experience of useSession

2 participants