Skip to content

Commit

Permalink
Merge pull request #68 from esc-chula/fix/fix-runtime-env
Browse files Browse the repository at this point in the history
fix runtime env web
  • Loading branch information
chickzilla authored Nov 1, 2024
2 parents 055783d + 2da1665 commit 4f9070a
Show file tree
Hide file tree
Showing 14 changed files with 207 additions and 119 deletions.
3 changes: 2 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ dist
.gitignore
docker-compose.yml
**/.next
**/.env
**/.env
**/.env.local
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ INTANIA_AUTH_APP_ID=
INTANIA_AUTH_SECRET=
INTANIA_AUTH_REDIRECT_URL=

JWT_SECRET=
JWT_SECRET=

NEXT_PUBLIC_NODE_ENV=
9 changes: 0 additions & 9 deletions .github/workflows/build-web.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ on:

env:
IMAGE_NAME: ${{ github.repository }}-web
#NEXT_PUBLIC_API_SERVER_URL: ${{ secrets.NEXT_PUBLIC_API_SERVER_URL }}
#INTANIA_AUTH_APP_ID: ${{ secrets.INTANIA_AUTH_APP_ID }}
#INTANIA_AUTH_REDIRECT_URL: ${{ secrets.INTANIA_AUTH_REDIRECT_URL }}
#JWT_SECRET: ${{ secrets.JWT_SECRET }}

jobs:
build:
Expand Down Expand Up @@ -56,11 +52,6 @@ jobs:
echo "tags = $RESULT"
echo "tags=$RESULT" >> "$GITHUB_OUTPUT"
#- name: Create env File
# run: |
# touch .env
# echo NEXT_PUBLIC_API_SERVER_URL=${{ secrets.NEXT_PUBLIC_API_SERVER_URL }} >> .env

- name: Build Docker image with multiple tags
run: |
tags="${{ steps.tags.outputs.tags }}"
Expand Down
1 change: 1 addition & 0 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"moment": "^2.30.1",
"moment-timezone": "^0.5.45",
"next": "^14.2.1",
"next-runtime-env": "^3.2.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-hook-form": "^7.51.5",
Expand Down
54 changes: 28 additions & 26 deletions apps/web/src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,47 +1,48 @@
import "../styles/globals.css";
import { Inter } from "next/font/google";
import localFont from "next/font/local";
import '../styles/globals.css';
import { Inter } from 'next/font/google';
import localFont from 'next/font/local';
import { PublicEnvScript } from 'next-runtime-env';

const inter = Inter({ subsets: ["latin"] });
const inter = Inter({ subsets: ['latin'] });
const sukhumvitSet = localFont({
src: [
{
path: "../../public/fonts/SukhumvitSet-Bold.ttf",
weight: "700",
style: "normal",
path: '../../public/fonts/SukhumvitSet-Bold.ttf',
weight: '700',
style: 'normal',
},
{
path: "../../public/fonts/SukhumvitSet-SemiBold.ttf",
weight: "600",
style: "normal",
path: '../../public/fonts/SukhumvitSet-SemiBold.ttf',
weight: '600',
style: 'normal',
},
{
path: "../../public/fonts/SukhumvitSet-Medium.ttf",
weight: "500",
style: "normal",
path: '../../public/fonts/SukhumvitSet-Medium.ttf',
weight: '500',
style: 'normal',
},
{
path: "../../public/fonts/SukhumvitSet-Text.ttf",
weight: "400",
style: "normal",
path: '../../public/fonts/SukhumvitSet-Text.ttf',
weight: '400',
style: 'normal',
},
{
path: "../../public/fonts/SukhumvitSet-Light.ttf",
weight: "300",
style: "normal",
path: '../../public/fonts/SukhumvitSet-Light.ttf',
weight: '300',
style: 'normal',
},
{
path: "../../public/fonts/SukhumvitSet-Thin.ttf",
weight: "100",
style: "normal",
path: '../../public/fonts/SukhumvitSet-Thin.ttf',
weight: '100',
style: 'normal',
},
],
variable: "--sukhumvit-set-font",
variable: '--sukhumvit-set-font',
});

export const metadata = {
title: "ESC Project Tracker",
description: "ESC Project Tracker",
title: 'ESC Project Tracker',
description: 'ESC Project Tracker',
};

