Skip to content

Instantly share code, notes, and snippets.

View nosvalds's full-sized avatar
🚀
Learning all the things

Nik Osvalds nosvalds

🚀
Learning all the things
View GitHub Profile
@nosvalds
nosvalds / scripts.js
Last active August 24, 2020 12:35
Medium Widget Alt Text Addition
((d, w) => {
// select the static HTML element that all this DOM manipulation will be contained to
let widget = d.getElementById("medium-widget");
const mainImgObserver = new MutationObserver(() => {
// if these elements have been rendered in the DOM
if (widget.getElementsByClassName("medium-widget-article__image").length > 0) {
// get the <a> link elements from the DOM, then map to get the child <img> elements
let imgElements = Array.from(widget.getElementsByClassName("medium-widget-article__image"))
.map((a) => a.firstElementChild);
// get the article description elements, then map to get the textConent of those elements
@nosvalds
nosvalds / scripts.js
Created August 24, 2020 12:57
Selecting and logging one element using a MutationObserver
((d, w) => {
// select the static HTML element that all this DOM manipulation will be contained to
let widget = d.getElementById("medium-widget");
const mainImgObserver = new MutationObserver(() => {
// if these elements have been rendered in the DOM
if (widget.getElementsByClassName("medium-widget-article__image").length > 0) {
// get the <a> link elements from the DOM, then map to get the child <img> elements
console.log(widget.getElementsByClassName("medium-widget-article__image"))
// stop observing
@nosvalds
nosvalds / index.html
Created August 24, 2020 13:14
HTML snippet from my portfolio of Pixel Perfect's Medium Widget
<!DOCTYPE html>
<html lang="en">
<head>
<!-- title and description -->
<meta name="description" content="Nikolas Osvalds personal web development portfolio website">
<title>Nik Osvalds - Home</title>
<meta property="og:image" content="https://www.nikolaso.com/images/photos/thumbnail.jpeg"/>
<!-- custom JS -->
<script defer src="./js/scripts.js"></script>
@nosvalds
nosvalds / API_Reponses.js
Last active September 10, 2020 08:59
DigitalHumani Tree Report Lambda Function
const Responses = {
_200(data = {}){
return {
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Methods': '*',
'Access-Control-Allow-Origin': '*',
},
statusCode: 200,
body: JSON.stringify(data)
@nosvalds
nosvalds / index.js
Created October 18, 2020 12:42
Express route definitions for password workflows
// Auth password routes
app.post(`/auth/forgot-password`, AuthController.forgotPassword);
app.post(`/auth/reset-password`, AuthController.resetPassword);
app.post(`/auth/update-password`, passport.authenticate('jwt', { session: false }), AuthController.updatePassword)
@nosvalds
nosvalds / user.json
Created October 18, 2020 13:16
JSON representation of a user record with password reset tokens in DynamoDB
{
"email": "<email>",
"password": "<password hash>",
"password_reset_tokens": {
"f7cfb2dbda77e093baf2a078f2ceb8c65965b7382109f23bb4710a9f83ad9c59": {
"expiration": "2020-06-30T00:00:00.00Z",
"used": true,
"created": "2020-06-29T23:00:00.00Z",
"updated": "2020-06-29T23:00:00.00Z"
},
@nosvalds
nosvalds / createResetToken.js
Created October 18, 2020 13:24
Function to create a password reset token
/**
* Create password reset token using inbuilt crypto
*/
const createResetToken = () => {
return require('crypto').randomBytes(32).toString('hex')
}
@nosvalds
nosvalds / User.js
Last active October 18, 2020 13:41
Function to set a users existing un-used password reset tokens to used
const table = process.env.USERS_TABLE;
const AWS = require('aws-sdk');
const dynamodb = new AWS.DynamoDB.DocumentClient();
/**
* Set a user's existing un-used reset tokens to used
* @param {object} user user object
*/
const expirePasswordTokens = async (user) => {
if (!user) throw new Error(`"user" is required`);
@nosvalds
nosvalds / AuthController.js
Last active October 18, 2020 13:50
forgot password controller function
const table = process.env.USERS_TABLE;
const fs = require('fs');
const fsPromises = fs.promises;
const Handlebars = require("handlebars");
// AWS
const AWS = require('aws-sdk')
const dynamodb = new AWS.DynamoDB.DocumentClient();
const ses = new AWS.SES({region: 'us-east-1'}); // Simple email service
@nosvalds
nosvalds / forgotPasswordEmail.html
Created October 18, 2020 14:12
body snippet from the forgot password email html template
<!-- BEGIN BODY // -->
<table border="0" cellpadding="0" cellspacing="0" width="100%" id="templateBody">
<tr>
<td class="bodyContent" style="padding-top:0; padding-bottom:0;">
<img src="https://digitalhumani.com/img/logo-final.png" style="max-width:60px;" id="bodyImage" />
</td>
</tr>
<tr>
<td valign="top" class="bodyContent">
<h1 style="color: #0A8A08 !important;">Password Reset Request</h1>