Move packages/apps back

This commit is contained in:
Darren Clarke 2023-03-10 08:26:51 +00:00
parent 6eaaf8e9be
commit 5535d6b575
348 changed files with 0 additions and 0 deletions

View file

@ -0,0 +1,13 @@
import "../styles/globals.css";
import { AppProps } from "next/app";
import { SessionProvider } from "next-auth/react";
function MetamigoStarter({ Component, pageProps }: AppProps) {
return (
<SessionProvider session={pageProps.session}>
<Component {...pageProps} />
</SessionProvider>
);
}
export default MetamigoStarter;

View file

@ -0,0 +1,15 @@
import { ApolloProvider } from "@apollo/client";
import { apolloClient } from "../lib/apollo-client";
import dynamic from "next/dynamic";
const MetamigoAdmin = dynamic(() => import("../components/MetamigoAdmin"), {
ssr: false,
});
export default function Home() {
return (
<ApolloProvider client={apolloClient}>
<MetamigoAdmin />
</ApolloProvider>
);
}

View file

@ -0,0 +1,106 @@
import { NextApiRequest, NextApiResponse } from "next";
import NextAuth from "next-auth";
import Google from "next-auth/providers/google";
import GitHub from "next-auth/providers/github";
import GitLab from "next-auth/providers/gitlab";
import Cognito from "next-auth/providers/cognito";
import { loadConfig, IAppConfig } from "config";
import { MetamigoAdapter } from "../../../lib/nextauth-adapter";
import { CloudflareAccessProvider } from "../../../lib/cloudflare";
const nextAuthOptions = (config: IAppConfig, req: NextApiRequest) => {
const { nextAuth, cfaccess } = config;
const adapter = MetamigoAdapter(config);
const providers = [];
const { audience, domain } = cfaccess;
const cloudflareAccessEnabled = audience && domain;
if (cloudflareAccessEnabled)
providers.push(CloudflareAccessProvider(audience, domain, adapter, req));
else {
if (nextAuth.google?.id)
providers.push(
Google({
clientId: nextAuth.google.id,
clientSecret: nextAuth.google.secret,
})
);
if (nextAuth.github?.id)
providers.push(
GitHub({
clientId: nextAuth.github.id,
clientSecret: nextAuth.github.secret,
})
);
if (nextAuth.gitlab?.id)
providers.push(
GitLab({
clientId: nextAuth.gitlab.id,
clientSecret: nextAuth.gitlab.secret,
})
);
if (nextAuth.cognito?.id)
providers.push(
Cognito({
clientId: nextAuth.cognito.id,
clientSecret: nextAuth.cognito.secret,
// domain: nextAuth.cognito.domain,
})
);
}
if (providers.length === 0)
throw new Error(
"No next-auth providers configured. See Metamigo configuration docs."
);
return {
secret: nextAuth.secret,
session: {
jwt: true,
maxAge: 8 * 60 * 60, // 8 hours
},
jwt: {
secret: nextAuth.secret,
encryption: false,
signingKey: nextAuth.signingKey,
encryptionKey: nextAuth.encryptionKey,
},
providers,
adapter,
callbacks: {
session: async (session: any, token: any) => {
// make the user id available in the react client
session.user.id = token.userId;
return session;
},
jwt: async (token: any, user: any) => {
const isSignIn = Boolean(user);
// Add auth_time to token on signin in
if (isSignIn) {
// not sure what this does
// if (!token.aud) token.aud;
token.aud = nextAuth.audience;
token.picture = user.avatar;
token.userId = user.id;
token.role = user.userRole ? `app_${user.userRole}` : "app_anonymous";
}
return token;
},
},
};
};
const nextAuth = async (
req: NextApiRequest,
res: NextApiResponse
): Promise<void> =>
// @ts-expect-error: Type mismatch
NextAuth(req, res, nextAuthOptions(await loadConfig(), req));
export default nextAuth;

View file

@ -0,0 +1,38 @@
import { createProxyMiddleware } from "http-proxy-middleware";
export default createProxyMiddleware({
target:
process.env.NODE_ENV === "production"
? "http://metamigo-api:3001"
: "http://localhost:3001",
changeOrigin: true,
pathRewrite: { "^/graphql": "/graphql" },
xfwd: true,
onProxyReq: function (proxyReq, req, _res) {
const auth = proxyReq.getHeader("authorization");
if (auth) {
// pass along user provided authorization header
return;
}
// Else extract the session token from the cookie and pass
// as bearer token to the proxy target
let token = req.cookies["__Secure-next-auth.session-token"];
if (!token) token = req.cookies["next-auth.session-token"];
//console.log(req.body);
//if (req.body.query) console.log(req.body.query);
if (token) {
proxyReq.setHeader("authorization", `Bearer ${token}`);
proxyReq.removeHeader("cookie");
} else {
console.error("no token found. proxied request to backend will fail.");
}
},
});
export const config = {
api: {
bodyParser: false,
},
};

View file

@ -0,0 +1,38 @@
import { createProxyMiddleware } from "http-proxy-middleware";
export default createProxyMiddleware({
target:
process.env.NODE_ENV === "production"
? "http://metamigo-api:3001"
: "http://localhost:3001",
changeOrigin: true,
pathRewrite: { "^/api/v1": "/api" },
xfwd: true,
onProxyReq: function (proxyReq, req, res) {
const auth = proxyReq.getHeader("authorization");
if (auth) {
// pass along user provided authorization header
return;
}
// Else extract the session token from the cookie and pass
// as bearer token to the proxy target
//const token = req.cookies["next-auth.session-token"];
let token = req.cookies["__Secure-next-auth.session-token"];
if (!token) token = req.cookies["next-auth.session-token"];
if (token) {
proxyReq.setHeader("authorization", `Bearer ${token}`);
proxyReq.removeHeader("cookie");
} else {
console.error("no token found. proxied request to backend will fail.");
}
return;
},
});
export const config = {
api: {
bodyParser: false,
},
};

View file

@ -0,0 +1,29 @@
import { NextPage } from "next";
import { Typography, Box, Button, Grid, Link } from "@material-ui/core";
import { FC, PropsWithChildren, useEffect } from "react";
import { useRouter } from "next/router";
export const RedirectToAdmin: FC<PropsWithChildren> = ({ children }) => {
const router = useRouter();
useEffect(() => {
router.push("/admin");
});
return <>{children}</>;
};
const Home: NextPage = () => (
<Box>
<Typography variant="h3">Metamigo</Typography>
<Grid container justify="space-around" style={{ padding: 60 }}>
<Grid item>
<Link href="/admin">
<Button variant="contained">Admin</Button>
<RedirectToAdmin />
</Link>
</Grid>
</Grid>
</Box>
);
export default Home;

View file

@ -0,0 +1,27 @@
import { Button } from "@material-ui/core";
import { signIn, signOut, useSession } from "next-auth/react";
export default function myComponent() {
const { data: session } = useSession();
return (
<>
{!session && (
<>
Not signed in <br />
<Button variant="contained" onClick={signIn as any}>
Sign in
</Button>
</>
)}
{session && (
<>
Signed in as {session.user?.email} <br />
<Button variant="contained" onClick={signOut as any}>
Sign out
</Button>
</>
)}
</>
);
}