Skip to content
On this page

Pre-release

You are looking at the website for the fully functional Feathers v5 (Dove) pre-release. Check out what's new, and please let us know about any issues or questions . The current v4 documentation can be found at crow.docs.feathersjs.com.

Anonymous authentication

Anonymous authentication can be allowed by creating a custom strategy that returns the params that you would like to use to identify an authenticated user.

ts
import { Params } from '@feathersjs/feathers'
import {
  AuthenticationBaseStrategy,
  AuthenticationResult,
  AuthenticationService
} from '@feathersjs/authentication'

class AnonymousStrategy extends AuthenticationBaseStrategy {
  async authenticate(authentication: AuthenticationResult, params: Params) {
    return {
      anonymous: true
    }
  }
}

export default function (app: Application) {
  const authentication = new AuthenticationService(app)
  // ... authentication service setup
  authentication.register('anonymous', new AnonymousStrategy())
}
import { Params } from '@feathersjs/feathers'
import {
  AuthenticationBaseStrategy,
  AuthenticationResult,
  AuthenticationService
} from '@feathersjs/authentication'

class AnonymousStrategy extends AuthenticationBaseStrategy {
  async authenticate(authentication: AuthenticationResult, params: Params) {
    return {
      anonymous: true
    }
  }
}

export default function (app: Application) {
  const authentication = new AuthenticationService(app)
  // ... authentication service setup
  authentication.register('anonymous', new AnonymousStrategy())
}
import { Params } from '@feathersjs/feathers'
import {
  AuthenticationBaseStrategy,
  AuthenticationResult,
  AuthenticationService
} from '@feathersjs/authentication'

class AnonymousStrategy extends AuthenticationBaseStrategy {
  async authenticate(authentication, params) {
    return {
      anonymous: true
    }
  }
}

export default function (app) {
  const authentication = new AuthenticationService(app)
  // ... authentication service setup
  authentication.register('anonymous', new AnonymousStrategy())
}
import { Params } from '@feathersjs/feathers'
import {
  AuthenticationBaseStrategy,
  AuthenticationResult,
  AuthenticationService
} from '@feathersjs/authentication'

class AnonymousStrategy extends AuthenticationBaseStrategy {
  async authenticate(authentication, params) {
    return {
      anonymous: true
    }
  }
}

export default function (app) {
  const authentication = new AuthenticationService(app)
  // ... authentication service setup
  authentication.register('anonymous', new AnonymousStrategy())
}

Next, we create a hook called allow-anonymous that sets params.authentication if it does not exist and if params.provider exists (which means it is an external call) to use that anonymous strategy:

ts
import { Hook, HookContext } from '@feathersjs/feathers'

export default (): Hook => {
  return async (context: HookContext) => {
    const { params } = context

    if (params.provider && !params.authentication) {
      context.params = {
        ...params,
        authentication: {
          strategy: 'anonymous'
        }
      }
    }

    return context
  }
}
import { Hook, HookContext } from '@feathersjs/feathers'

export default (): Hook => {
  return async (context: HookContext) => {
    const { params } = context

    if (params.provider && !params.authentication) {
      context.params = {
        ...params,
        authentication: {
          strategy: 'anonymous'
        }
      }
    }

    return context
  }
}
import { Hook, HookContext } from '@feathersjs/feathers'

export default () => {
  return async (context) => {
    const { params } = context

    if (params.provider && !params.authentication) {
      context.params = {
        ...params,
        authentication: {
          strategy: 'anonymous'
        }
      }
    }

    return context
  }
}
import { Hook, HookContext } from '@feathersjs/feathers'

export default () => {
  return async (context) => {
    const { params } = context

    if (params.provider && !params.authentication) {
      context.params = {
        ...params,
        authentication: {
          strategy: 'anonymous'
        }
      }
    }

    return context
  }
}

This hook should be added before the authenticate hook wherever anonymous authentication should be allowed:

js
all: [ allowAnonymous(), authenticate('jwt', 'anonymous') ],
all: [ allowAnonymous(), authenticate('jwt', 'anonymous') ],

If an anonymous user now accesses the service externally, the service call will succeed and have params.anonymous set to true.

Released under the MIT License.