authTwa, router udpate > protectedLayout

This commit is contained in:
Verticool 2025-02-27 04:41:09 +06:00
parent 0bc77e44f8
commit 7cb7a7d73a
6 changed files with 72 additions and 15 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 64 KiB

After

Width:  |  Height:  |  Size: 52 KiB

BIN
public/splash_old.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 64 KiB

View File

@ -3,21 +3,32 @@ import { createBrowserRouter, RouterProvider } from "react-router-dom";
import { Routes } from "~/app/router/constants";
import { RootPage } from "~/pages/root";
import { ViewContentPage } from "~/pages/view-content";
import { ProtectedLayout } from "./protected-layout";
const router = createBrowserRouter([
{ path: Routes.Root, element: <RootPage /> },
{ path: Routes.ViewContent, element: <ViewContentPage /> },
{ path: Routes.SentryCheck, element: <div className="flex justify-center items-center h-screen">
<button
type="button"
className="btn btn-primary p-2 border bg-red-900"
onClick={() => {
throw new Error("Sentry Test Error");
}}
>
Sentry Test Error
</button>
</div> },
{
element: <ProtectedLayout />,
children: [
{ path: Routes.Root, element: <RootPage /> },
{ path: Routes.ViewContent, element: <ViewContentPage /> },
{
path: Routes.SentryCheck,
element: (
<div className="flex h-screen items-center justify-center">
<button
type="button"
className="btn btn-primary border bg-red-900 p-2"
onClick={() => {
throw new Error("Sentry Test Error");
}}
>
Sentry Test Error
</button>
</div>
),
},
],
},
]);
export const AppRouter = () => {

View File

@ -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 />;
};

View File

@ -102,7 +102,7 @@ export const WelcomeStep = ({ nextStep }: WelcomeStepProps) => {
<section
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>
);
}
@ -112,7 +112,7 @@ export const WelcomeStep = ({ nextStep }: WelcomeStepProps) => {
<div className="flex items-center justify-center overflow-hidden w-[100%] h-[400px]">
<img
alt={"splash"}
className={" w-full shrink-0"}
className={"w-[50%] shrink-0"}
src={"/splash.gif"}
/>
</div>

View File

@ -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);
};