Skip to content

Instantly share code, notes, and snippets.

View heytulsiprasad's full-sized avatar
⚛️
Overreacting

Tulsi Prasad heytulsiprasad

⚛️
Overreacting
View GitHub Profile
@heytulsiprasad
heytulsiprasad / bcryptHashing.js
Created February 24, 2020 16:27
gist having methods of bcrypt to hash passwords and compare efficiently
// shows hashing passwords through famous npm package bcrypt
const bcrypt = require("bcryptjs")
const myFunction = async () => {
const password = "thisisapassword"
const hashedPassword = await bcrypt.hash(password, 8)
console.log(password)
console.log(hashedPassword)
@heytulsiprasad
heytulsiprasad / jwtAuthenticate.js
Last active February 24, 2020 16:39
gist overviewing methods under jsonwebtoken to generate and verify jwts
// we are going to explore jsonwebtoken methods
const jwt = require("jsonwebtoken")
const myFunction = async () => {
const token = jwt.sign({ _id: "abcdefg" }, "anysecretkey", {
expiresIn: "10 seconds"
})
console.log(token)
@heytulsiprasad
heytulsiprasad / userModel.js
Created February 24, 2020 17:48
some mongoose pre handler to run before a query takes place with mongoDB
const bcrypt = require("bcryptjs")
const mongoose = require("mongoose")
// we need to design a schema first of all
const userSchema = new mongoose.Schema({
name: {
type: String
},
password: {
type: String
@heytulsiprasad
heytulsiprasad / index.js
Created February 25, 2020 06:42
making relation to another mongo document using mongoose methods
// this is a file in the root folder
const User = require("./models/user.js");
const main = async () => {
// to show tasks saved by a particular user
const user = await User.findById("5e54b46f3022d70b2ceaf2e5")
// console.log(user.tasks) --> without virtual (returns undefined, as tasks ain't a field)
@heytulsiprasad
heytulsiprasad / multerInit.js
Created February 25, 2020 13:30
get started with multer npm package for uploading files
// index.js >>> root file of /src
const multer = require("multer")
const express = require("express")
const app = express()
const upload = multer({
dist: "images"
})
@heytulsiprasad
heytulsiprasad / multer-config.js
Created March 2, 2020 05:56
adding all the functionality to package multer for image uploading
const multer = require("multer")
const express = require("express")
const app = express()
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, __dirname + "/uploads")
},
filename: function (req, file, cb) {
const firstname = file.originalname.split(".")[0]
@heytulsiprasad
heytulsiprasad / mongoose-model.js
Last active March 6, 2020 11:12
shows how a basic mongoose model should look like
const mongoose = require("mongoose")
mongoose.connect("mongodb://127.0.0.1:27017/whatever-you-call", {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
useFindAndModify: false
});
// connect to database
@heytulsiprasad
heytulsiprasad / auth.js
Last active March 6, 2020 16:39
design a middleware for a minimalistic application
const jwt = require("jsonwebtoken")
const User = require("../models/user")
const auth = async (req, res, next) => {
try {
const token = req.header("Authorization").replace("Bearer", "")
const decoded = jwt.verify(token, "thegivensecret")
const user = await User.findOne({
_id: decoded._id,
"tokens.token": token
@heytulsiprasad
heytulsiprasad / index.html
Last active March 7, 2020 05:11
how i add svg's to my website
<!-- Let all the boilerplate here -->
<svg class="my-svg-class">
<use xlink:href="img/sprite.svg#icon-whatever"></use>
</svg>
<!-- I bought the svg from IconMoon.io then made all the code inside sprite.svg
and targeted the icon name followed by # -->
@heytulsiprasad
heytulsiprasad / form.html
Created March 9, 2020 18:02
a blank form page with html to minimize the complexity
<form action="/contact" method="post">
<label>TITLE</label>
<label>
<input type="radio" name="title" value="mr">Mr
</label>
<label>
<input type="radio" name="title" value="mrs">Mrs
</label></br>
<label>First name</label></br>
<input type="text" name="first_name"></br>