Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f40df89

Browse files
committed
example authentication with aws cognito with lambda
0 parents commit f40df89

File tree

7 files changed

+398
-0
lines changed

7 files changed

+398
-0
lines changed

‎.gitignore‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
node_modules
2+
awscognito.zip
3+
awscognitosss.zip
4+
awscogsearnito.zip
5+
awscogsnito.zip
6+
search.js

‎event/index.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
username: "",
3+
password: ""
4+
}

‎login.js‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
var mysql = require("mysql");
2+
const AWSCognito = require("amazon-cognito-identity-js");
3+
4+
var pool = mysql.createPool({
5+
connectionLimit: 10,
6+
host: "",
7+
user: "root",
8+
password: "testing",
9+
database: ""
10+
});
11+
12+
var poolData = {
13+
UserPoolId: "", // Your user pool id here
14+
ClientId: "" // Your client id here
15+
};
16+
17+
var userPool = new AWSCognito.CognitoUserPool(poolData);
18+
19+
20+
exports.Login = (event, context, callback) => {
21+
22+
var username = event.username;
23+
var password = event.password
24+
25+
var authenticationData = {
26+
Username: username, // your username here
27+
Password: password, // your password here
28+
};
29+
30+
var authenticationDetails = new AWSCognito.AuthenticationDetails(authenticationData);
31+
32+
var userData = {
33+
Username: username,
34+
Pool: userPool
35+
}
36+
37+
var cognitoUser = new AWSCognito.CognitoUser(userData);
38+
39+
cognitoUser.authenticateUser(authenticationDetails, {
40+
onSuccess: function (result) {
41+
callback(null, result.getAccessToken().getJwtToken())
42+
}
43+
})
44+
45+
};
46+
47+

‎package-lock.json‎

Lines changed: 236 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "awscognito",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "",
10+
"license": "ISC",
11+
"dependencies": {
12+
"amazon-cognito-identity-js-node": "0.0.3",
13+
"mysql": "^2.16.0",
14+
"node-fetch": "^2.3.0"
15+
}
16+
}

‎refreshToken.js‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const AWS = require('aws-sdk');
2+
const CognitoUserPool = require('amazon-cognito-identity-js-node').CognitoUserPool;
3+
const CognitoUserSession = require('amazon-cognito-identity-js-node').CognitoUserSession;
4+
const CognitoUser = require('amazon-cognito-identity-js-node').CognitoUser;
5+
const CognitoIdToken = require('amazon-cognito-identity-js-node').CognitoIdToken;
6+
const CognitoAccessToken = require('amazon-cognito-identity-js-node').CognitoAccessToken;
7+
const CognitoRefreshToken = require('amazon-cognito-identity-js-node').CognitoRefreshToken;
8+
9+
const COGNITO_IDENTITY_POOL_ID = "YOUR IDENTITY POOL ID";
10+
const COGNITO_USER_POOL_ID = "YOUR COGNITO USER POOL ID";
11+
const COGNITO_CLIENT_ID = "YOUR COGNITO CLIENT ID";
12+
const AWS_API_GATEWAY_HOSTNAME = "API GATEWAY HOSTNAMR";
13+
const AWS_REGION = "us-east-2";
14+
15+
AWS.config.region = 'us-east-2'; // Region
16+
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
17+
IdentityPoolId: 'YOUR DENTITY POOL ID',
18+
});
19+
20+
exports.refreshToken = (event, context, callback) => {
21+
const AccessToken = new CognitoAccessToken({ AccessToken: event.accessToken });
22+
const IdToken = new CognitoIdToken({ IdToken: event.idToken });
23+
const RefreshToken = new CognitoRefreshToken({ RefreshToken: event.refreshToken });
24+
const sessionData = {
25+
IdToken: IdToken,
26+
AccessToken: AccessToken,
27+
RefreshToken: RefreshToken
28+
};
29+
const cachedSession = new CognitoUserSession(sessionData);
30+
31+
if (cachedSession.isValid()) {
32+
callback(null, "Token Valid") //result if your token valid
33+
} else {
34+
const poolData = {
35+
UserPoolId: COGNITO_USER_POOL_ID,
36+
ClientId: COGNITO_CLIENT_ID
37+
};
38+
const userPool = new CognitoUserPool(poolData);
39+
const userData = {
40+
Username: event.username,
41+
Pool: userPool
42+
};
43+
cognitoUser = new CognitoUser(userData)
44+
cognitoUser.refreshSession(RefreshToken, (err, session) => {
45+
if (err) throw err;
46+
const tokens = {
47+
accessToken: session.getAccessToken().getJwtToken(),
48+
idToken: session.getIdToken().getJwtToken(),
49+
refreshToken: session.getRefreshToken().getToken()
50+
};
51+
const loginInfo = {};
52+
loginInfo[`cognito-idp.${AWS_REGION}.amazonaws.com/${COGNITO_USER_POOL_ID}`] = tokens.idToken;
53+
const params = {
54+
IdentityPoolId: COGNITO_IDENTITY_POOL_ID,
55+
Logins: loginInfo
56+
};
57+
AWS.config.credentials = new AWS.CognitoIdentityCredentials(params)
58+
callback(null, tokens)
59+
});
60+
}
61+
62+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /