Skip to content

Commit

Permalink
adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
Admiralfeb committed Sep 26, 2024
1 parent 48fcea1 commit 91fcfef
Show file tree
Hide file tree
Showing 32 changed files with 880 additions and 550 deletions.
1,130 changes: 738 additions & 392 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"fix-react-default-imports": "npx react-codemod update-react-imports"
},
"dependencies": {
"@auth/mongodb-adapter": "^3.5.2",
"@emotion/cache": "^11.13.1",
"@emotion/react": "^11.13.3",
"@emotion/server": "^11.11.0",
Expand All @@ -23,17 +24,16 @@
"@mui/material-nextjs": "^6.1.1",
"@mui/styles": "^6.0.0",
"@mui/x-date-pickers": "^7.16.0",
"@next-auth/mongodb-adapter": "^1.1.3",
"axios": "^1.7.7",
"bson": "^4.5.2",
"clsx": "^1.1.1",
"dayjs": "^1.11.13",
"gray-matter": "^4.0.3",
"luxon": "^3.5.0",
"moment": "^2.29.1",
"mongodb": "^4.17.2",
"mongodb": "^6.9.0",
"next": "^14.2.9",
"next-auth": "^4.24.7",
"next-auth": "^5.0.0-beta.21",
"next-pwa": "^5.6.0",
"nodemailer": "^6.9.15",
"notistack": "^3.0.1",
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 6 additions & 0 deletions src/app/admin/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { auth } from '@/auth';
import { ReactNode } from 'react';
export default async function AdminLayout({ children }: { children: ReactNode }) {
const session = await auth();
return <>{children}</>;
}
16 changes: 7 additions & 9 deletions src/pages/admin/index.page.tsx → src/app/admin/page.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { runAdminAuthCheck } from '@/utils/runAuthCheck';
import { AdminDashboard } from '@@/admin/components/adminDashboard';
import { GetServerSideProps } from 'next';
import Head from 'next/head';
import { redirect, RedirectType } from 'next/navigation';

const AdminPage = () => {
export default async function AdminPage() {
const authResult = await runAdminAuthCheck('admin_index');
if (authResult.redirect) {
redirect(authResult.destination, RedirectType.replace);
}
return (
<>
<Head>
Expand All @@ -13,10 +17,4 @@ const AdminPage = () => {
<AdminDashboard />
</>
);
};

export default AdminPage;

export const getServerSideProps: GetServerSideProps = async (context) => {
return runAdminAuthCheck(context, 'admin_index');
};
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
14 changes: 12 additions & 2 deletions src/app/api/builds/getBuilds.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import { IBuildInfov2 } from '@/app/builds/_models';
import { getItems } from '@/utils/db';
import { getItems, getItemsByQuery } from '@/utils/db';

const COLLECTION = 'shipBuildsv2';
export const getBuilds = async () => {
const items = await getItems<IBuildInfov2>(COLLECTION, 'shipId', 1);
console.log({ items });
// console.log({ items });
return items;
};

export const getBuildById = async (id: string) => {
const items = await getItemsByQuery<IBuildInfov2>(COLLECTION, { _id: id });

if (items.length > 0) {
return items[0];
} else {
return null;
}
};
2 changes: 2 additions & 0 deletions src/app/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { handlers } from '@/auth';
export const { GET, POST } = handlers;
9 changes: 3 additions & 6 deletions src/pages/auth/error.page.tsx → src/app/auth/error/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Container, Paper, Typography } from '@mui/material';

const AuthErrorPage = () => {
export default function AuthErrorPage() {
return (
<Container maxWidth="xs">
<Paper
Expand All @@ -10,14 +10,11 @@ const AuthErrorPage = () => {
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
}}
>
}}>
<Typography component="h1" variant="h5">
Sign In Failed
</Typography>
</Paper>
</Container>
);
};

export default AuthErrorPage;
}
27 changes: 27 additions & 0 deletions src/app/auth/not-authorized/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Box, Container, Link, Paper, Typography } from '@mui/material';
import Image from 'next/image';
import NextLink from 'next/link';
import notFoundImg from 'public/img/404.jpg';

