Coder Social home page Coder Social logo

Handle expired token error about figbird HOT 2 CLOSED

darrenkeen avatar darrenkeen commented on May 20, 2024
Handle expired token error

from figbird.

Comments (2)

KidkArolis avatar KidkArolis commented on May 20, 2024 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:

  1. Customizing how handleError of Feathers AuthenticationClient handles the authentication error (e.g. handling 401s and 404s instead of the Feathers default 401 and 403).
  2. 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.

darrenkeen avatar darrenkeen commented on May 20, 2024

This is awesome! Love the solution and the detailed answer is superb. Puts me on the right path!

from figbird.

Related Issues (16)

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.