Skip to content

Instantly share code, notes, and snippets.

View Tribhuwan-Joshi's full-sized avatar
🌊
water

Tjsm Tribhuwan-Joshi

🌊
water
View GitHub Profile
@Tribhuwan-Joshi
Tribhuwan-Joshi / auth.ts
Created October 25, 2023 11:48
Add Auth in Next13
import NextAuth from "next-auth";
import GithubProvider from "next-auth/providers/github";
export const authOptions = {
providers: [
GithubProvider({
clientId: process.env.GITHUB_ID ?? "",
clientSecret: process.env.GITHUB_SECRET ?? "",
}),
],
@Tribhuwan-Joshi
Tribhuwan-Joshi / app.js
Created July 14, 2023 08:15
JWT overview
const express = require("express");
const jwt = require("jsonwebtoken");
const app = express();
app.get("/api", (req, res) => {
res.json({
message: "Welcome to the API",
});
});
@Tribhuwan-Joshi
Tribhuwan-Joshi / app.js
Created July 11, 2023 07:25
Authentication passport
const express = require("express");
const path = require("path");
const session = require("express-session");
const passport = require("passport");
const LocalStrategy = require("passport-local").Strategy;
const mongoose = require("mongoose");
const User = require("./models/user");
const {
getUser,
@Tribhuwan-Joshi
Tribhuwan-Joshi / throttle.js
Created May 20, 2023 15:23
Given a function fn and a time in milliseconds t, return a throttled version of that function.
/*
A throttled function is first called without delay and then, for a time interval of t milliseconds, can't be executed but should store the latest function arguments provided to call fn with them after the end of the delay.
*/
const throttle = function (fn , t){
let intervalInProgress = null;
let argsToProcess = null;
const intervalFunction = () =>{
if(argsToProcess == null){
@Tribhuwan-Joshi
Tribhuwan-Joshi / promisePool.js
Created May 18, 2023 11:33
Promise pool to limit concurrency
const promisePool = async function(functions, n) {
async function evaluateNext(){
if(functions.length == 0 ) return ;
const fn = functions.shift();
await fn();
await evaluateNext(); // call the function recursively until the function ends
}
const nPromises = Array(n).fill().map(evaluateNext); // create array of size n to call the evalute next fn.
await Promise.all(nPromises); // wait for all promise to resolve
};
@Tribhuwan-Joshi
Tribhuwan-Joshi / timeLimit.js
Created May 16, 2023 09:31
Promise Time Limit JS
/**
* @param {Function} fn
* @param {number} t
* @return {Function}
*/
var timeLimit = function(fn, t) {
return async function(...args) {
return Promise.race([new Promise((res,rej)=>setTimeout(()=>rej("Time Limit Exceeded"),t)) , fn(...args) ])
}
async function sleep(millis){
return new Promise(res => setTimeout(res,millis) ) // return a promise that will resolve after millis milliseconds
}
const t = Date.now();
sleep(100).then(()=>console.log(Date.now() - t ));
@Tribhuwan-Joshi
Tribhuwan-Joshi / curry.js
Created May 14, 2023 06:13
Smooth currying in JS
const curry = function (fn){
return function currying(...args){
if(args.length >= fn.length ){
return fn(...args);
}
else {
return function(...args2){
return currying.apply(...args.concat(args2))
}
}
@Tribhuwan-Joshi
Tribhuwan-Joshi / App.jsx
Created March 30, 2023 12:59
Note appwrite
import { useEffect, useRef, useState } from "react";
import {
BrowserRouter as Router,
Routes,
Route,
Navigate,
} from "react-router-dom";
import "./index.css";
import { projectId, account } from "./appwrite/appwriteConfig";
import Signup from "./components/Signup";
@Tribhuwan-Joshi
Tribhuwan-Joshi / Main.tsx
Last active March 16, 2023 04:34
Smooth scrolling react
import React from "react";
function Main() {
return (
<div className="flex-1 overflow-auto scroll-smooth ">
<section id="about" className="h-[100%] bg-pink-200"></section>
<section id="skills" className="h-[100%] bg-pink-400"></section>
<section id="projects" className="h-[100%] bg-pink-800"></section>
<section id="interests" className="h-[100%] bg-gray-400"></section>
<section id="certifications" className="h-[100%] bg-red-200"></section>