This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import axios from "axios"; | |
export const subscribe = async (email) => { | |
try { | |
const data = { | |
} | |
await axios.post("/api/subscribe", data); | |
return true; | |
} catch (error) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(""); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(() => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{loggedIn ? ( | |
<PostList /> | |
) : ( | |
<div style={styles.flex}> | |
<Image | |
width="80px" | |
height="80px" | |
src="/pinnie.svg" | |
alt="Pinnie the pinata" | |
/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const checkUserSession = async () => { | |
try { | |
const token = localStorage.getItem("pinnie-token"); | |
if(!token) { | |
return false; | |
} else { | |
return await validateToken(token); | |
} | |
} catch (error) { | |
throw error; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const PostList = () => { | |
const [allPosts, setPosts] = useState([]); | |
const [loading, setLoading] = useState(true); | |
useEffect(() => { | |
loadPosts(); | |
}, []); | |
const loadPosts = async () => { | |
try { | |
const posts = await loadAllPosts(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import axios from "axios"; | |
import { verifyToken } from "./validate"; | |
const config = { | |
headers: { | |
Authorization: | |
"Bearer YOUR PINATA JWT", | |
"Content-Type": "application/json", | |
}, | |
}; |