Comments (2)
You might be able to do so in your feathers client setup (regardless of whether you're using websocket or rest, since feathers abstracts both the same way). This is what we do:
import createFeathersClient from '@feathersjs/feathers'
import auth from '@feathersjs/authentication-client'
import socketio from '@feathersjs/socketio-client'
import io from 'socket.io-client'
export function createFeathers() {
const socket = io()
const feathers = createFeathersClient()
feathers.configure(socketio(socket))
feathers.configure(
auth({
storage: window.localStorage,
storageKey: 'access-token',
path: 'api/authentication',
Authentication: AuthenticationClient,
}),
)
feathers.hooks({
before: {
all: [],
},
error: {
all: [handleUnauthenticated()],
},
})
return feathers
}
class AuthenticationClient extends auth.AuthenticationClient {
handleError(error, type) {
if (error.code === 401 || error.code === 404) {
const promise = this.removeAccessToken().then(() => this.reset())
return type === 'logout'
? promise
: promise.then(() => Promise.reject(error))
}
return Promise.reject(error)
}
}
function handleUnauthenticated() {
return context => {
const { error } = context
// The server is responding with 401s, that means the token is expired,
// or invalid, etc.
// The other case is when the socket is not connected, then we
// do not want to reload the page, we just want to wait for the socket
// to reconnect and reauthenticate, this is typically due to temporary
// loss of server connection
if (error.name === 'NotAuthenticated') {
if (window.location.pathname !== '/login') {
window.location.reload()
}
}
}
}
There are 2 things relevant in the snippet above:
- Customizing how
handleError
of FeathersAuthenticationClient
handles the authentication error (e.g. handling 401s and 404s instead of the Feathers default 401 and 403). - Most relevant to your question - installing a client side error hook
handleUnauthenticated
which gets called on any failed requests. In our case we reload the page (which then redirects to the login page).
Let me know if this helps.
from figbird.
This is awesome! Love the solution and the detailed answer is superb. Puts me on the right path!
from figbird.
Related Issues (16)
- useFind returns the same item multiple times HOT 4
- Add `useRealtime` / `useEvents` hook HOT 1
- Example incomplete HOT 2
- Invalid Query Parameter - `useFind` affects `useMutation` methods HOT 3
- What about typescript? HOT 1
- duplicated entries with combination of allPages and $in inside query with feathers api
- before hook
- Pagination
- relations included HOT 3
- ERR! Cannot read properties of null (reading 'children') HOT 1
- Question: Can Figbird be used in a ReactJs class component? HOT 1
- Improve realtime event handling in figbird
- useMutation doesn't update state during the first mutation call
- Removing Item Immediately
- Not properly working together with <React.Supsense> component
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 figbird.