From 26d17f6e5bc7fba9fc682571469df6f0be506b09 Mon Sep 17 00:00:00 2001 From: brandonyeu Date: Sat, 5 Oct 2024 16:03:06 -0400 Subject: [PATCH 1/2] First Branch Commit --- lib/Helpers/admin.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 lib/Helpers/admin.js diff --git a/lib/Helpers/admin.js b/lib/Helpers/admin.js new file mode 100644 index 0000000..9a243eb --- /dev/null +++ b/lib/Helpers/admin.js @@ -0,0 +1,15 @@ +import PocketBase from 'pocketbase'; + +const pb = new PocketBase('http://127.0.0.1:8090'); + +... + +const authData = await pb.admins.authWithPassword('test@example.com', '1234567890'); + +// after the above you can also access the auth data from the authStore +console.log(pb.authStore.isValid); +console.log(pb.authStore.token); +console.log(pb.authStore.model.id); + +// "logout" the last authenticated account +pb.authStore.clear(); \ No newline at end of file From 6824b3e5e3640b3f538abc4e7ea0597df1f4caa3 Mon Sep 17 00:00:00 2001 From: brandonyeu Date: Sat, 5 Oct 2024 17:23:41 -0400 Subject: [PATCH 2/2] Background Auth done --- Auth.js | 9 ++++ app/page.tsx | 17 +++++-- contexts/AuthWrapper.tsx | 102 +++++++++++++++++++++++++++++++++++++++ lib/Helpers/admin.js | 15 ------ 4 files changed, 123 insertions(+), 20 deletions(-) create mode 100644 Auth.js create mode 100644 contexts/AuthWrapper.tsx delete mode 100644 lib/Helpers/admin.js diff --git a/Auth.js b/Auth.js new file mode 100644 index 0000000..c57d173 --- /dev/null +++ b/Auth.js @@ -0,0 +1,9 @@ +import pb from "./lib/pocketbase" +export default function Auth(){ + return ( + <> +

Logged In: {pb.authStore.isValid.toString()}

+ + + ) +} \ No newline at end of file diff --git a/app/page.tsx b/app/page.tsx index ceec928..45384eb 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -1,12 +1,19 @@ import {NavBar} from "@/components/ours/Navbar"; import StartPage from "@/components/ours/Start Page/StartPage"; import TextScroll from "@/components/ours/Start Page/TextScroll/page"; +import Auth from "@/Auth"; +import AuthWrapper from "@/contexts/AuthWrapper"; + export default function Home() { return ( -
- - - -
+ +
+ + + + +
+
+ ); } diff --git a/contexts/AuthWrapper.tsx b/contexts/AuthWrapper.tsx new file mode 100644 index 0000000..14192ca --- /dev/null +++ b/contexts/AuthWrapper.tsx @@ -0,0 +1,102 @@ +"use client"; + +import { + createContext, + FC, + ReactNode, + useContext, + useEffect, + useState, +} from "react"; +import { useRouter } from "next/navigation"; +import pb from "../lib/pocketbase"; +import type { AuthProviderInfo, Record as PbRecord } from "pocketbase"; + +interface PbUser { + id: string; + name: string; + email: string; + username: string; + avatarUrl: string; +} + +interface AuthContextType { + user: PbUser | null; + googleSignIn: () => void; + githubSignIn: () => void; + setUserData: (user: PbRecord) => void; + signOut: () => void; +} + +const AuthContext = createContext(null); + +const AuthWrapper: FC<{ children: ReactNode }> = ({ children }) => { + const router = useRouter(); + + const [user, setUser] = useState(null); + const [googleAuthProvider, setGoogleAuthProvider] = + useState(null); + const [githubAuthProvider, setGithubAuthProvider] = + useState(null); + + useEffect(() => { + const initAuth = async () => { + const authMethods = await pb + .collection("users") + .listAuthMethods() + .then((methods) => methods) + .catch((err) => { + console.error(err); + }); + + if (authMethods) + for (const provider of authMethods.authProviders) { + if (provider.name === "google") setGoogleAuthProvider(provider); + if (provider.name === "github") setGithubAuthProvider(provider); + } + }; + + initAuth(); + + if (pb.authStore.model) setUserData(pb.authStore.model as PbRecord); + }, []); + + const setUserData = (pbUser: PbRecord) => { + const { id, name, email, username, avatarUrl } = pbUser; + setUser({ id, name, email, username, avatarUrl }); + }; + + const googleSignIn = () => { + signOut(); + localStorage.setItem("provider", JSON.stringify(googleAuthProvider)); + const redirectUrl = `${location.origin}/signin`; + const url = googleAuthProvider?.authUrl + redirectUrl; + + router.push(url); + }; + + const githubSignIn = () => { + signOut(); + localStorage.setItem("provider", JSON.stringify(githubAuthProvider)); + const redirectUrl = `${location.origin}/signin`; + const url = githubAuthProvider?.authUrl + redirectUrl; + + router.push(url); + }; + + const signOut = () => { + setUser(null); + pb.authStore.clear(); + }; + + return ( + + {children} + + ); +}; + +export const usePbAuth = () => useContext(AuthContext) as AuthContextType; +export default AuthWrapper; \ No newline at end of file diff --git a/lib/Helpers/admin.js b/lib/Helpers/admin.js deleted file mode 100644 index 9a243eb..0000000 --- a/lib/Helpers/admin.js +++ /dev/null @@ -1,15 +0,0 @@ -import PocketBase from 'pocketbase'; - -const pb = new PocketBase('http://127.0.0.1:8090'); - -... - -const authData = await pb.admins.authWithPassword('test@example.com', '1234567890'); - -// after the above you can also access the auth data from the authStore -console.log(pb.authStore.isValid); -console.log(pb.authStore.token); -console.log(pb.authStore.model.id); - -// "logout" the last authenticated account -pb.authStore.clear(); \ No newline at end of file