Skip to content

Commit

Permalink
wip: refactor middlewares
Browse files Browse the repository at this point in the history
  • Loading branch information
majkshkurti committed Sep 14, 2023
1 parent c323cf4 commit 610567d
Show file tree
Hide file tree
Showing 22 changed files with 556 additions and 225 deletions.
20 changes: 14 additions & 6 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,21 @@
NEXT_PUBLIC_APP_URL=http://localhost:3000
NEXT_PUBLIC_API_URL=http://127.0.0.1:8081

############
# GOAT #
############
NEXT_PUBLIC_MAP_TOKEN=
ACCOUNTS_API_URL=
GEOAPI_URL=
ANALYSIS_URL=

###############
# NEXT AUTH #
###############

# @see: https://next-auth.js.org/providers/keycloak
NEXTAUTH_URL=http://localhost:3000
NEXTAUTH_SECRET=secret
KEYCLOAK_ISSUER=https://auth.dev.plan4better.de/realms/p4b
KEYCLOAK_CLIENT_ID=account
KEYCLOAK_CLIENT_SECRET=secret
NEXTAUTH_URL=
NEXTAUTH_SECRET=
KEYCLOAK_ISSUER=
KEYCLOAK_CLIENT_ID=
KEYCLOAK_CLIENT_SECRET=

2 changes: 1 addition & 1 deletion .github/workflows/nextjs-bundle-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ jobs:
id: fc
with:
issue-number: ${{ github.event.number }}
body-includes: "<!-- __NEXTJS_BUNDLE_@p4b/goat -->"
body-includes: "<!-- __NEXTJS_BUNDLE_@p4b/web -->"

- name: 💬 Create Comment
uses: peter-evans/create-or-update-comment@v1.4.4
Expand Down
2 changes: 1 addition & 1 deletion apps/goat/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ WORKDIR /app
RUN npm install -g turbo
COPY . .
COPY .env.docker /app/apps/goat/.env
RUN turbo prune --scope=@p4b/goat --docker
RUN turbo prune --scope=@p4b/web --docker

# Add lockfile and package.json's of isolated subworkspace
FROM base AS installer
Expand Down
2 changes: 1 addition & 1 deletion apps/web/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ WORKDIR /app
RUN npm install -g turbo
COPY . .
COPY .env.docker /app/apps/goat/.env
RUN turbo prune --scope=@p4b/goat --docker
RUN turbo prune --scope=@p4b/web --docker

# Add lockfile and package.json's of isolated subworkspace
FROM base AS installer
Expand Down
127 changes: 9 additions & 118 deletions apps/web/app/[lng]/auth/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,128 +1,19 @@
"use client";

import { makeStyles } from "@/lib/theme";
import { useSession } from "next-auth/react";

import Box from "@p4b/ui/components/Box";
import Grid from "@p4b/ui/components/Grid";
import { Loading } from "@p4b/ui/components/Loading";
import Image from "next/image";
import AuthLayout from "@p4b/ui/components/AuthLayout";

interface AuthLayoutProps {
children: React.ReactNode;
}

