Skip to content

Commit de63a81

Browse files
committed
chore(auth): improve API key error message for expired or invalid keys
1 parent c0facde commit de63a81

3 files changed

Lines changed: 13 additions & 6 deletions

File tree

‎backend/middleware/verifyApiKey.js‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,16 @@ module.exports = async (req, res, next) => {
88
if (!apiKey) return res.status(401).json({ error: 'API key not found' });
99

1010
const hashedApi = hashApiKey(apiKey);
11-
11+
1212

1313
const project = await Project.findOne({ apiKey: hashedApi })
1414
.populate('owner', 'isVerified');
15-
if (!project) return res.status(401).json({ error: 'Invalid API key' });
15+
if (!project) return res.status(401).json(
16+
{
17+
"error": "API key is expired or invalid.",
18+
"action": "Please use a valid API key or regenerate a new one from the dashboard."
19+
}
20+
);
1621

1722
if (!project.owner.isVerified) return res.status(401).json({ error: 'Owner not verified', fix: 'Verify your account on https://urbackend.bitbros.in/dashboard' });
1823
req.project = project;

‎backend/routes/projects.js‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,20 @@ router.get('/:projectId', authMiddleware, async (req, res) => {
9797
// REGENERATE API KEY
9898
router.patch('/:projectId/regenerate-key', authMiddleware, async (req, res) => {
9999
try {
100-
const newApiKey = uuidv4();
100+
const newApiKey = generateApiKey();
101+
const hashed = hashApiKey(newApiKey);
102+
101103
const project = await Project.findOneAndUpdate(
102104
{ _id: req.params.projectId, owner: req.user._id },
103-
{ $set: { apiKey: newApiKey } },
105+
{ $set: { apiKey: hashed } },
104106
{ new: true }
105107
);
106108
if (!project) return res.status(404).json({ error: "Project not found." });
107109

108110
const projectObj = project.toObject();
109111
delete projectObj.apiKey;
110112
delete projectObj.jwtSecret;
111-
res.json({ apiKey: project.apiKey, project: projectObj });
113+
res.json({ apiKey: newApiKey, project: projectObj });
112114
} catch (err) {
113115
res.status(500).json({ error: err.message });
114116
}

‎frontend/src/pages/LandingPage/index.jsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ function LandingPage() {
174174
<div className="dot green" style={{ width: 10, height: 10, borderRadius: '50%', background: '#27c93f' }}></div>
175175
</div>
176176
<div className="url-bar">
177-
<span style={{ color: '#fff' }}>https://api.urbackend.com/v1/users</span>
177+
<span style={{ color: '#fff' }}>https://api.urbackend.bitbros.in/v1/users</span>
178178
<span style={{ fontSize: '0.75rem', color: '#666', border: '1px solid #333', padding: '2px 6px', borderRadius: 4 }}>GET</span>
179179
</div>
180180
</div>

0 commit comments

Comments
 (0)