-
Notifications
You must be signed in to change notification settings - Fork 6
/
cognito.js
82 lines (74 loc) · 1.94 KB
/
cognito.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
let axios = require("axios");
let qs = require("qs");
let {
COGNITO_CLIENT_ID,
COGNITO_DOMAIN_NAME_URL,
COGNITO_LOGIN_GRANT_TYPE,
COGNITO_LOGIN_REDIRECT_URL,
COGNITO_LOGIN_RESPONSE_TYPE,
COGNITO_LOGIN_SCOPE,
COGNITO_LOGOUT_REDIRECT_URL,
} = process.env;
let getUserInfo = async ({ accessToken }) => {
let url = `${COGNITO_DOMAIN_NAME_URL}/oauth2/userInfo`;
let { data } = await axios({
url,
method: "get",
headers: {
Authorization: `Bearer ${accessToken}`,
},
});
// "data" looks like:
// {
// sub: '6010f967-5ed...',
// email_verified: 'true',
// email: 'example@example.com',
// username: '6010f967-5ed...'
// }
console.log("/oauth2/userInfo", data);
return data;
};
let postToken = async ({ code }) => {
console.log("code", code);
let url = `${COGNITO_DOMAIN_NAME_URL}/oauth2/token`;
let params = {
grant_type: COGNITO_LOGIN_GRANT_TYPE,
client_id: COGNITO_CLIENT_ID,
redirect_uri: COGNITO_LOGIN_REDIRECT_URL,
code,
};
let { data } = await axios({
url,
method: "post",
data: qs.stringify(params),
});
// "data" looks like:
// {
// id_token: "eyJra...",
// access_token: "eyJra...",
// refresh_token: "eyJjd...",
// expires_in: 3600,
// token_type: "Bearer",
// };
console.log("/oauth2/token", data);
return data;
};
let getLogin = async () => {
let params = {
client_id: COGNITO_CLIENT_ID,
response_type: COGNITO_LOGIN_RESPONSE_TYPE,
scope: COGNITO_LOGIN_SCOPE,
redirect_uri: COGNITO_LOGIN_REDIRECT_URL,
}
let url = `${COGNITO_DOMAIN_NAME_URL}/login?${qs.stringify(params, { encode: false })}`;
return url;
};
let getLogout = async () => {
let params = {
client_id: COGNITO_CLIENT_ID,
logout_uri: COGNITO_LOGOUT_REDIRECT_URL,
}
let url = `${COGNITO_DOMAIN_NAME_URL}/logout?${qs.stringify(params, { encode: false })}`;
return url;
};
module.exports = { getUserInfo, postToken, getLogin, getLogout };