Skip to content

Instantly share code, notes, and snippets.

View polluterofminds's full-sized avatar

Justin Hunter polluterofminds

View GitHub Profile
@polluterofminds
polluterofminds / auth.js
Last active January 13, 2021 15:42
Auth
import axios from "axios";
export const subscribe = async (email) => {
try {
const data = {
email
}
await axios.post("/api/subscribe", data);
return true;
} catch (error) {
@polluterofminds
polluterofminds / login.js
Created January 13, 2021 15:47
Login Page
import { useState } from "react";
import Head from "next/head";
import Link from "next/link";
import { login } from "./actions/auth";
const Login = () => {
const [email, setEmail] = useState("");
const [errorMessage, setError] = useState("");
const [successMessage, setSuccessMessage] = useState("");
@polluterofminds
polluterofminds / verify.js
Created January 13, 2021 16:10
Verify Auth
import { useEffect, useState } from "react";
import { useRouter } from "next/router";
import { validateToken } from "./actions/auth";
import Head from "next/head";
import Link from "next/link";
const Verify = () => {
const [error, setError] = useState(false);
const router = useRouter();
useEffect(() => {
@polluterofminds
polluterofminds / auth.js
Created January 13, 2021 16:12
Validate Token Action
export const validateToken = async (token) => {
try {
const data = {
token
}
const res = await axios.post("api/validate", data);
if(res.data === "OK") {
localStorage.setItem("pinnie-token", token);
} else {
return false;
@polluterofminds
polluterofminds / validate.js
Created January 13, 2021 16:15
Validate Endpoint
import jwt from "jsonwebtoken";
const SECRET = "MY SUPER SECRET SIGNING KEY";
export const verifyToken = async (token) => {
try {
const verified = await jwt.verify(token, SECRET);
return verified;
} catch (error) {
throw error;
}
@polluterofminds
polluterofminds / index.js
Created January 13, 2021 16:34
Conditionally render home page
{loggedIn ? (
<PostList />
) : (
<div style={styles.flex}>
<Image
width="80px"
height="80px"
src="/pinnie.svg"
alt="Pinnie the pinata"
/>
@polluterofminds
polluterofminds / auth.js
Created January 13, 2021 16:41
Check User Sessions
export const checkUserSession = async () => {
try {
const token = localStorage.getItem("pinnie-token");
if(!token) {
return false;
} else {
return await validateToken(token);
}
} catch (error) {
throw error;
@polluterofminds
polluterofminds / PostList.js
Created January 13, 2021 17:17
PostList component
const PostList = () => {
const [allPosts, setPosts] = useState([]);
const [loading, setLoading] = useState(true);
useEffect(() => {
loadPosts();
}, []);
const loadPosts = async () => {
try {
const posts = await loadAllPosts();
@polluterofminds
polluterofminds / auth.js
Last active January 13, 2021 17:21
Load All Posts Action
export const loadAllPosts = async () => {
try {
const token = localStorage.getItem("pinnie-token");
const config = {
headers: {
"token": token,
"Content-Type": "application/json"
}
}
const res = await axios.get("/api/posts", config);
@polluterofminds
polluterofminds / posts.js
Created January 13, 2021 17:24
Posts API Endpoint
import axios from "axios";
import { verifyToken } from "./validate";
const config = {
headers: {
Authorization:
"Bearer YOUR PINATA JWT",
"Content-Type": "application/json",
},
};