Skip to content

Instantly share code, notes, and snippets.

View bibhuticoder's full-sized avatar
⌨️
Coding

Bibhuti Poudyal bibhuticoder

⌨️
Coding
View GitHub Profile
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
const PERMISSIONS = {
"vendor": {
"products": ["POST", "PUT", "DELETE", "GET"],
"orders": ["POST", "PUT", "DELETE", "GET"],
"stores": ["POST", "PUT", "DELETE", "GET"],
"dashboard": ["GET"]
},
"customer": {
"products": ["GET"],
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;
// 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; }
// USER
{
"id": "user-23423423-234234-234234-23423",
"data": {
"firstname": "John",
"lastname": "Doe",
"email": "john.doe@gmail.com",
"age": "32"
},
"createdAt": 7124564538,
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'
{
"id": "objectType-23423423-234234-234234-23423",
"data": {...},
"createdAt": 4564564564,
"updatedAt": 4564564567,
"createdBy": 'user-630eb68f-e0fa-5ecc-887a-7c7a62614681', //optional
"deletedAt": 4565961589 // optional
}
version: 0.2
phases:
install:
commands:
- npm i npm@latest -g
- pip install --upgrade pip
- pip install --upgrade awscli
pre_build:
commands:
{
"Version": "2012-10-17",
"Id": "Policy1593349925842",
"Statement": [
{
"Sid": "Stmt1593349907809",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:*",
"Resource": [
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