authTwa, router udpate > protectedLayout
This commit is contained in:
parent
0bc77e44f8
commit
7cb7a7d73a
Binary file not shown.
|
Before Width: | Height: | Size: 64 KiB After Width: | Height: | Size: 52 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 64 KiB |
|
|
@ -3,21 +3,32 @@ import { createBrowserRouter, RouterProvider } from "react-router-dom";
|
||||||
import { Routes } from "~/app/router/constants";
|
import { Routes } from "~/app/router/constants";
|
||||||
import { RootPage } from "~/pages/root";
|
import { RootPage } from "~/pages/root";
|
||||||
import { ViewContentPage } from "~/pages/view-content";
|
import { ViewContentPage } from "~/pages/view-content";
|
||||||
|
import { ProtectedLayout } from "./protected-layout";
|
||||||
|
|
||||||
const router = createBrowserRouter([
|
const router = createBrowserRouter([
|
||||||
|
{
|
||||||
|
element: <ProtectedLayout />,
|
||||||
|
children: [
|
||||||
{ path: Routes.Root, element: <RootPage /> },
|
{ path: Routes.Root, element: <RootPage /> },
|
||||||
{ path: Routes.ViewContent, element: <ViewContentPage /> },
|
{ path: Routes.ViewContent, element: <ViewContentPage /> },
|
||||||
{ path: Routes.SentryCheck, element: <div className="flex justify-center items-center h-screen">
|
{
|
||||||
|
path: Routes.SentryCheck,
|
||||||
|
element: (
|
||||||
|
<div className="flex h-screen items-center justify-center">
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
className="btn btn-primary p-2 border bg-red-900"
|
className="btn btn-primary border bg-red-900 p-2"
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
throw new Error("Sentry Test Error");
|
throw new Error("Sentry Test Error");
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
Sentry Test Error
|
Sentry Test Error
|
||||||
</button>
|
</button>
|
||||||
</div> },
|
</div>
|
||||||
|
),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
export const AppRouter = () => {
|
export const AppRouter = () => {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,20 @@
|
||||||
|
import { useEffect, useState } from 'react';
|
||||||
|
import { Outlet } from 'react-router-dom';
|
||||||
|
import { useAuthTwa } from '~/shared/services/authTwa';
|
||||||
|
|
||||||
|
export const ProtectedLayout = () => {
|
||||||
|
const auth = useAuthTwa();
|
||||||
|
const [isInitialLoad, setIsInitialLoad] = useState(true);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
void auth.mutateAsync().finally(() => {
|
||||||
|
setIsInitialLoad(false);
|
||||||
|
});
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
if (isInitialLoad || auth.isLoading) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return <Outlet />;
|
||||||
|
};
|
||||||
|
|
@ -102,7 +102,7 @@ export const WelcomeStep = ({ nextStep }: WelcomeStepProps) => {
|
||||||
<section
|
<section
|
||||||
className={"relative flex h-[100vh] items-center justify-center"}
|
className={"relative flex h-[100vh] items-center justify-center"}
|
||||||
>
|
>
|
||||||
<img alt={"splash"} className={"mb-20 h-[400px]"} src={"/splash.gif"} />
|
<img alt={"splash"} className={"mb-20 w-[50%]"} src={"/splash.gif"} />
|
||||||
</section>
|
</section>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
@ -112,7 +112,7 @@ export const WelcomeStep = ({ nextStep }: WelcomeStepProps) => {
|
||||||
<div className="flex items-center justify-center overflow-hidden w-[100%] h-[400px]">
|
<div className="flex items-center justify-center overflow-hidden w-[100%] h-[400px]">
|
||||||
<img
|
<img
|
||||||
alt={"splash"}
|
alt={"splash"}
|
||||||
className={" w-full shrink-0"}
|
className={"w-[50%] shrink-0"}
|
||||||
src={"/splash.gif"}
|
src={"/splash.gif"}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,26 @@
|
||||||
|
import { useMutation } from "react-query";
|
||||||
|
import { request } from "~/shared/libs";
|
||||||
|
import { useWebApp } from "@vkruglikov/react-telegram-web-app";
|
||||||
|
|
||||||
|
const sessionStorageKey = "auth_v1_token";
|
||||||
|
|
||||||
|
export const useAuthTwa = () => {
|
||||||
|
const WebApp = useWebApp();
|
||||||
|
|
||||||
|
const makeAuthRequest = async () => {
|
||||||
|
const res = await request.post<{
|
||||||
|
auth_v1_token: string;
|
||||||
|
}>("/auth.twa", {
|
||||||
|
twa_data: WebApp.initData,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (res?.data?.auth_v1_token) {
|
||||||
|
localStorage.setItem(sessionStorageKey, res.data.auth_v1_token);
|
||||||
|
} else {
|
||||||
|
throw new Error("Failed to get auth token");
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
};
|
||||||
|
|
||||||
|
return useMutation(["auth"], makeAuthRequest);
|
||||||
|
};
|
||||||
Loading…
Reference in New Issue