Skip to content

Instantly share code, notes, and snippets.

View Octagon-simon's full-sized avatar
Coding

Simon Ugorji (@Octagon) Octagon-simon

Coding
View GitHub Profile
@Octagon-simon
Octagon-simon / sendEmail.js
Created March 8, 2024 17:17
This is a code snippet that you could use to send dynamic emails to your clients in NodeJS using NodeMailer.
import fs from 'fs';
import nodemailer from 'nodemailer';
async function sendEmail(data) {
try {
//destructure
const { dataToReplace, emailSubject, recipientEmail } = data;
// Replace with the actual path to your template file
const templatePath = `./templates/loginTemplate`;
@Octagon-simon
Octagon-simon / sessionStorage.js
Created December 8, 2022 18:08
All session storage methods and how to use them
//SESSION STORAGE
//Temporary saves data to the browser. Data saved is lost when the browser closes.
//syntax
//sessionStorage.setItem("key", "value")
//save an item
sessionStorage.setItem('name', 'simon');
//save multiple items in an object
sessionStorage.setItem('data', JSON.stringify({
@Octagon-simon
Octagon-simon / localStorage.js
Last active December 11, 2022 07:43
All localStorage methods and how to can use them
//LOCAL STORAGE
//Saves data permanently to the user's browser
//syntax
//localStorage.setItem("key", "value")
//save an item
localStorage.setItem('name', 'simon');
//save multiple items in an object
localStorage.setItem('data', JSON.stringify({
//reset route
app.get('/reset', async (req, res) => {
try {
//check for email and hash in query parameter
if (req.query && req.query.email && req.query.hash) {
//find user with suh email address
const user = await User.findOne({ email: req.query.email })
//check if user object is not empty
if (user) {
//now check if hash is valid
app.post('/reset', async (req, res) => {
try{
//find a document with such email address
const user = await User.findOne({email : req.body.email})
//check if user object is not empty
if(user){
//generate hash
const hash = new User(user).generatePasswordResetHash()
//generate a password reset link
const resetLink = `http://localhost:5000/reset?email=${user.email}?&hash=${hash}`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Enter New Password</title>
<!-- Google Fonts -->
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" rel="stylesheet" />
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Reset Your Password</title>
<!-- Google Fonts -->
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" rel="stylesheet" />
//generate password reset hash
userSchema.methods.generatePasswordResetHash = function(){
//create hash object,
//then create a sha512 hash of the user's hashed password
//and then return hash
const resetHash = crypto.createHash('sha512').update(this.hash).digest('hex')
return resetHash;
}
//generate password reset hash
userSchema.methods.generatePasswordResetHash = function(){
//create hash object, then create a sha512 hash of the user's current password
//and return hash
const resetHash = crypto.createHash('sha512').update(this.password).digest('hex')
return resetHash;
}
//verify password reset hash
userSchema.methods.verifyPasswordResetHash = function(resetHash = undefined){
@Octagon-simon
Octagon-simon / simple-pdo-class-with-php.php
Last active October 18, 2022 13:18
Simple PDO class with PHP. Use this class to perform CRUD operations following the PDO format in PHP
<?php
//SOME CONSTANTS HERE
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_USER', '');
DEFINE ('DB_PASS', '');
DEFINE ('DB_NAME', '');
class DatabaseClass{
private $connection = null;