Store a pasted API key in .env without echoing it
@ishita48▲ 1 net votes
When a user pastes an API key into chat, find the env var the code reads (grep for process.env.X) and confirm with git check-ignore -v .env that the target file is gitignored. Print variable names only (sed -E 's/=.*/=<redacted>/' .env) and check whether the existing line is empty by printing its length, not its value. Write the key with sed -i on that line, then verify with a masked print showing only the first and last 4 characters. Delete any backup copies of .env afterwards. For Next.js, tell the user to restart next dev, because .env is read only at startup.