export default function NotAuthorizedPage() {
return (
<Container maxWidth="lg" sx={{ textAlign: 'center', marginTop: 1 }}>
<Paper>
<Typography variant="h3">Not Authorized</Typography>
<Typography variant="subtitle1">
You were hyperdicted by the auth patrol... escape destruction by going{' '}
<NextLink href="/home" passHref>
<Link>home</Link>
</NextLink>
.
</Typography>
<Box
sx={{ maxWidth: '100%', height: 'auto' }}
component={Image}
alt="404 Error"
src={notFoundImg}
/>
</Paper>
</Container>
);
}
26 changes: 26 additions & 0 deletions src/app/auth/signIn/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { auth } from '@/auth';
import { redirects } from '@/data/redirects';
import { redirect } from 'next/navigation';
import { SignInForm } from './signIn-form';

async function redirectIfLoggedIn(redirectKey: string) {
const session = await auth();
if (session) {
const redirectPath = redirects.find((x) => x.key === redirectKey)?.path;
if (redirectPath) {
redirect(redirectPath);
} else {
redirect('/');
}
}
}

export default async function SignInPage({
searchParams,
}: {
searchParams: { [key: string]: string | string[] | undefined };
}) {
await redirectIfLoggedIn(searchParams['redirect'] as string);

return <SignInForm />;
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { redirects } from '@/data/redirects';
'use client';
import { signIn } from '@/auth';
import { LockOutlined } from '@mui/icons-material';
import { Avatar, Box, Button, Container, Paper, TextField } from '@mui/material';
import { GetServerSideProps } from 'next';
import { getSession, signIn } from 'next-auth/react';
import { Container, Paper, Avatar, Box, TextField, Button } from '@mui/material';
import { useForm } from 'react-hook-form';

const SignInPage = () => {
export function SignInForm() {
const { register, handleSubmit } = useForm<{ email: string }>({
defaultValues: { email: '' },
});
Expand Down Expand Up @@ -40,7 +39,6 @@ const SignInPage = () => {
required
fullWidth
label="Email Address"
name="email"
autoComplete="email"
autoFocus
{...register('email', { required: 'Your email is required' })}
Expand All @@ -61,25 +59,4 @@ const SignInPage = () => {
</Paper>
</Container>
);
};

export default SignInPage;

export const getServerSideProps: GetServerSideProps = async (context) => {
const session = await getSession(context);
if (session) {
const redirectKey = context.query.redirect as string;
const redirectPath = redirects.find((x) => x.key === redirectKey)?.path;
if (redirectPath) {
return {
redirect: { destination: redirectPath, permanent: false },
};
} else {
return {
redirect: { destination: '/home', permanent: false },
};
}
}

return { props: {} };
};
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Container, Paper, Typography } from '@mui/material';

const VerifyRequestPage = () => {
export default function VerifyRequestPage() {
return (
<Container maxWidth="xs">
<Paper
Expand All @@ -11,14 +11,11 @@ const VerifyRequestPage = () => {
flexDirection: 'column',
alignItems: 'center',
textAlign: 'center',
}}
>
}}>
<Typography component="h1" variant="h5">
Check your email for a 'magic' link to sign in.
</Typography>
</Paper>
</Container>
);
};

export default VerifyRequestPage;
}
4 changes: 3 additions & 1 deletion src/app/builds/_providers/buildProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getShipInfofromID } from '@/app/builds/_functions/getShipInfo';
import { useShipBuilds } from '@/app/builds/_hooks/useShipBuilds';
import { IBuildInfov2, IShipInfo } from '@/app/builds/_models';
import { useSnackbar } from 'notistack';
import { createContext, ReactNode, useState } from 'react';
import { createContext, ReactNode, useContext, useState } from 'react';

