Browse Source

ログイン周り 整備

develop
sosuke.iwabuchi 2 years ago
parent
commit
07e175e94c
5 changed files with 27 additions and 39 deletions
  1. +1
    -2
      src/api/auth.ts
  2. +17
    -31
      src/contexts/AuthContext.tsx
  3. +1
    -1
      src/layouts/dashbord/navigator.tsx
  4. +7
    -4
      src/pages/common/Page404.tsx
  5. +1
    -1
      src/pages/dashboard/index.tsx

+ 1
- 2
src/api/auth.ts View File

@@ -3,8 +3,7 @@ import { getUrl } from "./url";


type MeResponse = { type MeResponse = {
data: { data: {
id: string;
name: string;
customer_name: string;
email: string; email: string;
}; };
} & APICommonResponse; } & APICommonResponse;


+ 17
- 31
src/contexts/AuthContext.tsx View File

@@ -8,69 +8,57 @@ type Auth = {
initialized: boolean; initialized: boolean;
authenticated: boolean; authenticated: boolean;


userId: string | null;
name: string; name: string;
email: string; email: string;


login: (email: string, password: string) => Promise<boolean>; login: (email: string, password: string) => Promise<boolean>;
logout: VoidFunction; logout: VoidFunction;
me: VoidFunction;
}; };
export const AuthContext = createContext<Auth>({ export const AuthContext = createContext<Auth>({
initialized: false, initialized: false,


authenticated: false, authenticated: false,


userId: null,
name: "", name: "",
email: "", email: "",


login: async (email: string, password: string) => false, login: async (email: string, password: string) => false,
logout: () => {}, logout: () => {},
me: () => {},
}); });


type Props = HasChildren; type Props = HasChildren;
function AuthContextProvider({ children }: Props) { function AuthContextProvider({ children }: Props) {
const [initialized, setInitialized] = useState(false); const [initialized, setInitialized] = useState(false);
const [userId, setUserId] = useState<string | null>(null);
const [name, setName] = useState(""); const [name, setName] = useState("");
const [email, setEmail] = useState(""); const [email, setEmail] = useState("");


const testLogin = () => {
//TODO MOCK対応
setInitialized(true);
setUserId("testing");
setName("testuser");
setEmail("test@test.com");
};

const authenticated = useMemo(() => { const authenticated = useMemo(() => {
// return !!userId;
return true;
}, [userId]);
return !!email;
}, [email]);


const { callAPI: callMe } = useAPICall({ const { callAPI: callMe } = useAPICall({
apiMethod: me, apiMethod: me,
backDrop: true, backDrop: true,
onSuccess: (res) => {
onSuccess: ({ data }) => {
setInitialized(true); setInitialized(true);


//削除予定
testLogin();
setEmail(data.email);
setName(data.customer_name);
}, },
onFailed: () => { onFailed: () => {
clear(); clear();
setInitialized(true); setInitialized(true);

//削除予定
testLogin();
}, },
}); });


const { callAPI: callLogin } = useAPICall({ const { callAPI: callLogin } = useAPICall({
apiMethod: APILogin, apiMethod: APILogin,
backDrop: true, backDrop: true,
onSuccess: (res) => {
setInitialized(true);
onSuccess: ({ data }) => {
setEmail(data.email);
setName(data.customer_name);
}, },
}); });


@@ -82,25 +70,23 @@ function AuthContextProvider({ children }: Props) {
}); });


const clear = () => { const clear = () => {
setUserId(null);
setName(""); setName("");
setEmail(""); setEmail("");
}; };


const login = async (email: string, password: string) => { const login = async (email: string, password: string) => {
//削除予定
testLogin();
return true;
// const res = await callLogin({ email, password });
// if (!res) return false;

// return res.result === ResultCode.SUCCESS;
const res = await callLogin({ email, password });
return res?.result === ResultCode.SUCCESS;
}; };
const logout = () => { const logout = () => {
callLogout({}); callLogout({});
console.info("ログアウト"); console.info("ログアウト");
}; };


const meFetch = () => {
callMe({});
};

useEffect(() => { useEffect(() => {
callMe({}); callMe({});
}, []); }, []);
@@ -111,13 +97,13 @@ function AuthContextProvider({ children }: Props) {
// Value // Value
initialized, initialized,
authenticated, authenticated,
userId,
name, name,
email, email,


// Func // Func
login, login,
logout, logout,
me: meFetch,
}} }}
> >
{children} {children}


+ 1
- 1
src/layouts/dashbord/navigator.tsx View File

@@ -64,7 +64,7 @@ const itemCategory = {
export default function Navigator(props: DrawerProps) { export default function Navigator(props: DrawerProps) {
const { ...other } = props; const { ...other } = props;


const { userId, name } = useAuth();
const { name } = useAuth();


const { navigateWhenChanged } = useNavigateCustom(); const { navigateWhenChanged } = useNavigateCustom();




+ 7
- 4
src/pages/common/Page404.tsx View File

@@ -7,15 +7,18 @@ import { getPath } from "routes/path";


export default function Page404() { export default function Page404() {
const { navigateWhenChanged } = useNavigateCustom(); const { navigateWhenChanged } = useNavigateCustom();
const { authenticated } = useAuth();
const { authenticated, initialized } = useAuth();


// ログインページにアクセス経験ある場合は、ログインページへ遷移させる
useEffect(() => { useEffect(() => {
if (!initialized) return;
if (authenticated) { if (authenticated) {
navigateWhenChanged(getPath(PageID.DASHBOARD_OVERVIEW)); navigateWhenChanged(getPath(PageID.DASHBOARD_OVERVIEW));
return; return;
} else {
navigateWhenChanged(getPath(PageID.LOGIN));
return;
} }
}, []);
}, [initialized, authenticated]);


return <Box>NotFound.</Box>;
return null;
} }

+ 1
- 1
src/pages/dashboard/index.tsx View File

@@ -1,6 +1,6 @@
import { Box } from "@mui/material"; import { Box } from "@mui/material";
import { PageID, TabID } from "pages";
import useDashboard from "hooks/useDashBoard"; import useDashboard from "hooks/useDashBoard";
import { PageID, TabID } from "pages";
import { useEffect } from "react"; import { useEffect } from "react";


export default function Overview() { export default function Overview() {


Loading…
Cancel
Save