-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthMiddleware.js
More file actions
63 lines (52 loc) · 1.84 KB
/
Copy pathauthMiddleware.js
File metadata and controls
63 lines (52 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const { supabase } = require('./supabaseClient');
/**
* Express middleware to authenticate JWT tokens using Supabase
* Extracts the JWT from the Authorization header and verifies it
*/
const authMiddleware = async (req, res, next) => {
try {
// Get the Authorization header
const authHeader = req.headers.authorization;
if (!authHeader) {
return res.status(401).json({
error: 'Authorization header is required',
message: 'Please provide a valid JWT token in the Authorization header'
});
}
// Check if the header starts with 'Bearer '
if (!authHeader.startsWith('Bearer ')) {
return res.status(401).json({
error: 'Invalid authorization format',
message: 'Authorization header must start with "Bearer "'
});
}
// Extract the token (remove 'Bearer ' prefix)
const token = authHeader.substring(7);
if (!token) {
return res.status(401).json({
error: 'Token is required',
message: 'Please provide a valid JWT token'
});
}
// Verify the token using Supabase
const { data: { user }, error } = await supabase.auth.getUser(token);
if (error || !user) {
console.error('Token verification failed:', error);
return res.status(401).json({
error: 'Invalid or expired token',
message: 'The provided JWT token is invalid or has expired'
});
}
// Add the user to the request object for use in subsequent middleware/routes
req.user = user;
// Call next() to proceed to the next middleware or route handler
next();
} catch (error) {
console.error('Auth middleware error:', error);
return res.status(500).json({
error: 'Internal server error',
message: 'An error occurred while authenticating the request'
});
}
};
module.exports = authMiddleware;