Skip to content

Commit

Permalink
Merge pull request #3 from nulib/build-errors
Browse files Browse the repository at this point in the history
Fix linting and typescript errors in build
  • Loading branch information
kdid authored Feb 27, 2025
2 parents 950a820 + c3968c0 commit 138dc75
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 21 deletions.
57 changes: 37 additions & 20 deletions app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,35 +1,45 @@
"use client";
import { Amplify } from "aws-amplify";
import { Authenticator } from "@aws-amplify/ui-react";
import { useState } from "react";
import { useState, useEffect } from "react";
import { fetchAuthSession } from '@aws-amplify/auth';
import "@aws-amplify/ui-react/styles.css";

export default function Home() {
const [inputValue, setInputValue] = useState("");
const [apiResponse, setApiResponse] = useState(null);
const apiUrl = process.env.NEXT_PUBLIC_API_URL;
const userPoolId = process.env.NEXT_PUBLIC_COGNITO_USER_POOL_ID;
const userPoolClientId = process.env.NEXT_PUBLIC_COGNITO_USER_POOL_CLIENT_ID;
const [isConfigured, setIsConfigured] = useState(false);
const [apiUrl, setApiUrl] = useState("");

useEffect(() => {
const apiUrlValue = process.env.NEXT_PUBLIC_API_URL;
const userPoolId = process.env.NEXT_PUBLIC_COGNITO_USER_POOL_ID;
const userPoolClientId = process.env.NEXT_PUBLIC_COGNITO_USER_POOL_CLIENT_ID;

if (!apiUrl) {
throw new Error("API_URL is not defined");
}

Amplify.configure({
Auth: {
Cognito: {
userPoolId,
userPoolClientId
},
if (!apiUrlValue || !userPoolId || !userPoolClientId) {
console.error("Required environment variables are not defined");
return;
}
});

setApiUrl(apiUrlValue);

Amplify.configure({
Auth: {
Cognito: {
userPoolId,
userPoolClientId
},
}
});

setIsConfigured(true);
}, []);

async function getToken() {
try {
const session = await fetchAuthSession();
const accessToken = session.tokens.accessToken.toString();
const idToken = session.tokens.idToken.toString();
const accessToken = session.tokens?.accessToken?.toString() || '';
const idToken = session.tokens?.idToken?.toString() || '';
console.log('accessToken:', accessToken);
console.log('idToken:', idToken);
return idToken;
Expand All @@ -39,15 +49,19 @@ export default function Home() {
}
}

const getApiResponse = async (user) => {
console.log("user", user);
const getApiResponse = async () => {
try {
const token = await getToken();

if (!token) {
console.error("User is not properly authenticated");
return;
}

if (!apiUrl) {
console.error("API URL is not defined");
return;
}

return await fetch(apiUrl, {
method: "POST",
Expand All @@ -73,6 +87,9 @@ export default function Home() {
}
}

if (!isConfigured) {
return <div>Loading...</div>;
}

return (
<Authenticator
Expand All @@ -92,7 +109,7 @@ export default function Home() {
/>
<button
onClick={async () => {
const response = await getApiResponse(user);
await getApiResponse();
}}
className="button"
>
Expand Down
2 changes: 1 addition & 1 deletion next.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { NextConfig } from "next";

const nextConfig: NextConfig = {
output: "export"
output: "export",
};

export default nextConfig;

0 comments on commit 138dc75

Please sign in to comment.