export default function RootLayout({
Expand All @@ -53,8 +54,9 @@ export default function RootLayout({
<html lang="en">
<head>
<link rel="icon" type="x-icon" href="/icons/esc.svg" />
<PublicEnvScript />
</head>
<body className={inter.className + " " + sukhumvitSet.className}>
<body className={inter.className + ' ' + sukhumvitSet.className}>
{children}
</body>
</html>
Expand Down
4 changes: 2 additions & 2 deletions apps/web/src/app/login/route.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { env } from '@/src/env';
import { env } from 'next-runtime-env';
import { redirect } from 'next/navigation';

export async function GET() {
redirect(
`https://account.intania.org/?appId=${env.INTANIA_AUTH_APP_ID}&callbackUrl=${env.INTANIA_AUTH_REDIRECT_URL}`,
`https://account.intania.org/?appId=${env('INTANIA_AUTH_APP_ID')}&callbackUrl=${env('INTANIA_AUTH_REDIRECT_URL')}`,
);
}
14 changes: 1 addition & 13 deletions apps/web/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,9 @@ import OnboardingPageHeader from '@/src/components/header/OnboardingPageHeader';
import { Button } from '@/src/components/ui/button';
import Image from 'next/image';
import Link from 'next/link';
import { env } from '../env';
import { env } from 'next-runtime-env';

export default function Page() {
console.log(
'test',
env.NEXT_PUBLIC_API_SERVER_URL,
env.INTANIA_AUTH_REDIRECT_URL,
);

return (
<>
<main className="flex flex-col items-center">
Expand All @@ -29,12 +23,6 @@ export default function Page() {
ครบจบทุกกระบวนการ มาเริ่มต้นเอกสารกัน !
</h2>
</div>
<p>
debugs: {env.NEXT_PUBLIC_API_SERVER_URL}{' '}
{process.env.NEXT_PUBLIC_API_SERVER_URL}{' '}
{process.env.INTANIA_AUTH_REDIRECT_URL}{' '}
{env.INTANIA_AUTH_REDIRECT_URL}
</p>
<Button
className="rounded-[80px] bg-gradient-red text-2xl font-bold px-12 py-[10px] h-16"
asChild
Expand Down
6 changes: 3 additions & 3 deletions apps/web/src/app/trpc.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { createTRPCProxyClient, httpBatchLink, loggerLink } from '@trpc/client';
import type { AppRouter } from '../../../api/src/trpc/trpc.router';
import { env } from '../env';
import { env } from 'next-runtime-env';

export const trpc = createTRPCProxyClient<AppRouter>({
links: [
loggerLink({
enabled: (opts) =>
(env.NODE_ENV === 'development' && typeof window !== 'undefined') ||
(env('NEXT_PUBLIC_NODE_ENV') === 'development' && typeof window !== 'undefined') ||
(opts.direction === 'down' && opts.result instanceof Error),
}),
httpBatchLink({
url: `${env.NEXT_PUBLIC_API_SERVER_URL}/trpc`,
url: `${env('NEXT_PUBLIC_API_SERVER_URL')}/trpc`,
fetch(url, options) {
return fetch(url, {
...options,
Expand Down
26 changes: 0 additions & 26 deletions apps/web/src/env.ts

This file was deleted.

4 changes: 2 additions & 2 deletions apps/web/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { jwtVerify } from 'jose';
import { trpc } from './app/trpc';
import { parseJwt } from './service/auth';
import { Payload } from './interface/auth';
import { env } from './env';
import { env } from 'next-runtime-env';

function redirect(req: NextRequest, payload: Payload): NextResponse {
const path = req.nextUrl.pathname;
Expand All @@ -19,7 +19,7 @@ export async function middleware(req: NextRequest) {
if (accessToken) {
const verifyResult = await jwtVerify(
accessToken,
new TextEncoder().encode(env.JWT_SECRET),
new TextEncoder().encode(env('JWT_SECRET')),
).catch(() => null);
const verifiedPayload = verifyResult?.payload as Payload | undefined;
// console.log(
Expand Down
9 changes: 4 additions & 5 deletions apps/web/src/service/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { cookies } from 'next/headers';
import { trpc } from '../app/trpc';
import type { Payload, Tokens } from '../interface/auth';
import { authErrors } from '../errors/auth';
import { env } from '../env';

export async function getCookies(): Promise<Tokens> {
try {
Expand All @@ -28,20 +27,20 @@ export async function getCookies(): Promise<Tokens> {
}

export async function signIn(token: string): Promise<Tokens> {
console.log(
/* console.log(
'Nextjs server-side, Signing in with token:',
JSON.stringify({ token }),
'destination:',
env.NEXT_PUBLIC_API_SERVER_URL,
env('NEXT_PUBLIC_API_SERVER_URL'),
'auth/signin',
);
); */

// const data = await trpc.authRouter.signin.mutate({ token }).catch((err) => {
// console.error('Nextjs server-side, signIn err:', err);
// throw new Error(authErrors.signInError);
// });
const response = await fetch(
`${env.NEXT_PUBLIC_API_SERVER_URL}/auth/signin`,
`${process.env.NEXT_PUBLIC_API_SERVER_URL}/auth/signin`,
{
method: 'POST',
headers: {
Expand Down
4 changes: 2 additions & 2 deletions apps/web/src/service/aws/uploadFileToS3.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { env } from '@/src/env';
import { env } from 'next-runtime-env';
import { getCookies } from '../auth';

interface UploadResponse {
Expand All @@ -19,7 +19,7 @@ export default async function uploadFileToS3(obj: {
formData.append('fileName', obj.file.name);
const cookies = await getCookies();
const response = await fetch(
`${env.NEXT_PUBLIC_API_SERVER_URL}/aws/upload`,
`${env('NEXT_PUBLIC_API_SERVER_URL')}/aws/upload`,
{
method: 'POST',
body: formData,
Expand Down
Loading

0 comments on commit 4f9070a

Please sign in to comment.