$ npm install @feathersjs/authentication --save
The JWTStrategy
is an authentication strategy included in @feathersjs/authentication
for authenticating JSON web token service methods calls and HTTP requests, e.g.
{
"strategy": "jwt",
"accessToken": "<your JWT>"
}
header
(default:'Authorization'
): The HTTP header containing the JWTschemes
(default:[ 'Bearer', 'JWT' ]
): An array of schemes to support
The default settings support passing the JWT through the following HTTP headers:
Authorization: <your JWT>
Authorization: Bearer <your JWT>
Authorization: JWT <your JWT>
Standard JWT authentication can be configured with those options in config/default.json
like this:
{
"authentication": {
"jwt": {}
}
}
Note: Since the default options are what most clients expect for JWT authentication they usually don't need to be customized.
jwtStrategy.getEntity(id, params)
returns the entity instance for id
, usually entityService.get(id, params)
. It will not be called if entity
in the authentication configuration is set to null
.
jwtStrategy.authenticate(data, params)
will try to verify data.accessToken
by calling the strategies authenticationService.verifyAccessToken.
Returns a promise that resolves with the following format:
{
[entity],
accessToken,
authentication: {
strategy: 'jwt',
payload
}
}
Note: Since the JWT strategy returns an
accessToken
property (the same as the token sent to this strategy), that access token will also be returned by authenticationService.create instead of creating a new one.
Parse the HTTP request headers for JWT authentication information. Returns a promise that resolves with either null
or data in the form of:
{
strategy: '<strategy name>',
accessToken: '<access token from HTTP header>'
}
:::: tabs :options="{ useUrlFragment: false }"
::: tab "JavaScript"
const { AuthenticationService, JWTStrategy } = require('@feathersjs/authentication');
class MyJwtStrategy extends JWTStrategy {
}
module.exports = app => {
const authService = new AuthenticationService(app);
service.register('jwt', new MyJwtStrategy());
// ...
app.use('/authentication', authService);
}
:::
::: tab "TypeScript"
import { Application } from '@feathersjs/feathers';
import { AuthenticationService, JWTStrategy } from '@feathersjs/authentication';
import { LocalStrategy } from '@feathersjs/authentication-local';
class MyJwtStrategy extends JWTStrategy {
}
export default (app: Application) => {
const authService = new AuthenticationService(app);
service.register('jwt', new MyJwtStrategy());
// ...
app.use('/authentication', authService);
}
:::
::::