Next.js Discord

Discord Forum

Call an API recurrently

Unanswered
Zepeto posted this in #help-forum
Open in Discord
Hi,
I'd like to call an API endpoint every X minutes, however I'm using next-auth to retrieve my token with
await auth();

that can only be used in server component, how can I do this ?

31 Replies

If you're referring to client, authjs must have something like useSession
@Zepeto From server
You’ll have to use from
Cron*
@Anay-208 | Ping in replies You’ll have to use from
There is no other way ? 🙁
@Zepeto There is no other way ? 🙁
You want to call it every X minutes.

Serverless functions only last for the duration of the request
and I can't do a trick so that a client component calls a server component ?
@Zepeto and I can't do a trick so that a client component calls a server component ?
Client component can call a server action, but the you're relying on Client for cron's, which the client can abuse.

and its a bad practice, relying on user's for cron
Californian
I suggest him to save the token in the cookies after login then you can be retrieving it from there either from server or client
@Anay-208 | Ping in replies Client component can call a `server action`, but the you're relying on Client for cron's, which the client can abuse. and its a bad practice, relying on user's for cron
If I create an api route that I call in a useEffect and in this api route I call my endpoint with the token, is this risky ?
@Zepeto If I create an api route that I call in a useEffect and in this api route I call my endpoint with the token, is this risky ?
It you think about it this way, what if no one is there on the client.

And yes, you’ll be creating security or DoS vulnerabilities, if you’re relying on client for it
@Anay-208 | Ping in replies How are you intending to do it?
That's the question, because I don't see how a cron can do it.
@Zepeto That's the question, because I don't see how a cron can do it.
With a cron, you can easily do it, but you won't be able to use auth.
Why do you even need to use auth there/
@Zepeto Because it’s a secured endpoint
Just use cron, you can secure that too
@Anay-208 | Ping in replies Just use cron, you can secure that too
But how will the cron send me the data?
@Zepeto But how will the cron send me the data?
what data do you need to send
@Anay-208 | Ping in replies what data do you need to send
It's very simple: I retrieve data from an api and display it on my page.
This data may change, so to update the data displayed I want to call my api every minute. How do I do this?
There should be a param for revalidating
It comes back to the proxy solution with a route
@Zepeto But doesn't that expose the token ?
Don’t expose any sensitive token to front end then, manage this in the back end
@Anay-208 | Ping in replies Don’t expose any sensitive token to front end then, manage this in the back end
That's what I'm trying to do, but It seems I can't do it
@Anay-208 | Ping in replies What error are you getting?
I don't see how I can do it, since if I want to call frequently I have to go through a client component