cognito-auth-wrapper
v1.0.3
Published
Simplified AWS Cognito auth flow wrapped around the amazon-cognito-identity-js package
Downloads
8
Maintainers
Readme
Simple Cognito Auth
Overview
Simple wrapper around the amazon-cognito-identity-js to save the duplication of code when creating new NodeJS-based services that handle user authentication
Example
Login
import { APIGatewayProxyHandler, APIGatewayProxyEvent } from 'aws-lambda';
import { Ok, errorHandler, BadRequest } from 'aws-lambda-response-helper';
import { attemptLogin, ILoginRequest } from 'cognito-auth-wrapper';
export const loginHandler: APIGatewayProxyHandler = async (event: APIGatewayProxyEvent) => {
try {
if (!event.body) {
throw new BadRequest('Body not provided');
}
const loginRequest: ILoginRequest = JSON.parse(event.body);
const authResponse = await attemptLogin(
loginRequest,
'<your cognito client ID>',
'<your cognito pool ID>'
);
return new Ok(event, authResponse);
} catch (err) {
return errorHandler(err, event);
}
};
Refresh Session
import { APIGatewayProxyHandler, APIGatewayProxyEvent } from 'aws-lambda';
import { Ok, errorHandler, BadRequest } from 'aws-lambda-response-helper';
import { refreshSession, getUser } from './index';
export const refreshSessionHandler: APIGatewayProxyHandler = async (event: APIGatewayProxyEvent) => {
try {
if (!event.queryStringParameters || !event.queryStringParameters.refresh_token) {
throw new BadRequest('Refresh token not provided, must provide query param "refresh_token"');
}
const authResponse = await refreshSession(
getUser(event.headers.Authorization!),
event.queryStringParameters.refresh_token,
'<your cognito client ID>',
'<your cognito pool ID>'
);
return new Ok(event, authResponse);
} catch (err) {
return errorHandler(err, event);
}
};
Contributing
Contributions welcome! Please follow steps below
- Checkout a new branch from main
- Document changes in the CHANGELOG.md under a new version number (guide in the changelog)
- Up the version in package.json
- Submit a PR