G
YouTube TikTok Games
Skip to content

Make next-auth framework agnostic (Vite, Vue, Express...) #2294

Closed

Description

@brillout

Follow up of https://twitter.com/balazsorban44/status/1410641689715347461.

cc @s-kris @balazsorban44

next-auth can actually already be used with Vite today thanks to https://github.com/s-kris/vite-ssr-next-auth.


UPDATE FROM MAINTAINER:

Proof of concept:

v4 has been marked as stable, and the source code has been refactored to be Next.js API routes agnostic. It is still undocumented, but we are ready to discuss how to integrate with other frameworks in the open. If you are developing in one of those communities (Vue, Svelte etc.), feel free to reach out!

Activity

  1. balazsorban44 commented on Jul 1, 2021

    @balazsorban44
    Contributor

    I'm very hopeful about this. We might have to do small adjustments, but I think in most cases, a tiny wrapper on top of NextAuth would be enough. The ground work could be done as early as in v4 (aiming for a release this summer).

    I already started to decouple/better namespace our client code, in v4 it will be under next-auth/react

    See #1473

  2. brillout commented on Jul 29, 2021

    @brillout
    Author

    The ground work could be done as early as in v4 (aiming for a release this summer)

    Exciting 😍.

    Will I then be able to use NextAuth.js in a let's say Vite + Vue app?

    I'm developing a framework ("Vike") on top of Vite + vite-plugin-ssr + RPC and I'd love to offer authentication to both my React and Vue users with NextAuth.js.

    Thanks for your wonderful work, it's lovely to see the JS ecosystem maturing.

  3. balazsorban44 commented on Jul 29, 2021

    @balazsorban44
    Contributor

    If we rewrite the backend a bit, as long as your framework can set cookies and do redirects and handle basic POST/GET requests, it should be good. We would need to separate the server/index.js file in a way that it is only a tiny wrapper for Next.js and the core stuff could be framework agnostic.

    I liked the Sveltekit endpoint approach:

    https://kit.svelte.dev/docs#routing-endpoints

  4. brillout commented on Jul 29, 2021

    @brillout
    Author

    Neat & yes, the backend integration should work then.

    How about Vue?

    You mentioned:

    I already started to decouple/better namespace our client code, in v4 it will be under next-auth/react

    Will we be able to have a next-auth/vue? I can see myself (and potentially other Vue users) to contribute.

  5. balazsorban44 commented on Jul 29, 2021

    @balazsorban44
    Contributor

    Yes, that would be easy I think.

    Here is the source code for the react specific frontend:

    https://github.com/nextauthjs/next-auth/blob/next/src/client/react.js

    We still have to work out some small things like tab syncing and invalidation of the session, but other than that it should not be hard, hopefully.

  6. brillout commented on Jul 29, 2021

    @brillout
    Author

    👌.

    Had a quick look over the source and yea should be fairly easy to port it to Vue's composition API; there aren't that many React specific things going on. AFACIT a common module that factors out the React/view-framework agnostic code should make porting quite easy.

    Can't wait to see NextAuth.js to become the de facto standard — Vite & Vue users will love it :-).

  7. wobsoriano commented on Jul 30, 2021

    @wobsoriano
    Contributor

    It's quite amazing that you don't even need to install react to use it with Nuxt. Here's a repo on how someone could use next-auth with nuxt! https://nuxt-next-auth.vercel.app/

    https://github.com/wobsoriano/nuxt-next-auth

  8. brillout commented on Jul 30, 2021

    @brillout
    Author

    @wobsoriano Wow neat... would you be up to develop some Vue composition functions? Basically like next-auth/react but for Vue. I'd happily assist/review.

  9. balazsorban44 commented on Jul 30, 2021

    @balazsorban44
    Contributor

    That's awesome @wobsoriano! Looking forward to set some time aside for this enhancement, so we can start the expansion! I will probably need some help for the Vue client.

  10. wobsoriano commented on Jul 31, 2021

    @wobsoriano
    Contributor

    Copied contents of next-auth/client and updated some lines to make it work with Vue's Composition API https://github.com/wobsoriano/nuxt-next-auth/blob/master/modules/next-auth/client.js

    Most if it are working now - useSession, getSession, signIn, signOut, getCsrfToken, getProviders.

    // store/index.js
    export const actions = {
        async nuxtServerInit({ commit }, { req }) {
            try {
                const session = await getSession({ req })
                commit('SET_SESSION', session);
            } catch (e) {
                commit('SET_SESSION', null);
            }
        }
    }
    <template>
      <div>
        <div v-for="provider in Object.values(providers)" :key="provider.id">
          <button @click="signIn(provider.id)">Sign in with {{ provider.name }}</button>
        </div>
      </div>
    </template>
    
    <script>
    import { defineComponent } from '@nuxtjs/composition-api';
    import { getProviders, signIn } from '~/modules/next-auth/client';
    
    export default defineComponent({
      middleware: 'guest',
      setup() {
        return {
          signIn
        }
      },
      async asyncData() {
        const providers = await getProviders()
        return {
          providers
        }
      }
    })
    </script>

    and adding configs directly to nuxt.config.js

    import Providers from 'next-auth/providers';
    
    export default {
      // other nuxt config
      nextAuth: {
        providers: [
          Providers.GitHub({
              clientId: process.env.GITHUB_CLIENT_ID,
              clientSecret: process.env.GITHUB_CLIENT_SECRET
          }),
        ],
      }
    }
  11. brillout commented on Aug 2, 2021

    @brillout
    Author

    @wobsoriano Neat neat 👌. What was your reason to use Nuxt's composition API instead of Vue's one? What do you think of factoring out the common code between next-auth/client and your version? That way anyone can use NextAuth.js with anything e.g. with Vue's composition API

  12. wobsoriano commented on Aug 3, 2021

    @wobsoriano
    Contributor

    @wobsoriano Neat neat 👌. What was your reason to use Nuxt's composition API instead of Vue's one? What do you think of factoring out the common code between next-auth/client and your version? That way anyone can use NextAuth.js with anything e.g. with Vue's composition API

    Actually published a simple Nuxt module for this that later we can add to next-auth/client since Nuxt 2.0 supports Vue 2 only

    https://github.com/wobsoriano/nuxt-next-auth

  13. brillout commented on Aug 3, 2021

    @brillout
    Author

    Nice 👍. Ideally nuxt-next-auth would be only a thin wrapper around a next-auth view-framework agnostic library. Right now there is a lot of duplicated code between next-auth/client and nuxt-next-auth right?

  14. wobsoriano commented on Aug 3, 2021

    @wobsoriano
    Contributor

    Nice 👍. Ideally nuxt-next-auth would be only a thin wrapper around a next-auth view-framework agnostic library. Right now there is a lot of duplicated code between next-auth/client and nuxt-next-auth right?

    Yeah, the whole client.js file is copied

  15. 42 remaining items

  16. balazsorban44 commented on Oct 13, 2021

    @balazsorban44
    Contributor

    That is really cool! 👏 I wonder if there is anything I should do/expose for this to be a viable base to build on top of. I would like to get the PR reviewed so we can get closer to a v4 stable release.

    It looks like you already have a working Nuxt & SvelteKit POC, which is awesome 😅

    Svelte and Vue clients could follow. In short, a client in next-auth is a list of methods that communicate with the backend REST API. The most important functionality is

    • a session cache that can re-fetch the session in intervals to avoid stale data
    • synced session state between tabs
    • signIn method to start the sign-in process directly to a provider, or redirect to the sign-in page
    • signOut empties the cache

    Docs here:

  17. wobsoriano commented on Oct 13, 2021

    @wobsoriano
    Contributor

    That is really cool! 👏 I wonder if there is anything I should do/expose for this to be a viable start to work on top of. I would like to get the PR reviewed so we can get closer to a v4 stable release.

    It looks like you already have a working Nuxt & SvelteKit POC, which is awesome 😅

    I think these functions can be used by Vue/Svelte (except useSession)

  18. balazsorban44 commented on Oct 13, 2021

    @balazsorban44
    Contributor

    And probably SessionProvider neither. I think we should create standalone clients anyway, in case something changes in Svelte or Vue in the future. I might also want to refactor the React client without worrying about crashing anything in other libraries.

    Do you want me to do anything more on the PR?

  19. wobsoriano commented on Oct 13, 2021

    @wobsoriano
    Contributor

    And probably SessionProvider neither. I think we should create standalone clients anyway, in case something changes in Svelte or Vue in the future. I might also want to refactor the React client without worrying about crashing anything in other libraries.

    Do you want me to do anything more on the PR?

    all good for now :D

  20. added
    staleDid not receive any activity for 60 days
    on Dec 13, 2021
  21. brillout commented on Dec 13, 2021

    @brillout
    Author

    Hi there! It looks like this issue hasn't had any activity for a while. It will be closed if no further activity occurs. If you think your issue is still relevant, feel free to comment on it to keep it open. (Read more at #912) Thanks!

    I think it's still relevant :-)

  22. removed
    staleDid not receive any activity for 60 days
    on Dec 13, 2021
  23. balazsorban44 commented on Dec 13, 2021

    @balazsorban44
    Contributor

    It is. I'm taking a small break from the project (low maintenance is still provided) for the holidays, and to be able to better focus on my new job. Will come back the next year.

    Cheers 🍻

  24. brillout commented on Dec 13, 2021

    @brillout
    Author

    That's actually good timing as we'll be able to show you the Vike + NextAuth integration next year :-).

    Happy holidays 🎄.

  25. deleted a comment from stale on Dec 15, 2021
  26. dohomi commented on Jan 18, 2022

    @dohomi

    I am using NextJS for the production app and Storybook for the UI testing solution and UI regression testing.

    Is it possible to use NextAuth currently already with v4 and Storybook? I couldn't find any pointers so far.

  27. balazsorban44 commented on Feb 5, 2022

    @balazsorban44
    Contributor

    Small update here, we are in the works to convert this repo to a monorepo that will contain all the projects like adapters, docs, and examples, with the core itself. this will open up for creating new packages for different frameworks. Once we are satisfied with the monorepo setup (#3650) we can get around this issue much more easily!

  28. locked and limited conversation to collaborators on Feb 12, 2022
  29. converted this issue into a discussion #3942 on Feb 12, 2022
  30. unpinned this issue on Dec 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions