awscognito-js
v0.2.5
Published
AwsCognito library to support the AWS authentication
Downloads
14
Readme
AWSCognito-JS
AWSCognito-JS library to simplify the integration of the AWS cognito authentication on javascript projects.
License
Features
- Configure
- Register User
- Verify User
- Resend Verification Code
- Authenticate User
- Refresh Session
- Forgot Password
- Reset Password
- Update Password
- SignOut User
- Update User Attributes
- Get User Attributes
Installation
npm i awscognito-js --save
Usage/Examples
Configure Cognito Library
const cognito = require("awscognito-js");
cognito.configure({
clientId: "Cognito Client ID",
userPoolId: "Cognito UserPool ID",
cookieStorage: "Domain Name"
});
| Parameter | Description | Required | | :-------- | :------------------------- | :------------------------- | | clientId | ClientId from AWS Cognito | True | | userPoolId | userPoolId from Cognito | True | | cookieStorage | Your Domain | False |
Usage
Register User
const attributes = { email: "[email protected]", "name": "Test User" }
const userName="testUser";
const password="Test1234!3"
cognito.register(userName, password, attributes).then(async (response)=>{
console.log(response)
}).catch(e=>{
console.log(e)
});
Output
{
user: { CognitoUser-object }
}
Verify Account
const userName="testUser";
const code="345232"
cognito.verifyAccount(userName, code).then(async (response)=>{
console.log(response)
}).catch(e=>{
console.log(e)
});
Resend Code
const userName="testUser";
cognito.resendCode(userName).then(async (response)=>{
console.log(response)
}).catch(e=>{
console.log(e)
});
Authenticate User
const userName="testUser";
const password="Test1234!3";
cognito.authenticate(userName, password).then(async (response)=>{
console.log(response)
}).catch(e=>{
console.log(e)
});
Output
{
idToken: {
jwtToken: 'eyJraWQiOiJcLzFOamU4SWRXYjVxMjhtM1dmb0RvWUNHNnNaang3OXc2QWI0cm5ZVFZhRT0iLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJhNjM0NDk4My05NDM0LTRhNjgtYmNlYy0zNDJhZGYyMzRlNmUiLCJlbWFpbF92ZXJpZmllZCI6ZmFsc2UsImlzcyI6Imh0dHBzOlwvXC9jb2duaXRvLWlkcC5hcC1zb3V0aGVhc3QtMS5hbWF6b25hd3MuY...',
payload: { user payload }
},
refreshToken: {
token: 'eyJjdHkiOiJKV1QiLCJlbmMiOiJBMjU2R0NNIiwiYWxnIjoiUlNBLU9BRVAifQ.rJYiWqo3pQ91UfGez8y4mGiZdJHbaLvTU-hXiwB32aKh3jd3xax6JObsy3AYHobVMBsbtP002k47PWhB...'
},
accessToken: {
jwtToken: 'eyJraWQiOiJWRzZkUGJvSHlRUzFaR1N3cmtYK2prQmRDbmlrXC9lZnNCN2FTWVFWWFRZZz0iLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJhNjM0NDk4My05NDM0LTRhNjgtYmNlYy0zNDJhZGYyMzRl...',
payload: { user payload }
},
clockDrift: 0
}
Refresh User Session
const userName="testUser";
const refreshToken="eyJjdHkiOiJKV1QiLCJlbmMiOiJBMjU2R0NNIiwiYWxnIjoiUlNBLU9BRVAifQ....";
cognito.refreshSession(userName, refreshToken).then(async (response)=>{
console.log(response)
}).catch(e=>{
console.log(e)
});
Output
SUCCCESS
Forgot Password
const userName="testUser";
cognito.forgotPassword(userName).then(async (response)=>{
console.log(response)
}).catch(e=>{
console.log(e)
});
Reset Password
const userName="testUser";
const code="632869";
const password="Test1234!3";
cognito.resetPassword(userName, code, password).then(async (response)=>{
console.log(response)
}).catch(e=>{
console.log(e)
});
Update Password
const oldPassword="Test1234!3";
const newPassword="Test1234!4";
cognito.updatePassword(oldPassword, newPassword).then(async (response)=>{
console.log(response)
}).catch(e=>{
console.log(e)
});
Signout User Session
cognito.signOut()