2023-08-20 05:23:15 +00:00
|
|
|
import redis from '@umami/redis-client';
|
2023-04-25 04:35:09 +00:00
|
|
|
import debug from 'debug';
|
2023-08-20 05:23:15 +00:00
|
|
|
import { setAuthKey } from 'lib/auth';
|
|
|
|
|
import { secret } from 'lib/crypto';
|
|
|
|
|
import { useValidate } from 'lib/middleware';
|
|
|
|
|
import { NextApiRequestQueryBody, User } from 'lib/types';
|
2023-04-05 06:29:54 +00:00
|
|
|
import { NextApiResponse } from 'next';
|
2022-11-09 06:58:52 +00:00
|
|
|
import {
|
|
|
|
|
checkPassword,
|
|
|
|
|
createSecureToken,
|
2023-08-02 18:56:42 +00:00
|
|
|
forbidden,
|
2023-08-20 05:23:15 +00:00
|
|
|
methodNotAllowed,
|
|
|
|
|
ok,
|
|
|
|
|
unauthorized,
|
2022-11-09 06:58:52 +00:00
|
|
|
} from 'next-basics';
|
2023-07-30 05:03:34 +00:00
|
|
|
import { getUserByUsername } from 'queries';
|
2023-08-20 05:23:15 +00:00
|
|
|
import * as yup from 'yup';
|
2022-11-15 21:21:14 +00:00
|
|
|
|
2023-04-25 04:35:09 +00:00
|
|
|
const log = debug('umami:auth');
|
|
|
|
|
|
2022-11-15 21:21:14 +00:00
|
|
|
export interface LoginRequestBody {
|
|
|
|
|
username: string;
|
|
|
|
|
password: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface LoginResponse {
|
|
|
|
|
token: string;
|
|
|
|
|
user: User;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-20 05:23:15 +00:00
|
|
|
const schema = {
|
|
|
|
|
POST: yup.object().shape({
|
|
|
|
|
username: yup.string().required(),
|
|
|
|
|
password: yup.string().required(),
|
|
|
|
|
}),
|
|
|
|
|
};
|
|
|
|
|
|
2022-11-15 21:21:14 +00:00
|
|
|
export default async (
|
|
|
|
|
req: NextApiRequestQueryBody<any, LoginRequestBody>,
|
|
|
|
|
res: NextApiResponse<LoginResponse>,
|
|
|
|
|
) => {
|
2023-08-02 18:56:42 +00:00
|
|
|
if (process.env.DISABLE_LOGIN) {
|
|
|
|
|
return forbidden(res);
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-20 05:23:15 +00:00
|
|
|
req.yup = schema;
|
|
|
|
|
await useValidate(req, res);
|
|
|
|
|
|
2022-11-09 06:58:52 +00:00
|
|
|
if (req.method === 'POST') {
|
|
|
|
|
const { username, password } = req.body;
|
2020-07-22 22:46:05 +00:00
|
|
|
|
2023-07-30 05:03:34 +00:00
|
|
|
const user = await getUserByUsername(username, { includePassword: true });
|
2021-10-04 03:44:02 +00:00
|
|
|
|
2022-11-09 06:58:52 +00:00
|
|
|
if (user && checkPassword(password, user.password)) {
|
|
|
|
|
if (redis.enabled) {
|
2023-04-05 06:29:54 +00:00
|
|
|
const token = await setAuthKey(user);
|
2022-11-09 06:58:52 +00:00
|
|
|
|
|
|
|
|
return ok(res, { token, user });
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-03 20:30:12 +00:00
|
|
|
const token = createSecureToken({ userId: user.id }, secret());
|
2022-11-08 00:22:49 +00:00
|
|
|
|
2023-04-13 19:08:53 +00:00
|
|
|
return ok(res, {
|
|
|
|
|
token,
|
2023-05-16 19:17:51 +00:00
|
|
|
user: { id: user.id, username: user.username, role: user.role, createdAt: user.createdAt },
|
2023-04-13 19:08:53 +00:00
|
|
|
});
|
2022-11-08 00:22:49 +00:00
|
|
|
}
|
|
|
|
|
|
2023-04-25 05:00:40 +00:00
|
|
|
log('Login failed:', { username, user });
|
2023-04-25 04:35:09 +00:00
|
|
|
|
2023-04-08 20:14:22 +00:00
|
|
|
return unauthorized(res, 'message.incorrect-username-password');
|
2020-07-22 22:46:05 +00:00
|
|
|
}
|
2020-07-29 02:04:45 +00:00
|
|
|
|
2022-11-09 06:58:52 +00:00
|
|
|
return methodNotAllowed(res);
|
2020-07-22 22:46:05 +00:00
|
|
|
};
|