Skip to main content

Authorize Your Code

In this example we're going to use the v3 Javascript Amazon Web Services SDK to authenticate ourselves with our Cognito credentials.

Install AWS Crendential Provider

At the terminal in the root of our project type:

npm install @aws-sdk/credential-provider-cognito-identity

Create Code to Authorize Your requests

Copy the following code into your index.js and replace the username and password with your credentials. Note that when you write production integration code you will probably want to get this information from a secure source such as environment variables or a secret manager.

index.js
const { CognitoIdentityProviderClient, InitiateAuthCommand } = require("@aws-sdk/client-cognito-identity-provider"); 
const client = new CognitoIdentityProviderClient({region: "eu-west-2"});

var params = {
AuthFlow: "USER_PASSWORD_AUTH",
ClientId: "1kvmindri6u8foemtqq3und18o",
AuthParameters: {
"USERNAME": "<<your api user's username>>",
"PASSWORD": "<<your api user's password>>",
}
};

const command = new InitiateAuthCommand(params);
client.send(command).then(response => {
console.log(response.AuthenticationResult.AccessToken); // successful response
});

Let's take a closer look at his code. In our params structure we define that we want to use a username and password to access get access to Legalesign resources, that's the ClientId.

note

Check the client id that you've been allocated in the Legalesign Console Application, or contact support. The one provided in the example may not be appropraite if you have additional client services or integrations. For this example though, the one provided will suffice.

Test the Code

Execute the code by running the following from the terminal:

node index.js

You should see something like the following:

{
AccessToken: 'eyJraWQiOiJJRFln...LxEsd4KZKxXqMOpqcSpxbFfcw2T3Vos7ZhQC5b96wTMkVDHs2BgOQvgGTGLQ',
ExpiresIn: 86400,
IdToken: 'eyJraWQiO...ZusOJDCo5dUy2F82xWO26t7hjMmDHGg9hRrzMAox-6MrpbJJ8fK7LCmXG2k6gWnNhRQN8oAhVRm6rPy25AUBUCtK80ByQ9DV6nLVx-Pw_UMMq-K_OJz1xoflw',
NewDeviceMetadata: undefined,
RefreshToken: 'eyJjdHkiOiJKV1QiLCJ...lH4D-3ikmnwctDhNHPE3iFQjGKxrp2FWSxTyRFwJ2GzgIBUqCL-po0NQYpeDj3YaCoyGtgniEpptegUD72cEf6XeMyCeJobYUg.Ki-I3EoOslK5e65Ys3lstQ',
TokenType: 'Bearer'
}