Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deploy: [DPCP-62] Alpha: Consolidate in www. (#28) #29

Merged
merged 12 commits into from
Aug 7, 2024
2 changes: 0 additions & 2 deletions .env.public
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
# @@@ WARNING: DON'T ADD NEXT_PUBLIC_* VARIABLES IF THEY'RE PRIVATE @@@

# env specific
NEXTAUTH_SECRET=random-string
NEXTAUTH_URL=http://localhost:3000
API_HOST_DEV=
API_HOST=http://localhost:3001

Expand Down
15 changes: 11 additions & 4 deletions lib/auth/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const methods = {
signIn: () => {},
signOut: async () => {
try {
const response = await fetch(`${process.env.NEXT_PUBLIC_NEXUS_HOST}/api/auth/signout`, {
const response = await fetch(`${process.env.NEXT_PUBLIC_NEXUS_HOST}/api/v1/auth/signout`, {
method: 'POST',
headers: {
Accept: 'application/json',
Expand All @@ -25,21 +25,28 @@ const methods = {
}
},
getCsrf: async () => {
console.log('GETTING CSTF');
try {
const response = await fetch(`${process.env.NEXT_PUBLIC_NEXUS_HOST}/api/auth/csrf`, { credentials: 'include' });
const response = await fetch(`${process.env.NEXT_PUBLIC_NEXUS_HOST}/api/v1/auth/csrf`, {
method: 'GET',
headers: {
Accept: 'application/json',
},
credentials: 'include',
});
const csrf = await response.json();
console.log({ response, csrf });
return csrf.csrfToken;
} catch (e) {
console.error(e);
}
},
getSession: async (params = { cookies: '' }) => {
try {
const response = await fetch(`${process.env.NEXT_PUBLIC_NEXUS_HOST}/api/auth/session`, {
const response = await fetch(`${process.env.NEXT_PUBLIC_NEXUS_HOST}/api/v1/auth/session`, {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
Cookie: params?.cookies,
},
credentials: 'include',
Expand Down
7 changes: 6 additions & 1 deletion next.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/** @type {import('next').NextConfig} */
const { withSentryConfig } = require('@sentry/nextjs');
const nextConfig = {
basePath: process.env.NEXUS_BASE_PATH,
assetPrefix: process.env.MAIN_URL || 'https://nyx.dreampip.com',
transpilePackages: ['next-auth'],
images: {
remotePatterns: [
Expand All @@ -23,6 +23,11 @@ const nextConfig = {
destination: '/services/rickmorty/list',
permanent: false,
},
// {
// source: '/signin',
// destination: '/dash/signin',
// permanent: false,
// },
];
},
};
Expand Down
4 changes: 3 additions & 1 deletion src/app/components/client/blocks/topnav-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ export const VTopNav = ({ user }: VTopNavProps) => {

// !make it isomorphic again with cookies
useEffect(() => {
getSession().then((session) => setUser(session?.user));
getSession().then((session) => {
setUser(session?.user)
});
}, []);

useEffect(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/app/components/client/elements/signin-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ export const VSignIn = ({ user }: VSignInProps) => {
</div>
);

return <DPButton onClick={() => navigate('/api/auth/signin')}>Sign in</DPButton>;
return <DPButton onClick={() => navigate('/api/v1/auth/signin')}>Sign in</DPButton>;
};
15 changes: 8 additions & 7 deletions src/app/components/client/elements/signup-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useContext, useEffect, useRef, useState } from 'react';
import { signIn, signOut, getCsrf } from "@auth";
import { AuthContext } from '@state';
import { ALogIn, ALogOut } from '@actions';
import { navigate, setCookie } from '@gateway';
import { navigate, setCookie, getCookie } from '@gateway';
import { Button, TextInput, Logo, Typography } from "@dreampipcom/oneiros";

interface IAuthProvider {
Expand All @@ -32,7 +32,7 @@ async function doSignIn() {


export const VSignUp = ({ providers, user }: VSignUpProps) => {
const [csrf, setCsrf] = useState();
const [csrf, setCsrf] = useState("");
const authContext = useContext(AuthContext);
const [isUserLoaded, loadUser] = ALogIn({});
const [, unloadUser] = ALogOut({});
Expand All @@ -45,7 +45,7 @@ export const VSignUp = ({ providers, user }: VSignUpProps) => {
const oauth = _providers.slice(1, providers.length)
const defaultP = _providers[0]

const signInUrl = '/api/auth/signin'
const signInUrl = '/api/v1/auth/signin'

const callbackUrl = process.env.NEXT_PUBLIC_NEXUS_BASE_PATH || "/"

Expand All @@ -54,10 +54,11 @@ export const VSignUp = ({ providers, user }: VSignUpProps) => {
const coercedName = name || user?.name || user?.email || "Young Padawan";

useEffect(() => {
getCsrf().then((_csrf) => {
setCsrf(_csrf);
setCookie({ name: '__Host-authjs.csrf-token', value: _csrf });
});
if(!csrf) {
const cookie = getCookie({ name: 'authjs.csrf-token' }).then((_csrf) => {
setCsrf(_csrf?.value || "");
});
}
}, [csrf]);

useEffect(() => {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
15 changes: 14 additions & 1 deletion src/app/gateway/client/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,18 @@ export async function navigate(url: string) {

export async function setCookie({ name, value }: { name: string; value: string }) {
// Set cookie
cookies().set(name, value);
const cookiesStore = cookies();
const currentCookies = cookiesStore.getAll();
console.log({ currentCookies });
cookiesStore.set(name, value, { secure: true });
console.log({ nextCookies: currentCookies });
}

export async function getCookie({ name }: { name: string }) {
// Set cookie
// const cookiesStore = cookies()
// const currentCookies = cookiesStore.getAll();
// console.log({ currentCookies })
return cookies().get(name);
// console.log({ nextCookies: currentCookies })
}
2 changes: 1 addition & 1 deletion src/app/gateway/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// index.ts

// client
export { navigate, setCookie } from './client/actions';
export { navigate, setCookie, getCookie } from './client/actions';

// server
export { getUser, loadChars, reloadChars, getChars } from './server/actions';
Expand Down
5 changes: 1 addition & 4 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import type { Metadata } from 'next';
import { DPTopNav } from '@blocks/server';
import { RootProviders } from '@state';
import { Inter } from 'next/font/google';
import './globals.css';

const inter = Inter({ subsets: ['latin'] });

export const metadata: Metadata = {
title: process.env.PATTERNS_TITLE,
description: process.env.PATTERNS_DESCRIPTION,
Expand All @@ -14,7 +11,7 @@ export const metadata: Metadata = {
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body className={inter.className}>
<body>
<RootProviders>
<DPTopNav />
{children}
Expand Down
14 changes: 14 additions & 0 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,24 @@ export const config = {
matcher: ['/api/:path*'],
};

const headers: Record<string, any> = {
'Access-Control-Allow-Origin': process.env.MAIN_URL || 'https://www.dreampip.com',
'Cache-Control': 'maxage=0, s-maxage=300, stale-while-revalidate=300',
// DEV-DEBUG:
// 'content-type': 'application/json',
// 'Access-Control-Allow-Origin': 'http://localhost:2999',
'Access-Control-Allow-Credentials': 'true',
'Access-Control-Allow-Headers': 'baggage, sentry-trace',
};

export function middleware(request: NextRequest) {
const response = NextResponse.next();
const pkce = request.cookies.get('next-auth.pkce.code_verifier');

Object.keys(headers).forEach((key: string) => {
response.headers.set(key, headers[key]);
});

if (pkce?.value) {
response.cookies.set('next-auth.pkce.code_verifier', pkce.value, {
httpOnly: true,
Expand Down
Loading