/**
* Add Build Function. Used in the Build Provider to trigger the dialog.
Expand Down Expand Up @@ -43,6 +43,8 @@ interface IBuildContext {

export const BuildContext = createContext<IBuildContext | null>(null);

export const useBuildService = () => useContext(BuildContext);

export const BuildContextProvider = ({
children,
init,
Expand Down
9 changes: 6 additions & 3 deletions src/app/builds/detail/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import Head from 'next/head';
import { getBuilds } from 'src/pages/api/builds.api';
import { getBuildById, getBuilds } from 'src/app/api/builds/getBuilds';
import { BuildDetail } from '../../_components/builds/buildDetail';

export const revalidate = 60;
export const dynamicParams = true;

export async function generateStaticParams() {
const builds = await getBuilds();

Expand All @@ -10,8 +13,8 @@ export async function generateStaticParams() {
return paths;
}

export default async function BuildDetailPage() {
const builds = await getBuilds();
export default async function BuildDetailPage({ params }: { params: { id: string } }) {
const builds = await getBuildById(params.id);
return (
<>
<Head>
Expand Down
2 changes: 1 addition & 1 deletion src/app/builds/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getBuilds } from '#/builds.api';
import { getBuilds } from '../api/builds/getBuilds';
import { BuildSystem } from '@/app/builds/_components';
import Head from 'next/head';

Expand Down
Binary file added src/app/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions src/auth.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { NextAuthConfig } from 'next-auth';
import SendGrid from 'next-auth/providers/sendgrid';

export const authConfig: NextAuthConfig = {
providers: [SendGrid({ from: process.env.EMAIL_FROM })],
};
29 changes: 9 additions & 20 deletions src/pages/api/auth/[...nextauth].api.ts → src/auth.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
import mongoClient from '@/lib/db';
import { ICMDR } from '@@/admin/models/cmdr';
import { MongoDBAdapter } from '@next-auth/mongodb-adapter';
import { MongoDBAdapter } from '@auth/mongodb-adapter';
import NextAuth from 'next-auth';
import EmailProvider from 'next-auth/providers/email';
import { authConfig } from './auth.config';

export default NextAuth({
export const { auth, handlers, signIn, signOut } = NextAuth({
// https://next-auth.js.org/configuration/providers
providers: [
EmailProvider({
server: {
host: process.env.EMAIL_SERVER,
port: 465,
auth: {
user: process.env.EMAIL_USER,
pass: process.env.EMAIL_PASS,
},
},
from: process.env.EMAIL_FROM,
}),
],
...authConfig,
// Database optional. MySQL, Maria DB, Postgres and MongoDB are supported.
// https://next-auth.js.org/configuration/databases
//
Expand Down Expand Up @@ -70,12 +58,13 @@ export default NextAuth({
callbacks: {
signIn: async ({ user }) => {
const db = (await mongoClient).db();
const email: string = user.email;

const email: string | null | undefined = user.email;

const cursor = db.collection<ICMDR>('cmdrs').find({});
const members = await cursor.toArray();
cursor.close();
const authUser = members.find((x) => x.email.toLowerCase() === email.toLowerCase());
const authUser = members.find((x) => x.email?.toLowerCase() === email?.toLowerCase());

return authUser ? true : false;
},
Expand All @@ -85,11 +74,11 @@ export default NextAuth({
if (user) {
const db = (await mongoClient).db();

const email: string = user.email;
const email: string | undefined | null = user.email;
const cursor = db.collection<ICMDR>('cmdrs').find({});
const members = await cursor.toArray();
cursor.close();
const authUser = members.find((x) => x.email.toLowerCase() === email);
const authUser = members.find((x) => x.email?.toLowerCase() === email?.toLowerCase());
token = { ...token, ...authUser };
}
return token;
Expand Down
9 changes: 0 additions & 9 deletions src/models/auth/user.ts

This file was deleted.

4 changes: 0 additions & 4 deletions src/models/location.ts

This file was deleted.

4 changes: 0 additions & 4 deletions src/models/loginProviders.ts

This file was deleted.

1 change: 1 addition & 0 deletions src/modules/admin/components/adminDashboard.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client';
import { Button, ButtonProps, Container, Paper, styled, Typography } from '@mui/material';
import NextLink from 'next/link';

Expand Down
Loading

0 comments on commit 91fcfef

Please sign in to comment.