質問編集履歴

3

コード修正

2018/07/27 05:27

投稿

r-kurokw
r-kurokw

スコア14

test CHANGED
File without changes
test CHANGED
@@ -118,43 +118,39 @@
118
118
 
119
119
  });
120
120
 
121
- return new Promise((res, rej) => {
121
+ cognitoUser.authenticateUser(authenticationDetails, {
122
122
 
123
- cognitoUser.authenticateUser(authenticationDetails, {
123
+ onSuccess: function(result) {
124
124
 
125
- onSuccess: function(result) {
125
+ AWS.config.region = common.get_cognito().region;
126
126
 
127
- AWS.config.region = common.get_cognito().region;
127
+ AWS.config.credentials = new AWS.CognitoIdentityCredentials({
128
128
 
129
- AWS.config.credentials = new AWS.CognitoIdentityCredentials({
129
+ IdentityPoolId: common.get_cognito().identityPoolId,
130
130
 
131
- IdentityPoolId: common.get_cognito().identityPoolId,
131
+ Logins: {
132
132
 
133
- Logins: {
133
+ "cognito-idp.[region].amazonaws.com/[region]_[userpoolid]": result.getIdToken().getJwtToken()
134
134
 
135
- "cognito-idp.[region].amazonaws.com/[region]_[userpoolid]": result.getIdToken().getJwtToken()
135
+ }
136
136
 
137
- }
137
+ });
138
138
 
139
- });
139
+ AWS.config.credentials.refresh((err) => {
140
140
 
141
- AWS.config.credentials.refresh((err) => {
141
+ (err)? reject(err): resolve("Successfully login.");
142
142
 
143
- (err)? rej(err): res("Successfully login.");
143
+ });
144
144
 
145
- });
145
+ },
146
146
 
147
- },
147
+ onFailure: function(err) {
148
148
 
149
- onFailure: function(err) {
149
+ console.log(err);
150
150
 
151
- console.log(err);
151
+ reject(err);
152
152
 
153
- reject(err);
154
-
155
- }
153
+ }
156
-
157
- });
158
154
 
159
155
  });
160
156
 

2

コード修正

2018/07/27 05:27

投稿

r-kurokw
r-kurokw

スコア14

test CHANGED
File without changes
test CHANGED
@@ -10,11 +10,7 @@
10
10
 
11
11
 
12
12
 
13
- AWS Cognitoを用いて、ログイン認証を実現しようとしています。
13
+ Node.jsを用いて, AWS Cognitoにおけるログイン認証を実現しようとしています。
14
-
15
-
16
-
17
- 以下のプログラムを作成しましたが、上手くいっていません。。
18
14
 
19
15
 
20
16
 
@@ -24,13 +20,13 @@
24
20
 
25
21
 
26
22
 
27
- const AWS = require("aws-sdk"); // For AWS
23
+ const AWS = require("aws-sdk");
28
24
 
29
- global.fetch = require("node-fetch"); // For Cognito
25
+ global.fetch = require("node-fetch");
30
26
 
31
- const AmazonCognitoIdentity = require("amazon-cognito-identity-js"); // For Cognito
27
+ const AmazonCognitoIdentity = require("amazon-cognito-identity-js");
32
28
 
33
- const CognitoUserPool = AmazonCognitoIdentity.CognitoUserPool; // For Cognito
29
+ const CognitoUserPool = AmazonCognitoIdentity.CognitoUserPool;
34
30
 
35
31
  const common = require("./common");
36
32
 
@@ -78,31 +74,13 @@
78
74
 
79
75
  return new Promise((resolve, reject) => {
80
76
 
81
- if (!JSON.parse(event.body) || !event.body) {
77
+ if (!JSON.parse(event.body) || !event.body) reject();
82
78
 
83
- reject("必須項目を入力してください.");
79
+ else if (!JSON.parse(event.body).address) reject();
84
80
 
85
- return;
81
+ else if (!JSON.parse(event.body).password) reject();
86
82
 
87
- }
88
-
89
- if (!JSON.parse(event.body).address) { // Check mail address input
90
-
91
- reject("メールアドレスを入力してください.");
92
-
93
- return;
94
-
95
- }
96
-
97
- if (!JSON.parse(event.body).password) { // Check mail address input
98
-
99
- reject("パスワードを入力してください.");
100
-
101
- return;
102
-
103
- }
104
-
105
- resolve(JSON.parse(event.body));
83
+ else resolve(JSON.parse(event.body));
106
84
 
107
85
  });
108
86
 
@@ -142,69 +120,43 @@
142
120
 
143
121
  return new Promise((res, rej) => {
144
122
 
145
- cognitoUser.authenticateUser(authenticationDetails, {
123
+ cognitoUser.authenticateUser(authenticationDetails, {
146
124
 
147
- onSuccess: function(result) {
125
+ onSuccess: function(result) {
148
126
 
149
- AWS.config.region = common.get_cognito().region;
127
+ AWS.config.region = common.get_cognito().region;
150
128
 
151
- AWS.config.credentials = new AWS.CognitoIdentityCredentials({
129
+ AWS.config.credentials = new AWS.CognitoIdentityCredentials({
152
130
 
153
- IdentityPoolId: common.get_cognito().identityPoolId,
131
+ IdentityPoolId: common.get_cognito().identityPoolId,
154
132
 
155
- Logins: {
133
+ Logins: {
156
134
 
157
- "cognito-idp.[region].amazonaws.com/[region]_[userpoolid]": result.getIdToken().getJwtToken()
135
+ "cognito-idp.[region].amazonaws.com/[region]_[userpoolid]": result.getIdToken().getJwtToken()
158
-
159
- }
160
-
161
- });
162
-
163
- AWS.config.credentials.refresh((err) => {
164
-
165
- if (err) {
166
-
167
- rej(err);
168
-
169
- return;
170
-
171
- } else {
172
-
173
- res("Successfully login.");
174
-
175
- }
176
-
177
- });
178
-
179
- },
180
-
181
- onFailure: function(err) {
182
-
183
- console.log(err);
184
-
185
- reject(err);
186
-
187
- return;
188
136
 
189
137
  }
190
138
 
191
- reject(err);
139
+ });
192
140
 
193
- return;
141
+ AWS.config.credentials.refresh((err) => {
194
142
 
195
- }
143
+ (err)? rej(err): res("Successfully login.");
196
144
 
197
- });
145
+ });
198
146
 
199
- })
147
+ },
200
148
 
201
- .then(function(val) {
149
+ onFailure: function(err) {
202
150
 
203
- resolve(val);
151
+ console.log(err);
204
152
 
205
- return;
153
+ reject(err);
206
154
 
155
+ }
156
+
207
- });;
157
+ });
158
+
159
+ });
208
160
 
209
161
  });
210
162
 
@@ -220,9 +172,9 @@
220
172
 
221
173
  {
222
174
 
223
- code: "UnknownError",
175
+ "code": "UnknownError",
224
176
 
225
- message: "Unkown error"
177
+ "message": "Unknown error, the response body from fetch is: undefined"
226
178
 
227
179
  }
228
180
 

1

環境の更新

2018/07/27 05:25

投稿

r-kurokw
r-kurokw

スコア14

test CHANGED
File without changes
test CHANGED
@@ -1,5 +1,13 @@
1
1
  ### 環境
2
2
 
3
+ ・Node.js 6.10
4
+
5
+ ・AWS Lambda
6
+
7
+
8
+
9
+ ### 現状
10
+
3
11
 
4
12
 
5
13
  AWS Cognitoを用いて、ログイン認証を実現しようとしています。