Call an API recurrently
Unanswered
Zepeto posted this in #help-forum
ZepetoOP
Hi,
I'd like to call an API endpoint every X minutes, however I'm using next-auth to retrieve my token with
that can only be used in server component, how can I do this ?
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
ZepetoOP
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
Serverless functions only last for the duration of the request
ZepetoOP
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
and its a bad practice, relying on user's for cron
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
ZepetoOP
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
And yes, you’ll be creating security or DoS vulnerabilities, if you’re relying on client for it
@Anay-208 | Ping in replies 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
ZepetoOP
If I want to do things differently, would I need to set up a websocket?
@Zepeto If I want to do things differently, would I need to set up a websocket?
How are you intending to do it?
@Anay-208 | Ping in replies How are you intending to do it?
ZepetoOP
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/
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
ZepetoOP
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
ZepetoOP
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?
This data may change, so to update the data displayed I want to call my api every minute. How do I do this?
@Zepeto 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?
Oh, if the data is for displaying, then you can just use tanstack query with it
There should be a param for revalidating
@Anay-208 | Ping in replies Oh, if the data is for displaying, then you can just use tanstack query with it
ZepetoOP
But doesn't that expose the token ?
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
ZepetoOP
That's what I'm trying to do, but It seems I can't do it
@Zepeto That's what I'm trying to do, but It seems I can't do it
What error are you getting?
@Anay-208 | Ping in replies What error are you getting?
ZepetoOP
I don't see how I can do it, since if I want to call frequently I have to go through a client component