Next.js Discord

Discord Forum

Best way to store a login token in an API route?

Answered
Northeast Congo Lion posted this in #help-forum
Open in Discord
Northeast Congo LionOP
I want to integrate Copyleaks AI writing detection into the chatbot-ui sample app. I want to prototype a game about talking with enemy bots who attack you if you sound too human.

The Copyleaks API key is used only to login for an access token. https://api.copyleaks.com/documentation/v3/account/login All other API endpoints expect this access token.

Ideally this login process would stay entirely serverside. So I'm thinking of an API route , say POST api/analyze that logs in if necessary before the actual analysis request. Something like:
export default function handler(request) {
  // get login session (token + expire date) from **somewhere**
  // if session nonexistent or expired
    // request the endpoint above for the new session
    // store new session **somewhere**
  // request the analysis endpoint, with token in the header
}

Where would that somewhere be? It should be persistent for the life of the token at least (48 hours).
Answered by tafutada777
you did not mention where to host, but assuming it is Vercel, luckily Vercel KV get GA today, so @joulev says, store the token in KV, sharing across requests would be fine. KV is Redis btw. if not, you can use Upstash Redis, which Vercel uses behind the scene.
View full answer

7 Replies

@Northeast Congo Lion where do you host your Next.js app? usually, people use Vercel or cloud venders like AWS, GCP. those services allow you to set keys or provide secret keys manage services, thus you do not need to hard-code keys in .env file or something.
@tafutada777 <@278226034523111445> where do you host your Next.js app? usually, people use Vercel or cloud venders like AWS, GCP. those services allow you to set keys or provide secret keys manage services, thus you do not need to hard-code keys in .env file or something.
Northeast Congo LionOP
Yes, true for the permanent API key. But for most of the Copyleaks API, you instead auth with a temp access token.
For whatever reason, they decided the API key wasn't authentication enough so they make your app do login too.
- Request their account/login with the API key.
- They respond with an access token. Expires in 48 hours.
- Request their other endpoints with the access token.
So the question is how shall my API route reuse an access token for as long as it's valid?
you did not mention where to host, but assuming it is Vercel, luckily Vercel KV get GA today, so @joulev says, store the token in KV, sharing across requests would be fine. KV is Redis btw. if not, you can use Upstash Redis, which Vercel uses behind the scene.
Answer
Northeast Congo LionOP
It will be Vercel so yes, Vercel KV will be the one to try.
sure. you r lucky. Vercel KV got GA today.
Northeast Congo LionOP
Thanks 👍