Skip to content

Instantly share code, notes, and snippets.

{
"openapi": "3.0.0",
"paths": {
"/api/orders/orders/latest": {
"get": {
"operationId": "getLastPurchasedOrder",
"summary": "Get last purchase order for a user",
"description": "Get last purchased order by user ID",
"parameters": [
{
@kilgarenone
kilgarenone / medium-html-dump.html
Created March 2, 2021 13:09
the stuff of a medium article
<!DOCTYPE html>
<html lang="en">
<head>
<script defer src="https://cdn.optimizely.com/js/16180790160.js"></script>
<title data-rh="true">
Unlearning. My last post was about unlearning the… | by Kheoh Yee Wei |
Jan, 2021 | Medium
</title>
<meta data-rh="true" charset="utf-8" />
<meta
const argon2 = require("argon2-ffi").argon2i;
const crypto = require("crypto");
const util = require("util");
const createError = require("http-errors");
const randomBytes = util.promisify(crypto.randomBytes);
async function hashPassword(password) {
try {
return randomBytes(32).then((salt) => argon2.hash(password, salt));
} catch (e) {
@kilgarenone
kilgarenone / server.js
Last active April 28, 2020 14:16
server.js
const express = require("express");
const cors = require("cors");
const createError = require("http-errors");
const app = express();
const isProduction = app.get("env") === "production";
if (isProduction) {
app.set("trust proxy", 1);
import { h, Component } from "preact";
class Otp extends Component {
// onKeyDown event is triggered BEFORE onKeyPress event
keyDown = e => {
const { keyCode } = e;
const wasNonNumericEntered = !(keyCode >= 48 && keyCode <= 57) && !(keyCode >= 96 && keyCode <= 105);
// don't do anything if entered non-numeric values
if (wasNonNumericEntered) e.preventDefault();
@kilgarenone
kilgarenone / reqSessionUser.js
Created October 13, 2019 03:53
store user data to user session
const bcrypt = require("bcryptjs");
router.post("/register", async (req, res) => {
const { fullName, email, password } = req.body;
const hashedPassword = await bcrypt.hash(password, 8);
const query = `INSERT INTO account(fullname, email, password) VALUES ($1, $2, $3) RETURNING user_id, fullname, email`;
const values = [fullName, email, hashedPassword];
@kilgarenone
kilgarenone / userSession.js
Last active May 20, 2020 04:27
express session
// app.js
const express = require("express");
const session = require("express-session");
const redisStore = require("connect-redis")(session);
const redis = require("redis");
const redisClient = redis.createClient();
const app = express();
const isProduction = app.get("env") === "production";
@kilgarenone
kilgarenone / userAuth.js
Last active May 20, 2020 03:16
user authentication
const Auth = require("../lib/auth");
const multer = require("multer");
const upload = multer();
const asyncHandler = require("express-async-handler");
router.post("/signin", upload.none(), asyncHandler(async (req, res, next) => {
const { email, plainTextPassword } = req.body;
// get the hashed password associated to the email entered by user
const query = `SELECT password FROM account WHERE email=($1)`;
@kilgarenone
kilgarenone / hashingPassword.js
Last active May 20, 2020 02:54
hashing password
// import our wrapper auth.js
const Auth = require("../lib/auth");
const multer = require("multer");
// we gonna use upload.none() rather than body-parser to access 'req.body'
// I don't recommed inserting body-parser middleware to your 'app'. Specify it per route.
const upload = multer();
const asyncHandler = require("express-async-handler");
router.post("/register", upload.none(), asyncHandler(async (req, res, next) => {
const { fullName, email, password } = req.body;
@kilgarenone
kilgarenone / jwtExpress.js
Created October 7, 2019 13:43
jwt expressjs
function generateToken({ user_id, fullname, email }) {
const data = {
sub: user_id,
name: fullname,
email
};
return jwt.sign(data, SESSION_SECRET_KEY, {
expiresIn: "7d"
});