Skip to content

Instantly share code, notes, and snippets.

View itwasmattgregg's full-sized avatar
:octocat:
Artisinally crafting interwebs

Matt Gregg itwasmattgregg

:octocat:
Artisinally crafting interwebs
View GitHub Profile
@itwasmattgregg
itwasmattgregg / DOM3D.js
Created March 27, 2024 14:47 — forked from OrionReed/dom3d.js
3D DOM viewer, copy-paste this into your console to visualise the DOM topographically.
// 3D Dom viewer, copy-paste this into your console to visualise the DOM as a stack of solid blocks.
// You can also minify and save it as a bookmarklet (https://www.freecodecamp.org/news/what-are-bookmarklets/)
(() => {
const SHOW_SIDES = false; // color sides of DOM nodes?
const COLOR_SURFACE = true; // color tops of DOM nodes?
const COLOR_RANDOM = false; // randomise color?
const COLOR_HUE = 190; // hue in HSL (https://hslpicker.com)
const MAX_ROTATION = 180; // set to 360 to rotate all the way round
const THICKNESS = 20; // thickness of layers
const DISTANCE = 10000; // ¯\\_(ツ)_/¯
import { withIronSessionApiRoute, withIronSessionSsr } from 'iron-session/next';
const sessionOptions = {
password: process.env.SECRET_COOKIE_PASSWORD,
cookieName: 'next-iron-session/examples/next.js',
// secure: true should be used in production (HTTPS) but can't be used in development (HTTP)
cookieOptions: {
secure: process.env.NODE_ENV === 'production',
},
};
import { withSessionRoute } from '@utils/session';
export default withSessionRoute(async (req, res) => {
const { password } = await req.body;
try {
if (password === process.env.PASSWORD) {
const user = { isLoggedIn: true };
req.session.user = user;
await req.session.save();
import { withSessionRoute } from '@utils/session';
export default withSessionRoute(async (req, res) => {
req.session.destroy();
res.json({ isLoggedIn: false });
});
import { withSessionRoute } from '@utils/session';
export default withSessionRoute(async (req, res) => {
const user = req.session.get('user');
if (user) {
// in a real world application you might read the user id from the session and then do a database request
// to get more information on the user if needed
res.json({
isLoggedIn: true,
import { useEffect } from 'react';
import Router from 'next/router';
import useSWR from 'swr';
export default function useUser({
redirectTo = false,
redirectIfFound = false,
} = {}) {
const { data: user, mutate: mutateUser } = useSWR('/api/user');
import { useState } from 'react';
import useUser from '@utils/useUser';
export default function Login() {
// here we just check if user is already logged in and redirect to admin
const { mutateUser } = useUser({
redirectTo: '/admin',
redirectIfFound: true,
});
import { withSessionSsr } from '@utils/session';
export default function Admin() {
// Users will never see this unless they're logged in.
return <h1>Secure page</h1>;
}
export const getServerSideProps = withSessionSsr(async function ({ req, res }) {
const user = req.session.user;
@itwasmattgregg
itwasmattgregg / main.yml
Created May 23, 2020 17:57
Automate firebase hosting with github actions
name: Build and Deploy
on:
push:
branches:
- master
jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
@itwasmattgregg
itwasmattgregg / firebase.json
Last active May 23, 2020 17:56
Automate firebase hosting with github actions
{
"hosting": {
"site": "site-name",
"public": "dist",
"ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}