Skip to content

Instantly share code, notes, and snippets.

View diasjuniorr's full-sized avatar
🏠
Working from home

Dias Junior diasjuniorr

🏠
Working from home
View GitHub Profile
@diasjuniorr
diasjuniorr / auth.ts
Last active June 10, 2021 02:43
Next.js high-order function middlewware
// util/middleware/auth.ts
import { NextApiRequest, NextApiResponse } from 'next'
import { User } from './models/user'
// so we can add the user info to the req object
type NextApiRequestWithUser = NextApiRequest & {
user: string
}
export const auth = (handler: (req: NextApiRequestWithUser, res: NextApiResponse) => void) => {
@diasjuniorr
diasjuniorr / endpointGet.ts
Last active June 9, 2021 15:14
Next.js route handler
// pages/api/endpointGet.ts
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const { method, headers, query } = req
if (method === 'GET') {
console.log("inside the handler")
return res.status(200).json({})
}
import { useState, useEffect } from 'react'
const UseLocalStorageState = (key: string, defaultValue: any) => {
const [state, setState] = useState(() => {
let val
try {
val = JSON.parse(window.localStorage.getItem(key) || String(defaultValue))
} catch (error) {
val = defaultValue
}