Closed gao-ti closed 2 years ago
arn:aws:lambda:us-east-1:280022023954:function:zhiyuangao-graphql
const axios = require("axios");
exports.handler = async (event, context, callback) => {
console.log("%% auth event %%", event);
console.log("%% auth context %%", context);
console.log("%% auth callback %%", callback);
const op = event.requestContext.queryString;
const ops = [
"getSectors", "getStocks", "loginUser", "createUser",
"getSectorById", "getStockById",
]
console.log("OP is", op)
ops.forEach( (operation) => {
if (op.includes(operation)) {
console.log("$$ match success ", operation);
callback(
null,
{ "isAuthorized": true }
)
}
})
const token = event.authorizationToken
console.log("### Token", token);
let isAuthorized = false;
let headers = {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": "Token " + token
}
try {
const result = await axios.get("https://8080-trilogygrou-tu2k22zhiyu-sq8b22pxndn.ws.legacy.devspaces.com/api/v1/users/profile", { headers: headers });
console.log(result);
if (result.status == 200) {
isAuthorized = true;
}
} catch(error) {
console.log(error);
}
const response = {
"isAuthorized": isAuthorized,
"resolverContext": {
"token": token
}
}
console.log("Resposne is::", response);
callback(null,response);
};
const axios = require('axios');
exports.handler = async (event, context, callback) => {
console.log("context", context);
console.log("event", event);
const baseUrl = "https://8080-trilogygrou-tu2k22zhiyu-sq8b22pxndn.ws.legacy.devspaces.com/api/v1";
console.log("EVENT", event);
let headers = "";
try {
headers = {Authorization: `Token ${event.resolverContext.token}`}
} catch (err) {
console.log("Non token request recvd", err);
}
switch(event.field) {
case 'getSectors':
{
const {data} = await axios.get(`${baseUrl}/sectors/`)
return data;
}
case 'getStocks':
{
const {data} = await axios.get(`${baseUrl}/stocks/`)
return data;
}
case 'getSectorById':
{
const {data} = await axios.get(`${baseUrl}/sectors/${event.arguments.id}/`)
return data;
}
case 'getStockById':
{
const {data} = await axios.get(`${baseUrl}/stocks/${event.arguments.id}/`)
return data;
}
case 'getUser':
{
const {data} = await axios.get(`${baseUrl}/users/profile/`, {headers})
return data;
}
case 'createUser':
{
const {data} = await axios.post(`${baseUrl}/auth/signup/`, event.arguments.input)
return data;
}
case 'createSector':
{
const {data} = await axios.post(`${baseUrl}/sectors/`, event.arguments.input, {headers})
return data;
}
case 'createStock':
{
const {data} = await axios.post(`${baseUrl}/stocks/`, event.arguments.input, {headers})
return data;
}
case 'loginUser':
{
const {data} = await axios.post(
`${baseUrl}/auth/login/`,
event.arguments.input,
);
return data;
}
}
const response = {
statusCode: 200,
body: JSON.stringify('Hello from Lambda!'),
};
return response;
};
Reviewed!