Skip to content

Commit

Permalink
Change state on login/logout
Browse files Browse the repository at this point in the history
  • Loading branch information
aleda145 committed Aug 2, 2022
1 parent 9884ab9 commit bd031d7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
28 changes: 22 additions & 6 deletions frontend/src/App.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useState } from "react";
import "./App.css";
import Auth from "./auth/pocketbase";
import { BrowserRouter, Routes, Route } from "react-router-dom";
import { BrowserRouter, Routes, Route, Link } from "react-router-dom";
import Dashboard from "./Dashboard/Dashboard";
import Preferences from "./Preferences/Preferences";
import PocketBase from "pocketbase";
Expand All @@ -15,23 +15,39 @@ const queryClient = new QueryClient({
},
});
function App() {
const [token, setToken] = useState();
console.log(token);
const client = new PocketBase("http://localhost:8090");
const [isLoggedIn, setIsLoggedIn] = useState(client.AuthStore.isValid);

console.log(client.AuthStore.isValid);
console.log(client.AuthStore.model.email);
if (!client.AuthStore.isValid) {
return <Auth client={client} />;
if (!isLoggedIn) {
return <Auth client={client} setIsLoggedIn={setIsLoggedIn} />;
}
return (
<div className="wrapper">
<h1>Application</h1>
<QueryClientProvider client={queryClient}>
<BrowserRouter>
<Link to="/">
<button>home</button>
</Link>
<Link to="/dashboard">
<button>dashboard</button>
</Link>
<Link to="/preferences">
<button>preferences</button>
</Link>

<Routes>
{/* <Route path="/" element={<App />} /> */}
<Route path="dashboard" element={<Dashboard client={client} />} />
<Route
path="dashboard"
element={
<Dashboard client={client} setIsLoggedIn={setIsLoggedIn} />
}
/>
<Route path="preferences" element={<Preferences />} />
<Route path="/" element={<Preferences />} />
</Routes>
</BrowserRouter>
</QueryClientProvider>
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/Dashboard/Dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import React, { useEffect } from "react";
import { useQuery } from "@tanstack/react-query";
import { useNavigate, NavLink } from "react-router-dom";

export default function Dashboard({ client }) {
export default function Dashboard({ client, setIsLoggedIn }) {
const navigate = useNavigate();

async function logOutClicked() {
await client.AuthStore.clear();
navigate("/dashboard");
setIsLoggedIn(false);
navigate("/");
}
const { isLoading, error, data } = useQuery(["repoData"], () =>
client.Records.getList("user_nums", 1, 50, {
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/auth/pocketbase.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import React, { useState } from "react";
import PropTypes from "prop-types";
export default function Auth({ client }) {
export default function Auth({ client, setIsLoggedIn }) {
const [username, setUserName] = useState();
const [password, setPassword] = useState();

const handleSubmit = async (e) => {
e.preventDefault();
const userData = await client.Users.authViaEmail(username, password);
console.log(userData);
setIsLoggedIn(client.AuthStore.isValid);
};
return (
<div className="login-wrapper">
Expand Down

0 comments on commit bd031d7

Please sign in to comment.