Skip to content

Commit

Permalink
feat(enable js) html and js routes
Browse files Browse the repository at this point in the history
  • Loading branch information
ceddybi committed Apr 1, 2024
1 parent bedaea1 commit 7c7c405
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 62 deletions.
8 changes: 7 additions & 1 deletion src/app/api/captcha/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export async function POST(req: NextRequest) {
let iv = '';
let encrypted = '';
let confirmCode = '';
let hasJs = false;
const sendError = (_message = '') => {
let allowedOrigins = '*';
if (isProd) {
Expand Down Expand Up @@ -104,6 +105,9 @@ export async function POST(req: NextRequest) {
// console.log("captcha api", req);
try {
const formData = await req.formData();
const hasJavascript = formData.get('js') as string;
hasJs = hasJavascript === 'true';

iv = formData.get('encryptedi') as string;
if (isEmpty(iv)) return sendError();

Expand Down Expand Up @@ -141,12 +145,14 @@ export async function POST(req: NextRequest) {
cookieStore.set('refresh', 'true', { expires: in3Secs });
// cookieStore.set("message", "Successfully verified code", { expires: in3Secs });
// cookieStore.set("success", "true", { expires: in3Secs });
cookieStore.set('js', `${hasJs}`, { expires: in1hr });
cookieStore.set('endgamex', endgameCypher, { expires: in1hr });
cookieStore.set('endgamei', endgameCypherIv, { expires: in1hr });

const resUrl = !hasJs ? '/html' : '/';
return new Response(null, {
headers: {
Refresh: `0; url=/html`,
Refresh: `0; url=${resUrl}`,
// 'Refresh': `0; url=/`,
},
});
Expand Down
10 changes: 0 additions & 10 deletions src/app/homepage.tsx

This file was deleted.

2 changes: 1 addition & 1 deletion src/app/html/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { getMyWallets, getRates } from '@/lib/hooksServer/wallet';
import { HtmlPageWrapper } from './html.wrapper';

const publicRoutes = ['/html/login', '/html/signup', '/html/forgot-password'];
export default async function RootLayout({
export default async function HtmlLayout({
children,
}: {
children: React.ReactNode;
Expand Down
40 changes: 0 additions & 40 deletions src/app/icon.txt

This file was deleted.

29 changes: 29 additions & 0 deletions src/app/js.layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { awaitTo } from '@stoqey/client-graphql';
import { headers } from 'next/headers';
import React from 'react';

import { getMe } from '@/lib/hooksServer/user';

import { JsPageWrapper } from './js.wrapper';

const publicRoutes = ['/login', '/signup', '/forgot-password'];
export default async function JsLayout({
children,
}: {
children: React.ReactNode;
}) {
const user = await getMe();
const headersList = headers();
const fullUrl = headersList.get('x-url') || '';
const [, currentUrl] = await awaitTo(Promise.resolve(new URL(fullUrl)));
if (
!user &&
!publicRoutes.some((route) =>
(currentUrl?.pathname || '').startsWith(route),
)
) {
return <meta httpEquiv="refresh" content="0; url=/login" />;
}

return <JsPageWrapper user={user as any}>{children}</JsPageWrapper>;
}
91 changes: 91 additions & 0 deletions src/app/js.wrapper.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
'use client';

import {
AccentRegion,
Background,
CoreUIRoot,
Display,
FlexDirection,
FlexWrap,
Layout,
Overflow,
SVGAsset,
} from '@uuixjs/uuixweb';
import { isEmpty } from 'lodash';
import { usePathname } from 'next/navigation';
import React from 'react';

import { PageNotFound } from '@/components/404s/NotFound';
import Nav from '@/components/nav';
import VerticalSideBar from '@/components/sidebar/SideBar';

import { BodyContent } from './coreui';
// TODO NEXT_PUBLIC_JS_ONLY
const isJsOnly = !isEmpty(process.env.NEXT_PUBLIC_JS_ONLY);
const isProduction = process.env.NODE_ENV === 'production';

export const JsPageWrapper = ({ children, ...otherProps }: any) => {
const { user } = otherProps;
const pathname = usePathname();
const [hasJs, setHasJs] = React.useState(false);

React.useEffect(() => {
setHasJs(true);
}, []);

// if(isJsOnly){
// // return should only use no-js
// }

return (
<CoreUIRoot cssVars theme={otherProps.theme}>
{/* @ts-ignore */}
<AccentRegion
// {...generateAccentRegionProps("#28ff00")}
>
<Layout
id="layout-main"
background={Background.Base}
display={Display.Flex}
overflow={Overflow.Hidden}
fullWidth
fullHeight
>
{!hasJs && isProduction ? (
<PageNotFound
icon={SVGAsset.Lock}
message="Please enable javascript, and refresh"
title="NO JavaScript detected"
/>
) : (
<Layout
display={Display.Flex}
flexWrap={FlexWrap.NoWrap}
flexDirection={FlexDirection.Column}
overflow={Overflow.Hidden}
fullWidth
fullHeight
{...otherProps}
>
<Nav {...otherProps} />

{/* TODO no global sidebar? */}

{/* <BodyContent>{children}</BodyContent> */}
<BodyContent>
<Layout display={Display.Flex} fullWidth>
{user && <VerticalSideBar client {...otherProps} />}

<Layout fullWidth>
{/* RootMessageHtml */}
{children}
</Layout>
</Layout>
</BodyContent>
</Layout>
)}
</Layout>
</AccentRegion>
</CoreUIRoot>
);
};
4 changes: 2 additions & 2 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import './globals.css';
import './base.css';
import '@/styles/globals.css';
import '@/styles/base.css';
import 'react-image-gallery/styles/css/image-gallery.css';

import { isEmpty } from 'lodash';
Expand Down
13 changes: 10 additions & 3 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,36 @@ import { cookies } from 'next/headers';
import React from 'react';

import { PageNotFound } from '@/components/404s/NotFound';
import { AuthWall } from '@/components/AuthWall/authwall';
import {
encryptCaptchaCode,
getCaptchaFromLocalhost,
saveCaptchaToRedis,
} from '@/lib/hooksServer/captcha';
import { validateEndgameSession } from '@/middlewares/endgame.utils';

import RootIndex from './homepage';
import JsLayout from './js.layout';
import SearchPage from './search/page';

// PUBLIC PAGE
export default async function Home() {
export default async function RootPage() {
let iv;
let ciphertext;
let captcha;
const cookieStore = cookies();
const message = cookieStore.get('message')?.value;
const success = cookieStore.get('success')?.value === 'true';
const hasJs = cookieStore.get('js')?.value === 'true';

const endgamex = cookieStore.get('endgamex')?.value || '';
const endgamei = cookieStore.get('endgamei')?.value || '';
const [, validEndgame] = await awaitTo(
validateEndgameSession(endgamex, endgamei),
);

if (hasJs && validEndgame) {
return JsLayout({ children: <SearchPage /> });
}
if (validEndgame) {
return <meta httpEquiv="refresh" content="0; url=/html" />;
}
Expand Down Expand Up @@ -54,7 +61,7 @@ export default async function Home() {
}

return (
<RootIndex
<AuthWall
message={message}
success={success}
iv={iv}
Expand Down
11 changes: 9 additions & 2 deletions src/components/AuthWall/authwall.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ import {
import { styled } from '@uuixjs/uuixweb-lib';
import React from 'react';

import { SslattIcon } from '@/components/logo/icon';
import { MessageSuccessHtml } from '@/containers/actions.html';

import { SslattIcon } from '../logo/icon';

const SnakeBg = styled(Layout)`
.cls-snake-1,
.cls-snake-2 {
Expand All @@ -43,6 +42,13 @@ export interface AuthWallProps {

export const AuthWall = (props: AuthWallProps) => {
const { captcha = '', iv, encrypted, message = '', success = false } = props;

const [hasJs, setHasJs] = React.useState(false);

React.useEffect(() => {
setHasJs(true);
}, []);

return (
<AccentRegion inputColorIsDark>
<Layout
Expand Down Expand Up @@ -107,6 +113,7 @@ export const AuthWall = (props: AuthWallProps) => {
)}

<form action="/api/captcha" method="POST">
<input type="hidden" name="js" value={hasJs} />
<input type="hidden" name="encryptedi" value={iv} />
<input type="hidden" name="encryptedx" value={encrypted} />
{/* image render */}
Expand Down
File renamed without changes.
3 changes: 0 additions & 3 deletions src/styles/global.css

This file was deleted.

File renamed without changes.

0 comments on commit 7c7c405

Please sign in to comment.