DEV Community

Discussion on: Best practices to generate and store API keys using Node.js?

Collapse
 
davidszabo97 profile image
Dávid Szabó
  1. Generate a random string and store it in the database. As long and as complex as possible.
  2. The keys should be associated with the user who created it. Therefore you can lookup the API key in the database and find out which user it is. You can use a middleware to authenticate via API key.

Yes, I'd avoid JWTs here.

Collapse
 
nagarjun profile image
Nagarjun Palavalli

Store keys in plain text in the database? Seems like it might open up some kind of vulnerability.

Collapse
 
davidszabo97 profile image
Dávid Szabó

I never said you should store them as plain text. Encrypt them and when an API call comes in, decrypt the generated string to find out which access token it is.

Generate an ID for your access token then encrypt("ID" + "creation time") with your app secret.