@dataobservatory/cognito-auth
v2.2.3
Published
Data Observatory's private library for handling custom Authentication flows using AWS Cognito.
Downloads
16
Readme
Data Observatory's Cognito Auth
Description:
This library uses the AWS Cognito SDK and covers the following cases:
- User Registration
- Confirmation of registered users.
- Forwarding of registration confirmation codes.
- User authentication with AWS Cognito.
- Password recovery flow.
- Change of password.
- Log out.
Each of the operations described above allows you to manipulate both the error and success return values with a handler
callback function.
Conventions used:
Objects and it's properties and methods are named in PascalCase.
Types and Interfaces are named in PascalCase.
Method Parameters are named in camelCase.
Decisions were made based on Cognito's SDK.
Usage:
At the main HTML file, add teh following script
tag:
<script> window.global = window; </script>
Otherwise, the library might not work as expected.
// Instantiate a cognitoAuth object:
const auth = new CognitoAuth();
// User attributes for sign in.
const username = "[email protected]";
const password = "Password1!";
const attributes = [{ Name: "email", Value: username }];
// Response handler
const handler = (error?: Error, result?: unknown) => {
if (error) console.error(error);
else if (result) console.log(JSON.stringify(result));
}
// Register a new user:
auth.SignUp(
username,
password,
attributes,
handler
);
// Enable automatic sign in using:
auth.CachedSignIn(handler);
// Manual sign in
auth.SignIn(username, password, handler);
// Sign out invalidating user's token (recommended)
auth.SignOut(true, handler);