handle auth
This commit is contained in:
parent
a620067e01
commit
2154990079
@ -1,17 +1,17 @@
|
|||||||
import { FC } from 'react';
|
import { FC } from 'react';
|
||||||
// import { Sidebar } from './sidebar/Sidebar';
|
import { Sidebar } from '../sidebar/Sidebar';
|
||||||
import { Box, Toolbar } from '@mui/material';
|
import { Box, Toolbar } from '@mui/material';
|
||||||
|
|
||||||
interface LayoutProps {
|
interface LayoutProps {
|
||||||
children: React.ReactNode;
|
children: React.ReactNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
// const drawerWidth = 240;
|
const drawerWidth = 240;
|
||||||
|
|
||||||
export const Layout: FC<LayoutProps> = ({ children }) => {
|
export const Layout: FC<LayoutProps> = ({ children }) => {
|
||||||
return (
|
return (
|
||||||
<Box sx={{ display: 'flex' }}>
|
<Box sx={{ display: 'flex' }}>
|
||||||
{/* <Sidebar drawerWidth={drawerWidth} /> */}
|
<Sidebar drawerWidth={drawerWidth} />
|
||||||
|
|
||||||
<Box component='main' sx={{ flexGrow: 1, px: 3, py: 2 }}>
|
<Box component='main' sx={{ flexGrow: 1, px: 3, py: 2 }}>
|
||||||
<Toolbar />
|
<Toolbar />
|
||||||
|
@ -1,9 +1,32 @@
|
|||||||
import { Navigate, Outlet } from 'react-router-dom';
|
import { Navigate, Outlet } from 'react-router-dom';
|
||||||
import { PublicRoutes } from '../config/routes';
|
import { PublicRoutes } from '../config/routes';
|
||||||
|
import { Layout } from '../components/layout/Layout';
|
||||||
|
import { useEffect, useMemo, useState } from 'react';
|
||||||
|
import PocketBase from 'pocketbase';
|
||||||
|
|
||||||
const AuthGuard = () => {
|
const AuthGuard = () => {
|
||||||
const user = true;
|
const pb = useMemo(
|
||||||
return user ? <Outlet /> : <Navigate replace to={PublicRoutes.HOME} />;
|
() => new PocketBase(import.meta.env.VITE_BASE_URL_API),
|
||||||
|
[]
|
||||||
|
);
|
||||||
|
|
||||||
|
const [, setToken] = useState(pb.authStore.token);
|
||||||
|
const [user, setUser] = useState(pb.authStore.model);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
return pb.authStore.onChange((token, model) => {
|
||||||
|
setToken(token);
|
||||||
|
setUser(model);
|
||||||
|
});
|
||||||
|
}, [pb.authStore]);
|
||||||
|
|
||||||
|
return user ? (
|
||||||
|
<Layout>
|
||||||
|
<Outlet />
|
||||||
|
</Layout>
|
||||||
|
) : (
|
||||||
|
<Navigate replace to={PublicRoutes.HOME} />
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export default AuthGuard;
|
export default AuthGuard;
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
import PocketBase from 'pocketbase';
|
import PocketBase from 'pocketbase';
|
||||||
|
|
||||||
const pb = new PocketBase(import.meta.env.VITE_BASE_URL_API);
|
|
||||||
|
|
||||||
export interface LoginResponse {
|
export interface LoginResponse {
|
||||||
record: {
|
record: {
|
||||||
avatar: string;
|
avatar: string;
|
||||||
@ -19,6 +17,7 @@ export const login = async (
|
|||||||
email: string,
|
email: string,
|
||||||
password: string
|
password: string
|
||||||
): Promise<LoginResponse> => {
|
): Promise<LoginResponse> => {
|
||||||
|
const pb = new PocketBase(import.meta.env.VITE_BASE_URL_API);
|
||||||
const collection = 'users';
|
const collection = 'users';
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -26,12 +25,6 @@ export const login = async (
|
|||||||
.collection(collection)
|
.collection(collection)
|
||||||
.authWithPassword(email, password);
|
.authWithPassword(email, password);
|
||||||
|
|
||||||
document.cookie = pb.authStore.exportToCookie({
|
|
||||||
maxAge: 3600000,
|
|
||||||
httpOnly: false,
|
|
||||||
});
|
|
||||||
|
|
||||||
pb.authStore.clear();
|
|
||||||
return {
|
return {
|
||||||
record: {
|
record: {
|
||||||
avatar: authData.record.avatar,
|
avatar: authData.record.avatar,
|
||||||
|
Loading…
Reference in New Issue
Block a user