-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.js
More file actions
27 lines (23 loc) · 705 Bytes
/
auth.js
File metadata and controls
27 lines (23 loc) · 705 Bytes
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
import { expressjwt } from 'express-jwt';
import jwt from 'jsonwebtoken';
import { getUser } from './db/users.js';
const secret = Buffer.from('+Z3zPGXY7v/0MoMm1p8QuHDGGVrhELGd', 'base64');
export const authMiddleware = expressjwt({
algorithms: ['HS256'],
credentialsRequired: false,
secret,
});
export function decodeToken(token) {
return jwt.verify(token, secret);
}
export async function handleLogin(req, res) {
const { username, password } = req.body;
const user = await getUser(username);
if (!user || user.password !== password) {
res.sendStatus(401);
} else {
const claims = { sub: username };
const token = jwt.sign(claims, secret);
res.json({ token });
}
}