const AuthLayout = (props: AuthLayoutProps) => {
export default function Layout({ children }: { children: React.ReactNode }) {
const { status, data: session } = useSession();
const { children } = props;
const { classes } = useStyles();

return (
<div className={classes.root}>
<Grid container className={classes.gridContainer}>
<Grid xs={12} lg={6} className={classes.gridLeft}>
<Box
component="header"
sx={{
left: 0,
p: 3,
position: "fixed",
top: 0,
width: "100%",
}}>
<Box
component="a"
href="https://www.plan4better.de/"
target="_blank"
sx={{
display: "inline-flex",
width: 160,
}}>
<Image
width="100"
src="https://assets.plan4better.de/img/logo/plan4better_standard.svg"
alt="Plan4Better Logo"
/>
</Box>
</Box>

<div className={classes.child}>
<div className={classes.cardContainer}>
{!status ||
session?.error === "RefreshAccessTokenError" ||
(["unauthenticated", "loading"].includes(status) && (
<div className={classes.root}>
<div className={classes.loadingWrapper}>
<Loading />
</div>
</div>
))}
{children}
</div>
</div>
</Grid>
<Grid xs={12} lg={6} className={classes.gridRight}>
<Box sx={{ p: 3, width: 350 }} component="div">
<Image
width="100"
src="https://assets.plan4better.de/img/logo/goat_white.svg"
alt="Plan4Better Logo"
/>
</Box>
</Grid>
</Grid>
</div>
<AuthLayout>
{!status ||
session?.error === "RefreshAccessTokenError" ||
(["unauthenticated", "loading"].includes(status) && <Loading />)}
{children}
</AuthLayout>
);
};

const useStyles = makeStyles({ name: { AuthLayout } })((theme) => ({
root: {
display: "flex",
},
gridContainer: {
flex: "1 1 auto",
},
cardContainer: {
display: "flex",
justifyContent: "center",
},
child: {
width: "100%",
height: "100%",
display: "flex",
flexDirection: "column",
justifyContent: "center",
backgroundColor: theme.colors.useCases.surfaces.background,
},
image: {
width: "50%",
backgroundImage: 'url("https://assets.plan4better.de/img/login/artwork_1.png")',
height: "100vh",
},
gridLeft: {
height: "100vh",
display: "flex",
flexDirection: "column",
backgroundColor: theme.colors.useCases.surfaces.background,
},
gridRight: {
alignItems: "center",
background:
"radial-gradient(50% 50% at 50% 50%, rgba(40,54,72,0.8) 0%, rgba(40,54,72,0.9) 100%), url(https://assets.plan4better.de/img/login/artwork_1.png) no-repeat center",
backgroundSize: "cover",
color: "white",
display: "flex",
justifyContent: "center",
"& img": {
maxWidth: "100%",
},
},
loadingWrapper: {
justifyContent: "center",
marginTop: theme.spacing(4),
marginBottom: theme.spacing(4),
display: "flex",
},
}));

export default AuthLayout;
}
16 changes: 13 additions & 3 deletions apps/web/app/[lng]/auth/login/page.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
"use client";

import { useTheme } from "@mui/material";
import { signIn, useSession } from "next-auth/react";
import { redirect } from "next/navigation";

