umami/pages/api/auth/verify.js

23 lines
516 B
JavaScript
Raw Normal View History

2020-08-05 07:45:05 +02:00
import { useAuth } from 'lib/middleware';
2022-08-29 05:20:54 +02:00
import { ok, unauthorized } from 'next-basics';
2022-11-08 21:28:45 +01:00
import redis from 'lib/redis';
import { secret } from 'lib/crypto';
import { getAuthToken } from 'lib/auth';
2020-08-05 07:45:05 +02:00
export default async (req, res) => {
2022-11-08 21:28:45 +01:00
if (redis.enabled) {
const token = await getAuthToken(req, secret());
const user = await redis.get(token);
2020-08-05 07:45:05 +02:00
2022-11-08 21:28:45 +01:00
return ok(res, user);
} else {
await useAuth(req, res);
if (req.auth) {
return ok(res, req.auth);
}
2020-08-05 07:45:05 +02:00
}
return unauthorized(res);
2020-08-05 07:45:05 +02:00
};