Created
April 21, 2022 09:06
-
-
Save wattanakorn495/b7d91c44055a62237894b7972fd5b08c to your computer and use it in GitHub Desktop.
AWS Signer example
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
import axios from "axios"; | |
import { CEHCK_APIKEY_ENDPOINT } from "../config/auth"; | |
import { signedRequest } from "../signedRequest"; | |
export async function checkApikey(credentials, apikey) { | |
return signedRequest( | |
"GET", | |
CEHCK_APIKEY_ENDPOINT + `?key=${apikey}`, | |
null, | |
credentials | |
); | |
} |
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
import { Signer } from "@aws-amplify/core"; | |
import axios from "axios"; | |
export async function signedRequest( | |
method, | |
url, | |
data, | |
credentials, | |
apikey, | |
options | |
) { | |
let headers = {}; | |
if (apikey) { | |
headers.Authorization = `Bearer ${apikey}`; | |
} | |
// console.log("header", headers); | |
// let headers = { | |
// Authorization: `Bearer ${apikey}`, | |
// }; | |
let request = { | |
method, | |
url, | |
headers, | |
data, | |
}; | |
let access_info = { | |
access_key: credentials.accessKeyId, | |
secret_key: credentials.secretAccessKey, | |
session_token: credentials.sessionToken, | |
}; | |
let service_info = { | |
service: "execute-api", | |
region: "ap-southeast-1", | |
}; | |
if (options) { | |
if (options.service) { | |
service_info.service = options.service; | |
} | |
} | |
let signedUrl = Signer.signUrl(request, access_info, service_info, 300); | |
return await axios.get(signedUrl, { | |
headers, | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment