Comments (11)
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.
from nhost-js-sdk.
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.
from nhost-js-sdk.
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.
@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.
@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.
@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.
@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.
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.
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)
- putString method returns sample image.
- Check second parameter of `storage.put(path, file)`
- Uncaught TypeError: auth.requestPasswordChange is not a function HOT 4
- [FEATURE REQUEST] User Role helper
- JWT Refresh across multiple tabs HOT 11
- Refresh lock seems to not work HOT 5
- Are source maps being generated correctly?
- Add CI
- module has no exported defaults
- change password endpoint response HOT 5
- Improve robustness of expired token handling
- AUTO_ACTIVATE_NEW_USERS is false but user still get logged in on register HOT 2
- Question: Handling errors in response HOT 3
- HttpOnly Cookies auth flow is flawed. HOT 1
- Setting a timer for a long period of time HOT 1
- `/refresh_token` gets called twice when reloading a page
- API Response object HOT 2
- onAuthStateChanged
- onAuthStateChanged Callback parameter HOT 1
- Database backups
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nhost-js-sdk.