authentication-utils
v0.1.4
Published
To handle common utils
Readme
authentication common utils
A utility function to extract Bearer token from HTTP headers in a NestJS application.
Installation
You can install the package using npm or yarn:
npm install authentication-utilsor
yarn add authentication-utilsUsage
To use the getTokenFromHeaders function in your NestJS project, follow the steps below:
- Import the Function:
import { getTokenFromHeaders } from 'authentication-utils';- Use the Function:
Assuming you have an HTTP headers object, you can extract the Bearer token like this:
const headers = {
'authorization': 'Bearer my-jwt-token',
};
const token = getTokenFromHeaders(headers);
console.log(token); // Outputs: 'my-jwt-token'Example with NestJS Controller
Here's an example of using getTokenFromHeaders in a NestJS controller:
import { Controller, Get, Headers } from '@nestjs/common';
import { getTokenFromHeaders } from 'authentication-utils';
@Controller('example')
export class ExampleController {
@Get()
getExample(@Headers() headers: { [key: string]: any }): string {
const token = getTokenFromHeaders(headers);
return `Extracted token: ${token}`;
}
}API
getTokenFromHeaders(headers: { [key: string]: any }): string | null
Parameters
headers: An object containing your HTTP headers. Typically, you'll passreq.headersfrom an Express request object.
Returns
- A
stringrepresenting the extracted Bearer token if present, ornullif not found.
Contributing
GitHub. https://github.com/JustinMuyabi/nestjs-authentication-utils.git
