-
-
Save F1LT3R/a244f60e743573d16321c47339a10ac6 to your computer and use it in GitHub Desktop.
AWS Cognito Auth for Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
"AWS": { | |
"Region": "us-east-1", | |
"ClientId": "304laiduvj1ld6ls8s2lsjfipa", | |
"UserPoolId": "us-east-1_aaksjfhTo", | |
"IdentityPoolId": "us-east-1:018b7af4-9384-8afe-019d-013cc0192d001", | |
"LoginEndpoint": "cognito-idp.us-east-1.amazonaws.com/us-east-1_aaksjfhTo" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const AWS = require('aws-sdk') | |
const AWSCognito = require('amazon-cognito-identity-js-node') | |
const Config = require('app/aws-config.js.env') | |
AWS.config.update({ | |
region: Config.AWS.Region | |
}) | |
const login = (Username, Password) => new Promise((resolve, reject) => { | |
const authData = { | |
Username, | |
Password | |
} | |
const authDetails = new AWSCognito.AuthenticationDetails(authData) | |
const poolData = { | |
UserPoolId: Config.AWS.UserPoolId, | |
ClientId: Config.AWS.ClientId | |
} | |
const userPool = new AWSCognito.CognitoUserPool(poolData) | |
const userData = { | |
Username, | |
Pool: userPool | |
} | |
const cognitoUser = new AWSCognito.CognitoUser(userData) | |
const onFailure = err => { | |
console.error('Error authenticating user!') | |
reject(err) | |
} | |
const onSuccess = LoginTokens => { | |
console.log('User Authenticated !') | |
const Logins = { | |
[Config.AWS.LoginEndpoint]: LoginTokens.getIdToken().getJwtToken() | |
} | |
AWS.config.credentials = new AWS.CognitoIdentityCredentials({ | |
IdentityPoolId: Config.AWS.IdentityPoolId, | |
Logins | |
}) | |
AWS.config.credentials.get(err => { | |
if (err) { | |
return reject(err) | |
} | |
const AccessTokens = { | |
AccessKeyId: AWS.config.credentials.accessKeyId, | |
SecretAccessKey: AWS.config.credentials.secretAccessKey, | |
SessionToken: AWS.config.credentials.sessionToken | |
} | |
const Credentials = AWS.config.credentials | |
const identity = { | |
LoginTokens, | |
AccessTokens, | |
Credentials | |
} | |
resolve(identity) | |
}) | |
} | |
cognitoUser.authenticateUser(authDetails, {onSuccess, onFailure}) | |
}) | |
module.exports = { | |
login | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const https = require('https') | |
const Congito = require('./aws-cognito-auth') | |
const host = 'd9ansjaodj.execute-api.us-east-1.amazonaws.com' | |
const username = 'foo' | |
const password = 'bar' | |
const post = (endpoint, data) => new Promise((resolve, reject) => { | |
Congito.login(username, password).then(identity => { | |
const options = { | |
method: 'POST', | |
hostname: host, | |
path: endpoint, | |
headers: { | |
Authorization: identity.LoginTokens.idToken.jwtToken | |
} | |
} | |
const req = https.request(options, res => { | |
const serverError = res.statusCode >= 399 | |
let body = '' | |
res.on('data', chunk => { | |
body += chunk | |
}) | |
res.on('end', () => { | |
if (serverError) { | |
return reject(body) | |
} | |
resolve(body) | |
}) | |
res.on('close', () => { | |
if (serverError) { | |
return reject(body) | |
} | |
resolve(body) | |
}) | |
res.on('error', err => { | |
console.error(err) | |
console.error(body) | |
reject(err) | |
}) | |
}) | |
const inputJsonString = JSON.stringify(data) | |
req.write(inputJsonString) | |
req.end() | |
}).catch(err => { | |
console.error(err) | |
}) | |
}) | |
const myPostData = {baz: 'qux'} | |
post('/my-app/api/user-data/', myPostData}).then(response => { | |
console.log(response) | |
}).catch(err => { | |
console.error(err) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment