File size: 1,007 Bytes
7fcea49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import bcrypt from 'bcryptjs';
import jwt from 'jsonwebtoken';

export const hashPassword = async (password) => bcrypt.hash(password, 10);

export const verifyPassword = async (password, passwordHash) => bcrypt.compare(password, passwordHash);

export const signToken = (payload, secret) => jwt.sign(payload, secret, { expiresIn: '30d' });

export const verifyToken = (token, secret) => jwt.verify(token, secret);

export const getTokenFromReq = (req) => {
  const header = req.headers.authorization;
  if (header && header.toLowerCase().startsWith('bearer ')) return header.slice(7);
  if (req.cookies?.token) return req.cookies.token;
  return null;
};

export const requireAuth = (secret) => (req, res, next) => {
  try {
    const token = getTokenFromReq(req);
    if (!token) return res.status(401).json({ error: 'UNAUTHORIZED' });
    const decoded = verifyToken(token, secret);
    req.auth = decoded;
    return next();
  } catch {
    return res.status(401).json({ error: 'UNAUTHORIZED' });
  }
};