Skip to content

Instantly share code, notes, and snippets.

@allpwrfulroot
allpwrfulroot / _app.js
Created December 18, 2019 02:28
Implementing our AuthProvider
import React from 'react'
import App from 'next/app'
import { AuthProvider } from 'contexts/auth'
import Layout from 'components/layout'
class MyApp extends App {
render() {
const { Component, pageProps } = this.props
return (
<AuthProvider>
@allpwrfulroot
allpwrfulroot / AuthProvider.js
Last active September 29, 2020 13:34
AuthProvider using React Context and providing a simple hook
import React, { createContext, useContext, useState, useEffect } from 'react'
import { useRouter } from 'next/router'
const AuthContext = createContext()
function AuthProvider({ children }) {
const { pathname, events } = useRouter()
const [user, setUser] = useState()
async function getUser() {
try {
@allpwrfulroot
allpwrfulroot / me.js
Last active December 18, 2019 02:40
API route to get user information from an HttpOnly cookie
// From the docs: https://github.com/auth0/nextjs-auth0#user-profile
import auth0 from 'contexts/auth0'
export default async function me(req, res) {
try {
await auth0.handleProfile(req, res)
} catch (error) {
console.error(error)
res.status(error.status || 500).end(error.message)
}
@allpwrfulroot
allpwrfulroot / index.js
Last active December 12, 2019 00:50
Moving the data fetch from render (useEffect) to SSR (getInitialProps)
Projects.getInitialProps = async ({ req }) => {
// Prevent unnecessary refetching
if (process.browser) {
const { projects } = window.__NEXT_DATA__.props.pageProps
if (projects && projects.length > 0) {
return { projects }
}
}
try {
// Have the correct API url
@allpwrfulroot
allpwrfulroot / get-projects.js
Last active December 12, 2019 14:24
API route that invokes the getMDXInfo utility for the 'projects' directory
import { getMDXInfo } from 'utils/get-meta'
export default async (req, res) => {
if (req.method !== 'GET') {
return (res.statusCode = 401)
}
res.setHeader('Content-Type', 'application/json')
try {
const { list } = await getMDXInfo('projects')
res.status(200).json({ projects: list })
@allpwrfulroot
allpwrfulroot / index.js
Created December 11, 2019 19:23
Projects index page listing all project .mdx info
function Projects() {
const [projects, setProjects] = useState([])
const [error, setError] = useState()
useEffect(() => {
async function fetchProjects() {
try {
const result = await fetch(`/api/get-projects`)
const json = await result.json()
setProjects(json.projects)
} catch (err) {
@allpwrfulroot
allpwrfulroot / get-meta.js
Last active December 12, 2019 14:24
Demo utility function for directory introspection and data collection
const fs = require('fs')
const { promisify } = require('util')
const YAML = require('yaml')
async function getMDXInfo(content) {
try {
const files = await promisify(fs.readdir)(`./pages/${content}`)
const mdx = await files.filter(f => f.includes('.mdx'))
let list = await Promise.all(
mdx.map(async p => {
@allpwrfulroot
allpwrfulroot / projects-list.json
Last active December 12, 2019 00:44
Demo snippet of JSON listing project page metadata
[
{
"name": "nextjs-workshop",
"size": 299,
"title": "nextjs-workshop",
"description": "public workshop for getting started with NextJS",
"createdAt": "2019-10-27",
"type": "github",
"tags": ["React", "NextJS"]
},
@allpwrfulroot
allpwrfulroot / next.config.js
Last active December 11, 2019 16:50
Gists for Medium article
const frontmatter = require('remark-frontmatter')
const withMDX = require('@next/mdx')({
extension: /\.mdx?$/,
options: {
remarkPlugins: [frontmatter],
},
})
module.exports = withMDX({
pageExtensions: ['js', 'jsx', 'mdx'],
@allpwrfulroot
allpwrfulroot / ImgUpload.js
Created June 8, 2017 01:51
Excerpt from screen with image selection, resize, and upload to grahpql
...
async uploadImageAsync(uri) {
let formData = new FormData();
formData.append("query", `
mutation CreateFile($input: CreateFileInput!) {
createFile(input: $input) {
changedFile {
id
name