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
function checkPermission(role, resource, httpVerb){ | |
if (PERMISSIONS[role] && PERMISSIONS[role][resource]) | |
return PERMISSIONS[role][resource].includes(httpVerb); | |
return false; | |
} | |
// Example | |
// request from "admin" | |
// POST https://test-domain.com/products/ => true |
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
const PERMISSIONS = { | |
"vendor": { | |
"products": ["POST", "PUT", "DELETE", "GET"], | |
"orders": ["POST", "PUT", "DELETE", "GET"], | |
"stores": ["POST", "PUT", "DELETE", "GET"], | |
"dashboard": ["GET"] | |
}, | |
"customer": { | |
"products": ["GET"], |
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
const jwt = require('jsonwebtoken'); | |
// extract token from header | |
let authHeader = request.header.Authorization; | |
let token = authHeader.split(" ")[1]; | |
// decode token and get user's 'role' | |
let decodedVal = jwt.verify(token, process.env.JWT_KEY); | |
let role = decodedVal.role; |
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
// automatically appends updatedAt timestamp | |
const update = async (updateParams) => { | |
try { | |
const timestamp = new Date().getTime(); | |
params.UpdateExpression = params.UpdateExpression + ', #updatedAt = :updatedAt'; | |
params.ExpressionAttributeNames['#updatedAt'] = 'updatedAt'; | |
params.ExpressionAttributeValues[':updatedAt'] = timestamp; | |
return await db.update(updateParams).promise(); | |
} | |
catch (error) { throw error; } |
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
// USER | |
{ | |
"id": "user-23423423-234234-234234-23423", | |
"data": { | |
"firstname": "John", | |
"lastname": "Doe", | |
"email": "john.doe@gmail.com", | |
"age": "32" | |
}, | |
"createdAt": 7124564538, |
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 { v1 as uuidv1 } from 'uuid'; | |
function uuid(identifier){ | |
return identifier + '-' + uuidv1(); | |
} | |
uuid('user'); // ⇨ 'user-9k1teb4d-5b7d-7b2d-9bdd-2b0d7b3dcb6d' | |
uuid('product'); // ⇨ 'product-6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b' |
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
{ | |
"id": "objectType-23423423-234234-234234-23423", | |
"data": {...}, | |
"createdAt": 4564564564, | |
"updatedAt": 4564564567, | |
"createdBy": 'user-630eb68f-e0fa-5ecc-887a-7c7a62614681', //optional | |
"deletedAt": 4565961589 // optional | |
} |
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
version: 0.2 | |
phases: | |
install: | |
commands: | |
- npm i npm@latest -g | |
- pip install --upgrade pip | |
- pip install --upgrade awscli | |
pre_build: | |
commands: |
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
{ | |
"Version": "2012-10-17", | |
"Id": "Policy1593349925842", | |
"Statement": [ | |
{ | |
"Sid": "Stmt1593349907809", | |
"Effect": "Allow", | |
"Principal": "*", | |
"Action": "s3:*", | |
"Resource": [ |
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
class ExampleController < ApplicationController | |
before_action :res1, only: [:action_1, :action_2] | |
before_action :res2, only: [:action_3, :action_4] | |
def action_1 | |
# rest of the logic.... | |
render json: {data: '...'}, status: 200 | |
end | |
def action_2 |