From f65cc5378b33eadb4c0d9d41ec2720ed00fdf9b8 Mon Sep 17 00:00:00 2001 From: Bruno Silva Date: Mon, 27 Jan 2025 22:52:50 -0500 Subject: [PATCH] Added login page and pocketbase validation --- package-lock.json | 7 +++ package.json | 1 + src/pocketbase.js | 26 ++++++++ src/theme/ColorModeToggle/index.js | 14 +++++ src/theme/Root.js | 99 ++++++++++++++++++++++++++++++ 5 files changed, 147 insertions(+) create mode 100644 src/pocketbase.js create mode 100644 src/theme/ColorModeToggle/index.js create mode 100644 src/theme/Root.js diff --git a/package-lock.json b/package-lock.json index 83633c1..ee1c21b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,7 @@ "@docusaurus/preset-classic": "3.7.0", "@mdx-js/react": "^3.0.0", "clsx": "^2.0.0", + "pocketbase": "^0.25.1", "prism-react-renderer": "^2.3.0", "react": "^19.0.0", "react-dom": "^19.0.0" @@ -12146,6 +12147,12 @@ "node": ">=4" } }, + "node_modules/pocketbase": { + "version": "0.25.1", + "resolved": "https://registry.npmjs.org/pocketbase/-/pocketbase-0.25.1.tgz", + "integrity": "sha512-2IH0KLI/qMNR/E17C7BGWX2FxW7Tead+igLHOWZ45P56v/NyVT18Jnmddeft+3qWWGL1Hog2F8bc4orWV/+Fcg==", + "license": "MIT" + }, "node_modules/postcss": { "version": "8.5.1", "resolved": "https://registry.npmjs.org/postcss/-/postcss-8.5.1.tgz", diff --git a/package.json b/package.json index f34a65c..97fe992 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "@docusaurus/preset-classic": "3.7.0", "@mdx-js/react": "^3.0.0", "clsx": "^2.0.0", + "pocketbase": "^0.25.1", "prism-react-renderer": "^2.3.0", "react": "^19.0.0", "react-dom": "^19.0.0" diff --git a/src/pocketbase.js b/src/pocketbase.js new file mode 100644 index 0000000..96b8fbf --- /dev/null +++ b/src/pocketbase.js @@ -0,0 +1,26 @@ +import PocketBase from "pocketbase"; + +//initialize pocketbase +const app = (() => { + const POCKETBASE_URL = 'https://pocketbase-ai.wirediv.dev/'; + return new PocketBase(POCKETBASE_URL); +})(); + +//pass authstore to be used to callback when the user is authenticated +export const auth = app.authStore; + +//clears the auth and logs the user out +export const logout = (afterAction = () => {}) => { + app.authStore.clear(); + afterAction(null); +}; + +//method to authenticate the user with pocketbase +export const signIn = async (email, password) => { + try { + await app.collection("users").authWithPassword(email, password); + } catch (err) { + console.error(err); + alert(err.message); + } +}; \ No newline at end of file diff --git a/src/theme/ColorModeToggle/index.js b/src/theme/ColorModeToggle/index.js new file mode 100644 index 0000000..302ca28 --- /dev/null +++ b/src/theme/ColorModeToggle/index.js @@ -0,0 +1,14 @@ +import React from 'react'; +import ColorModeToggle from '@theme-original/ColorModeToggle'; +import {logout} from '@site/src/pocketbase'; + +export default function ColorModeToggleWrapper(props) { + return ( + <> + + logout(() => window.location.reload())}> + Logout + + + ); +} \ No newline at end of file diff --git a/src/theme/Root.js b/src/theme/Root.js new file mode 100644 index 0000000..bd465df --- /dev/null +++ b/src/theme/Root.js @@ -0,0 +1,99 @@ +import React, { useState } from "react"; +import { signIn, auth } from "@site/src/pocketbase"; + +export default function Root({ children }) { + const [userAuth, setUserAuth] = useState(""); + const [email, setEmail] = useState(""); + const [password, setPassword] = useState(""); + const [loading, setLoading] = useState(false); + + const handleSubmit = async (event) => { + event.preventDefault(); + setLoading(true) + await signIn(email,password) + setLoading(false) + }; + + auth.onChange(async function (user) { + if (user !== null) { + setUserAuth(user); + } + }); + + return ( + <> + {userAuth != "" ? ( + <>{children} + ) : ( +
+
+ +

Docs

+ + setEmail(e.target.value)} + style={{ + marginBottom: "10px", + padding: "5px", + borderRadius: "5px", + border: "1px solid #ccc", + }} + /> + + setPassword(e.target.value)} + style={{ + marginBottom: "10px", + padding: "5px", + borderRadius: "5px", + border: "1px solid #ccc", + }} + /> + +
+
+ )} + + ); +} \ No newline at end of file