Skip to content

Instantly share code, notes, and snippets.

@wattanakorn495
Created April 21, 2022 09:06
Show Gist options
  • Save wattanakorn495/b7d91c44055a62237894b7972fd5b08c to your computer and use it in GitHub Desktop.
Save wattanakorn495/b7d91c44055a62237894b7972fd5b08c to your computer and use it in GitHub Desktop.
AWS Signer example
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
);
}
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