#Calling a function w/ .env variables in an onSubmit()

1 messages · Page 1 of 1 (latest)

cursive spruceBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

waxen lantern
#

it sounds like onSubmit is from a client component. In client components server variables are not available. That's why you don't get the env variable

glass cypress
#

two things u can do

waxen lantern
#

yes, you need to handle that serverside. You can either create an route handler (api endpoint) or server action

glass cypress
#

lmao

waxen lantern
#

its a public api route rightß

glass cypress
#

have a middleware route that handles your request and forwards it to the user creation endpoint,

or have a server action

waxen lantern
glass cypress
#

for example /beefbonestinks and that sends a request to /creatuser

waxen lantern
#

And you want to ensure that only your app can access this endpoint? No external sites?

waxen lantern
#

when your route is public, you can add a public api key, that the user (client) somehow receives when using your page. This api key can then be verified by your server. Keep in mind: the public api key is shared with the client so it can be published and there can still abuse of your functions.

Some things you might want to consider are ratelimiting by ip. Another thing you might want to think of is by using CORS to only allow requests from a specific origin. That's just a basic protection and you should add an additional layer of security on the network level to prevent unwanted access

glass cypress
#

or just have a captcha system from google or cloudflare

waxen lantern
#

then you only need to create an api route or server action

#

for a route handler its:

export async function POST(request: Request) {
  const res = await request.json()
// do stuff
  return Response.json({ res })
}

And for server actions its an extra file with:

'use server'
 
export async function create() {
  // do stuff
}
#

no there is no special place for the file

waxen lantern
#

can you console.log the response? Maybe even the await response.json() if it#s json

glass cypress
#

hold on

#

so

#

so token is null?

waxen lantern
#

what about response.json?

#

normally telegram gives an response everytime

glass cypress
waxen lantern
#

do it like this:

const json = await response.json();
console.log(json, response)

Update:
First he needed to use a server action or route handler: #1276608412519628862 message
Then he encountered an error, that he can't see any response: #1276608412519628862 message
The solution for that was to log the json and response differently than he done (see this message).
Like that he can see that his backend works fine: #1276608412519628862 message

glass cypress
#

are u sure the token is defined

waxen lantern
#

oh

waxen lantern
waxen lantern
glass cypress
#

@spiral delta console.log your token and the patient

waxen lantern
#

first he logs the json and the response xD

#

see @glass cypress ^

glass cypress
#

whats with the patient

glass cypress
waxen lantern
#

that looks great.

statusText: 'OK',
and valid json:
id: 'pat::2723b785-9529-4db2-8e97-16c537712083',
email: 'david@theinnodsdsvationlab.dev',

#
status: 200,

everything is fine

#

the token thing?

#

that we recommended to use sevrerside stuff to get the env variable?

glass cypress
#

makes sense

waxen lantern
#

which message was the one, that solved this thread the most?

I know there are all important, but which message was the most helpful?

#

sure, take your time. Now you know how to check the response sunglasses_1

cursive spruceBOT
#
✅ Success!

This question has been marked as answered! If you have any other questions, feel free to create another post

Jump to answer
waxen lantern
#

happy to help

#

wow that's a lot! You'll need it a lot more in the future. And whenever you need it, just think about this thread and you are good to go 😊

waxen lantern
#

this thread is already solved. Create another thread and also share the ommited message (you see it in the server console). For only this new thread, you can ping me once

#

but I guess you can quickly solve this:

Patient validation failed: dateOfBirth: Cast to date failed for value "Invalid Date" (type Date) at path "dateOfBirth"',