-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
62 lines (51 loc) · 1.66 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'use strict'
const AmazonCognitoIdentity = require('amazon-cognito-identity-js')
// paste your credentials or use environment variables
const USER_POOL_ID = ""
const APP_CLIENT_ID = ""
const USERNAME = ""
const PASSWORD = ""
// https://stackoverflow.com/a/61376674
function asyncAuthenticateUser(cognitoUser, cognitoAuthenticationDetails) {
return new Promise(function (resolve, reject) {
cognitoUser.authenticateUser(cognitoAuthenticationDetails, {
onSuccess: resolve,
onFailure: reject,
})
})
}
function initializer() {
console.log(`Generating tokens for user ${USERNAME}\n`)
const authenticationData = {
Username: USERNAME,
Password: PASSWORD,
}
const authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails(
authenticationData
)
const poolData = {
UserPoolId: USER_POOL_ID, // Your user pool id here
ClientId: APP_CLIENT_ID, // Your client id here
}
const userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData)
const userData = {
Username: USERNAME,
Pool: userPool,
}
const cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData)
return { cognitoUser, authenticationDetails }
}
async function generateAccessToken() {
const { cognitoUser, authenticationDetails } = initializer()
try {
let result = await asyncAuthenticateUser(cognitoUser, authenticationDetails)
console.log(`Access Token: ${JSON.stringify(result.getAccessToken())}\n`)
console.log(`JWT: ${JSON.stringify(result.getAccessToken().getJwtToken())}\n`)
console.log('Sync check 1')
} catch (error) {
console.log(error.message)
} finally {
console.log('Done!')
}
}
generateAccessToken()