Skip to content

Commit

Permalink
fix(ui): layout auth
Browse files Browse the repository at this point in the history
  • Loading branch information
ceddybi committed Apr 15, 2024
1 parent 5fa06b8 commit ba483a0
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 24 deletions.
25 changes: 18 additions & 7 deletions src/app/html/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export default async function HtmlLayout({
let badges;

const [, currentUrl] = await awaitTo(Promise.resolve(new URL(fullUrl)));

const pathname = currentUrl?.pathname || '';
const config = await fetchConfig();

const allCurrencies = [
Expand All @@ -78,7 +78,10 @@ export default async function HtmlLayout({
currencies.map((w) => `${w.toUpperCase()}_USD`).join(','),
);

if (!config.REQUIRE_LOGIN) {
const loginRequired = config.REQUIRE_LOGIN;
const isIndex = pathname === '/html';

if (!loginRequired) {
publicRoutes.push('/html/ad/', '/html/store/');
}

Expand All @@ -87,12 +90,20 @@ export default async function HtmlLayout({
badges = await fetchBadges({ models: ['Notification', 'Chat'] });
vendor = await getVendor();
wallets = await getMyWallets(currencies);
}

if (loginRequired) {
if (
(isIndex && !user) ||
(!user &&
!publicRoutes.some((route) => (pathname || '').startsWith(route)))
) {
return <meta httpEquiv="refresh" content="0; url=/html/login" />;
}
} else if (
(currentUrl?.pathname === '/html' && config?.REQUIRE_LOGIN) ||
(currentUrl?.pathname !== '/html' &&
!publicRoutes.some((route) =>
(currentUrl?.pathname || '').startsWith(route),
))
!user &&
!isIndex &&
!publicRoutes.some((route) => (pathname || '').startsWith(route))
) {
return <meta httpEquiv="refresh" content="0; url=/html/login" />;
}
Expand Down
14 changes: 14 additions & 0 deletions src/app/signup/login.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use client';

import LayoutPage from '@/components/Layout';
import SignIn from '@/containers/SignIn';

export function Login() {
return (
<LayoutPage>
<SignIn afterLogin="/home" signup />
</LayoutPage>
);
}

export default Login;
13 changes: 13 additions & 0 deletions src/app/signup/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ApolloWrapper } from '@/lib/apollo-wrapper.client';

import Login from './login';

const loginPage = () => {
return (
<ApolloWrapper>
<Login />;
</ApolloWrapper>
);
};

export default loginPage;
56 changes: 41 additions & 15 deletions src/components/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const Wrapper = styled.div`

const publicRoutes = [
'/login',
'/register',
'/signup',
'/forgot-password',
'/reset-password',
];
Expand All @@ -55,36 +55,62 @@ function LayoutPage({ children, ...props }: LayoutPageProps) {
auth,
fallback = null,
nav = true,
user: userAuth,
user,
...otherProps
} = props;

const pathname = usePathname();

if (!getConfig().REQUIRE_LOGIN) {
const loginRequired = getConfig().REQUIRE_LOGIN || auth;

const isIndex = pathname === '/';

if (!loginRequired) {
publicRoutes.push('/ad/', '/store/');
}

// isVendor
const isAdminUser = (userAuth && userAuth.admin) || false;
const isLoggedIn = userAuth && userAuth.id;
const isAdminUser = (user && user.admin) || false;
const isLoggedIn = user && user.id;

const FallbackComponent = () => <BodyContent>{fallback}</BodyContent>;

const { theme: themeDarkLight } = useLayoutTheme();

const themeColorConfig = getConfig().theme;

// console.log('LayoutPage', { pathname, userAuth, isAdminUser, isLoggedIn });

console.log('LayoutPage', {
pathname,
userAuth: user,
isAdminUser,
isLoggedIn,
loginRequired,
isIndex,
});

const Redirect = () => {
if (loginRequired) {
if (
(isIndex && !user) ||
(!user &&
!publicRoutes.some((route) => (pathname || '').startsWith(route)))
) {
return <meta httpEquiv="refresh" content="0; url=/login" />;
}
} else {
if (isIndex) {
return null;
}

if (!publicRoutes.some((route) => (pathname || '').startsWith(route))) {
return <meta httpEquiv="refresh" content="0; url=/login" />;
}
}
return null;
};
return (
<Wrapper id="rootx">
{!userAuth &&
auth &&
pathname !== '/' &&
!publicRoutes.some((route) => (pathname || '').startsWith(route)) && (
<meta httpEquiv="refresh" content="0; url=/login" />
)}
<Redirect />

<CoreUIRoot appRootElementId="__next" theme={themeDarkLight} cssVars>
<AccentRegion
Expand Down Expand Up @@ -113,7 +139,7 @@ function LayoutPage({ children, ...props }: LayoutPageProps) {
>
{nav ? (
<>
<Nav user={userAuth} />
<Nav user={user} />
{auth ? (
// isAdmin
admin ? (
Expand Down Expand Up @@ -175,7 +201,7 @@ function WithLayoutProvider(props: LayoutPageProps) {

if (!initState) return null;

if (isAuth && !userAuth && loading) {
if (!userAuth && loading) {
return null;
}

Expand Down
2 changes: 0 additions & 2 deletions src/containers/SignIn/SignInFormPassword.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,6 @@ export function SignInFormPassword(props: SignInFormProps) {
}
}

console.log('newErrors', newErrors);

if (!isEmpty(newErrors)) {
return setErrors({ ...errors, ...newErrors });
}
Expand Down

0 comments on commit ba483a0

Please sign in to comment.