Skip to content

Instantly share code, notes, and snippets.

View Piyush-Use-Personal's full-sized avatar
🏠
Working from home

Piyush Dubey Piyush-Use-Personal

🏠
Working from home
View GitHub Profile
@Piyush-Use-Personal
Piyush-Use-Personal / deploy-aws-lambda.yml
Created October 31, 2021 13:42
A Github Action to deploy your aws lambda functions on branch push
name: Deploy lambda functions
on:
push:
branches:
- main
- feature/milestone4
jobs:
deploy-dev:
@Piyush-Use-Personal
Piyush-Use-Personal / index.js
Created June 24, 2021 18:21
Singly linked list
// create a node
class ListNode {
constructor(data, next = null) {
this.data = data;
this.next = next;
}
}
// create a node list
class LinkedList {
import Authentication from './../lib/Authentication.js';
class MyBank {
login = (req, res, next) => {
const userTypes = ['admin', 'agent', 'customer'];
let currentUserType = userTypes[Math.floor(Math.random() * userTypes.length)];
let payload = {
id : 'your unique id',
// some more user related data
}
import { Router } from 'express';
import Controller from './../controllers/index.js';
import AuthenticateRoles from './../middlewares/AuthenticateRoles.js';
var router = Router();
// To make user login.
router.get('/login', Controller.login)
// to get the account details
router.get('/account', AuthenticateRoles.Customer, Controller.getAccount)
@Piyush-Use-Personal
Piyush-Use-Personal / AuthenticationRole.js
Created February 14, 2021 17:07
Authenticate roles and the main controller for them
import Authentication from './../lib/Authentication.js';
class AuthRoles {
Authenticate = async (req, res, next, role) => {
try {
let decode = await Authentication.authenticate(req.headers.authorization, role)
req.user = decode;
next();
} catch (error) {
if(error.name == 'TokenExpiredError'){
@Piyush-Use-Personal
Piyush-Use-Personal / Authentication.js
Created February 14, 2021 16:46
Authentication wrapper to generate and verify the token
import JWT from 'jsonwebtoken';
class Authentication {
authenticate = (token, role) => {
return new Promise((resolve, reject) => {
jwt.verify(token, role, function(err, decoded) {
if (err) {
reject(err);
} else {
resolve(decoded)
@Piyush-Use-Personal
Piyush-Use-Personal / ResponseWrapper.js
Created November 28, 2020 15:07
Try Catch Block with async waterfall method in NodeJS
var messages = require('../static/StatusMessages');
var error_pointer = 'codes'
function SuccessResponse (code, res, responseObj, message) {
var response = {};
response.code = code;
response.result = responseObj;
if (code !== 200) {
response.message = messages[error_pointer][code];
} else {
response.message = message;