Giter Club home page Giter Club logo

Comments (11)

MarcelloTheArcane avatar MarcelloTheArcane commented on June 11, 2024 1

Thanks @elitan. I actually made a simple promise-based SDK - https://gist.github.com/MarcelloTheArcane/ce0b3bf2efa2d0b23de3812ab1bf692e

It seems to work with the Vue router guards.

from nhost-js-sdk.

Svarto avatar Svarto commented on June 11, 2024

from nhost-js-sdk.

MarcelloTheArcane avatar MarcelloTheArcane commented on June 11, 2024

Hello @Svarto, thanks for replying so quickly!

I'm not sure how I'd do that with router guards though - do you use that in Vue?

I suppose I could simply use page-by-page authentication checks, but that seems like a lot of repetition.

from nhost-js-sdk.

Svarto avatar Svarto commented on June 11, 2024

from nhost-js-sdk.

MarcelloTheArcane avatar MarcelloTheArcane commented on June 11, 2024

Okay thanks for the tips, I'll see if there is anything comparable for Vue.

Thinking about it, it would be nice if everything was promise-based, for example auth.getJWTToken().

Is that something v3 could address?

from nhost-js-sdk.

nunopato avatar nunopato commented on June 11, 2024

@MarcelloTheArcane I am not sure that would fix your issue. The code for isAuthenticated is not async because it immediately returns true, false, or null. Using a promise instead would have similar behavior, it wouldn't guarantee to wait until it knows whether the user is logged in or not. We are actually improving a few things for v3.0, so I will come back to you on this soon.

from nhost-js-sdk.

MarcelloTheArcane avatar MarcelloTheArcane commented on June 11, 2024

@nunopato Yes, I did have a quick look and I reckon it would need quite a big refactor to manage 🙁

I suppose in the meantime I could try to make my own SDK, but this one is so convenient!

I wonder what would happen if I wrapped it all in a setTimeout for 0 seconds - pushing it to the back of the stack? EDIT - looks like that is working! EDIT no that's just because setTimeout returns an index 🤦

from nhost-js-sdk.

elitan avatar elitan commented on June 11, 2024

@MarcelloTheArcane isAuthenticated() works well with onAuthStateChanged() described in the docs here.

To register a callback to run when the auth state changes (e.g., when a user logs out) is done via onAuthStateChanged().

auth.onAuthStateChanged((loggedIn) => {
  console.log("auth state changed!");
  console.log({ loggedIn });
});

onAuthStateChanged will trigger every time the auth state changes from/to null, true, false.

I don't know how to implement this in Vue/NuxtJS but this is how we use it in React: https://github.com/nhost/react-auth/blob/master/src/index.js. This will trigger a re-render. For auth-gate, we recommend something like https://github.com/nhost/nhost/blob/main/examples/create-react-app-apollo/src/components/auth-gate.jsx

from nhost-js-sdk.

nunopato avatar nunopato commented on June 11, 2024

@MarcelloTheArcane glad you have it sorted out, we will have some example apps for Vue/NuxtJS soon. Feel free to close the issue if there aren't any remaining issues for you.

from nhost-js-sdk.

MarcelloTheArcane avatar MarcelloTheArcane commented on June 11, 2024

I'd really like to see promises in the official SDK, but can close if that's not the direction you would like to take.

from nhost-js-sdk.

elitan avatar elitan commented on June 11, 2024

I'd really like to see promises in the official SDK, but can close if that's not the direction you would like to take.

I talked with a NuxtJS developer last week and I now understand why you'd want that. It might come soon :)

from nhost-js-sdk.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.