export default function Login() {
const { status, data: session } = useSession();
console.log("session", session);
if (status === "unauthenticated" || session?.error === "RefreshAccessTokenError") {
signIn("keycloak");
const theme = useTheme();
if (
status === "unauthenticated" ||
session?.error === "RefreshAccessTokenError"
) {
signIn(
"keycloak",
{},
{
theme: theme.palette.mode,
},
);
}
if (session && session?.error !== "RefreshAccessTokenError") {
redirect(`/`);
Expand Down
8 changes: 3 additions & 5 deletions apps/web/app/[lng]/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { languages } from "@/app/i18/settings";
import ThemeRegistry from "@/lib/ThemeRegistry";
import ThemeRegistry from "@/components/@mui/ThemeRegistry";
import AuthProvider from "@/lib/providers/AuthProvider";
import ToastProvider from "@/lib/providers/ToastProvider";
import StoreProvider from "@/lib/providers/StoreProvider";
Expand Down Expand Up @@ -27,11 +27,9 @@ export default function RootLayout({
children: React.ReactNode;
params: { lng: string };
}) {


return (
<html lang={lng} dir={dir(lng)}>
<body style={{background: '#FAFAFA'}}>
<body>
<StoreProvider>
<AuthProvider>
<ToastProvider>
Expand All @@ -42,4 +40,4 @@ export default function RootLayout({
</body>
</html>
);
}
}
118 changes: 115 additions & 3 deletions apps/web/app/[lng]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,117 @@
import { redirect } from "next/navigation";
// import { redirect } from "next/navigation";

export default async function Home() {
return redirect(`/home`);
// export default async function Home() {
// return redirect(`/home`);
// }
import * as React from "react";
import Box from "@mui/material/Box";
import Grid from "@mui/material/Unstable_Grid2";
import Drawer from "@mui/material/Drawer";
import Typography from "@mui/material/Typography";
import List from "@mui/material/List";
import ListItem from "@mui/material/ListItem";
import Alert from "@mui/material/Alert";
import AlertTitle from "@mui/material/AlertTitle";
import Image from "next/image";
import Card from "@mui/material/Card";
import CardActions from "@mui/material/CardActions";
import CardContent from "@mui/material/CardContent";
import Button from "@mui/material/Button";

function MediaCard({
heading,
text,
}: {
heading: string;
text: string;
}) {
return (
<Card>
<Image
alt="Random image"
src="https://source.unsplash.com/random"
width={640}
height={480}
style={{
maxWidth: "100%",
height: "200px",
objectFit: "cover",
}}
/>
<CardContent>
<Typography gutterBottom variant="h5" component="div">
{heading}
</Typography>
<Typography variant="body2" color="text.secondary">
{text}
</Typography>
</CardContent>
<CardActions>
<Button size="small">Share</Button>
<Button size="small">Learn More</Button>
</CardActions>
</Card>
);
}

export default function HomePage() {
return (
<Box sx={{ display: "flex" }}>
<div>
<Alert severity="info" sx={{ mt: 2, mb: 5 }}>
<AlertTitle>Hello 👋</AlertTitle>
This app uses the Next.js App Router and Material UI v5.
</Alert>
<Grid container rowSpacing={3} columnSpacing={3}>
<Grid xs={6}>
<MediaCard
heading="CMYK"
text="The CMYK color model (also known as process color, or four color) is a subtractive color model, based on the CMY color model, used in color printing, and is also used to describe the printing process itself."
/>
</Grid>
<Grid xs={6}>
<MediaCard
heading="HSL and HSV"
text="HSL (for hue, saturation, lightness) and HSV (for hue, saturation, value; also known as HSB, for hue, saturation, brightness) are alternative representations of the RGB color model, designed in the 1970s by computer graphics researchers."
/>
</Grid>
<Grid xs={6}>
<MediaCard
heading="RGB"
text="An RGB color space is any additive color space based on the RGB color model. RGB color spaces are commonly found describing the input signal to display devices such as television screens and computer monitors."
/>
</Grid>
<Grid xs={6}>
<MediaCard
heading="CIELAB"
text="The CIELAB color space, also referred to as L*a*b*, was intended as a perceptually uniform space, where a given numerical change corresponds to a similar perceived change in color."
/>
</Grid>
</Grid>
</div>
<Drawer
sx={{
width: 320,
flexShrink: 0,
"& .MuiDrawer-paper": {
width: 320,
boxSizing: "border-box",
top: ["48px", "56px", "64px"],
height: "auto",
bottom: 0,
},
}}
variant="permanent"
anchor="right"
>
<List sx={{ px: 2 }}>
<ListItem disablePadding>
<Typography variant="overline" sx={{ fontWeight: 500 }}>
On this page
</Typography>
</ListItem>
</List>
</Drawer>
</Box>
);
}
7 changes: 0 additions & 7 deletions apps/web/app/api/auth/[...nextauth]/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,6 @@ async function refreshAccessToken(token: JWT): Promise<JWT> {
export const options: NextAuthOptions = {
secret: process.env.NEXTAUTH_SECRET,
providers: [keycloak],
pages: {
signIn: "/auth/login",
error: "/auth/error",
},
theme: {
colorScheme: "light",
},
callbacks: {
async session({ session, token }) {
if (token) {
Expand Down
1 change: 1 addition & 0 deletions apps/web/app/i18/settings.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export const fallbackLng = "en";
export const languages = [fallbackLng, "de"];
export const defaultNS = "translation";
export const cookieName = 'i18next'

export function getOptions(lng = fallbackLng, ns = defaultNS) {
return {
Expand Down
Loading

0 comments on commit 610567d

Please sign in to comment.