Compare commits
7 Commits
main
...
hook-up-ba
Author | SHA1 | Date |
---|---|---|
|
50914f289d | 2 years ago |
|
68f012ba62 | 2 years ago |
|
168b3879af | 2 years ago |
|
c7e40722d1 | 2 years ago |
|
e5bc1a5802 | 2 years ago |
|
4c4a7d7009 | 2 years ago |
|
7e9ff1dfb9 | 2 years ago |
@ -1,40 +1,71 @@ |
||||
import React, { useState, useContext, createContext } from "react"; |
||||
|
||||
export interface AuthContextState { |
||||
loggedIn: boolean; |
||||
loginFailed: boolean; |
||||
login: (pass?: string) => void; |
||||
interface LoggedInState { |
||||
loggedIn: true; |
||||
headers: HeadersInit; |
||||
logout(): void; |
||||
} |
||||
|
||||
const DEFAULT_STATE: AuthContextState = { |
||||
loggedIn: false, |
||||
loginFailed: false, |
||||
login: () => console.error("No parent AuthContext found!"), |
||||
}; |
||||
interface LoggedOutState { |
||||
loggedIn: false; |
||||
login(password: string): Promise<boolean>; |
||||
} |
||||
|
||||
const AuthContext: React.Context<AuthContextState> = createContext( |
||||
DEFAULT_STATE |
||||
); |
||||
export type AuthState = LoggedInState | LoggedOutState; |
||||
|
||||
const password = "bubbles"; |
||||
const AuthContext = createContext({ |
||||
loggedIn: false, |
||||
login: () => { |
||||
throw new Error("No parent AuthContext found!"); |
||||
}, |
||||
} as AuthState); |
||||
|
||||
export const AuthProvider: React.FC = (props) => { |
||||
const [loggedIn, setLoggedIn] = useState(false); |
||||
const [loginFailed, setLoginFailed] = useState(false); |
||||
const [headers, setHeaders] = useState<HeadersInit | undefined>(); |
||||
|
||||
function logout() { |
||||
setLoggedIn(false); |
||||
setHeaders(undefined); |
||||
} |
||||
|
||||
function login(pass?: string): void { |
||||
if (pass === password) { |
||||
async function login(password: string): Promise<boolean> { |
||||
const username = process.env.NEXT_PUBLIC_EDITOR_USERNAME; |
||||
|
||||
if (!username) { |
||||
throw new Error( |
||||
"Missing NEXT_PUBLIC_EDITOR_USERNAME environment variable" |
||||
); |
||||
} |
||||
|
||||
const newHeaders = { |
||||
Authorization: `CustomBasic ${btoa(`${username}:${password}`)}`, |
||||
}; |
||||
|
||||
const res = await fetch("/api/editor/links", { headers: newHeaders }); |
||||
|
||||
if (res.status === 200) { |
||||
setLoggedIn(true); |
||||
setLoginFailed(false); |
||||
setHeaders(newHeaders); |
||||
return true; |
||||
} else { |
||||
setLoggedIn(false); |
||||
setLoginFailed(true); |
||||
logout(); |
||||
return false; |
||||
} |
||||
} |
||||
|
||||
return ( |
||||
<AuthContext.Provider value={{ loggedIn, login, loginFailed }} {...props} /> |
||||
<AuthContext.Provider |
||||
value={ |
||||
loggedIn && headers != null |
||||
? { loggedIn, headers, logout } |
||||
: { loggedIn: false, login } |
||||
} |
||||
{...props} |
||||
/> |
||||
); |
||||
}; |
||||
|
||||
export const useAuth = () => useContext(AuthContext); |
||||
export function useAuth(): AuthState { |
||||
return useContext(AuthContext); |
||||
} |
||||
|
@ -1,3 +0,0 @@ |
||||
export { default as Links } from "./Links"; |
||||
export { default as Editor } from "./Editor"; |
||||
export { default as Preview } from "./Preview"; |
@ -1,29 +1,27 @@ |
||||
import React from "react"; |
||||
import { GetStaticProps } from "next"; |
||||
import { Links } from "components"; |
||||
import { Link, Links } from "components/Links"; |
||||
import { readFileSync } from "fs"; |
||||
|
||||
// TODO: change
|
||||
const API = "https://api.thedogapi.com/v1/breeds?limit=10&page=0"; |
||||
export const getStaticProps: GetStaticProps<Props> = async () => { |
||||
if (!process.env.LINKS_FILE) { |
||||
throw new Error("Set the LINKS_FILE environment variable"); |
||||
} |
||||
|
||||
export const getStaticProps: GetStaticProps = async () => { |
||||
// fetch data here
|
||||
const data = await fetch(API).then((res) => res.json()); |
||||
const links = JSON.parse(readFileSync(process.env.LINKS_FILE).toString()); |
||||
|
||||
return { |
||||
props: { data }, // will be passed to the page component as props
|
||||
props: { links }, |
||||
revalidate: 1, |
||||
}; |
||||
}; |
||||
|
||||
const Home: React.FC = ({ data }: any) => { |
||||
return ( |
||||
<Links |
||||
links={data.map((dog: any) => ({ |
||||
title: dog.name, |
||||
url: "https://www.google.com/", |
||||
}))} |
||||
/> |
||||
); |
||||
interface Props { |
||||
links: Link[]; |
||||
} |
||||
|
||||
const Home: React.FC<Props> = ({ links }) => { |
||||
return <Links links={links} />; |
||||
}; |
||||
|
||||
export default Home; |
||||
|
Loading…
Reference in new issue