mirror of
https://github.com/umami-software/umami.git
synced 2026-02-06 05:37:20 +01:00
Moved code into src folder. Added build for component library.
This commit is contained in:
parent
7a7233ead4
commit
ede658771e
490 changed files with 749 additions and 442 deletions
75
src/pages/api/auth/login.ts
Normal file
75
src/pages/api/auth/login.ts
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
import redis from '@umami/redis-client';
|
||||
import debug from 'debug';
|
||||
import { setAuthKey } from 'lib/auth';
|
||||
import { secret } from 'lib/crypto';
|
||||
import { useValidate } from 'lib/middleware';
|
||||
import { NextApiRequestQueryBody, User } from 'lib/types';
|
||||
import { NextApiResponse } from 'next';
|
||||
import {
|
||||
checkPassword,
|
||||
createSecureToken,
|
||||
forbidden,
|
||||
methodNotAllowed,
|
||||
ok,
|
||||
unauthorized,
|
||||
} from 'next-basics';
|
||||
import { getUserByUsername } from 'queries';
|
||||
import * as yup from 'yup';
|
||||
|
||||
const log = debug('umami:auth');
|
||||
|
||||
export interface LoginRequestBody {
|
||||
username: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
export interface LoginResponse {
|
||||
token: string;
|
||||
user: User;
|
||||
}
|
||||
|
||||
const schema = {
|
||||
POST: yup.object().shape({
|
||||
username: yup.string().required(),
|
||||
password: yup.string().required(),
|
||||
}),
|
||||
};
|
||||
|
||||
export default async (
|
||||
req: NextApiRequestQueryBody<any, LoginRequestBody>,
|
||||
res: NextApiResponse<LoginResponse>,
|
||||
) => {
|
||||
if (process.env.DISABLE_LOGIN) {
|
||||
return forbidden(res);
|
||||
}
|
||||
|
||||
req.yup = schema;
|
||||
await useValidate(req, res);
|
||||
|
||||
if (req.method === 'POST') {
|
||||
const { username, password } = req.body;
|
||||
|
||||
const user = await getUserByUsername(username, { includePassword: true });
|
||||
|
||||
if (user && checkPassword(password, user.password)) {
|
||||
if (redis.enabled) {
|
||||
const token = await setAuthKey(user);
|
||||
|
||||
return ok(res, { token, user });
|
||||
}
|
||||
|
||||
const token = createSecureToken({ userId: user.id }, secret());
|
||||
|
||||
return ok(res, {
|
||||
token,
|
||||
user: { id: user.id, username: user.username, role: user.role, createdAt: user.createdAt },
|
||||
});
|
||||
}
|
||||
|
||||
log('Login failed:', { username, user });
|
||||
|
||||
return unauthorized(res, 'message.incorrect-username-password');
|
||||
}
|
||||
|
||||
return methodNotAllowed(res);
|
||||
};
|
||||
19
src/pages/api/auth/logout.ts
Normal file
19
src/pages/api/auth/logout.ts
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
import { methodNotAllowed, ok } from 'next-basics';
|
||||
import redis from '@umami/redis-client';
|
||||
import { useAuth } from 'lib/middleware';
|
||||
import { getAuthToken } from 'lib/auth';
|
||||
import { NextApiRequest, NextApiResponse } from 'next';
|
||||
|
||||
export default async (req: NextApiRequest, res: NextApiResponse) => {
|
||||
await useAuth(req, res);
|
||||
|
||||
if (req.method === 'POST') {
|
||||
if (redis.enabled) {
|
||||
await redis.del(getAuthToken(req));
|
||||
}
|
||||
|
||||
return ok(res);
|
||||
}
|
||||
|
||||
return methodNotAllowed(res);
|
||||
};
|
||||
18
src/pages/api/auth/sso.ts
Normal file
18
src/pages/api/auth/sso.ts
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
import { NextApiRequestAuth } from 'lib/types';
|
||||
import { useAuth } from 'lib/middleware';
|
||||
import { NextApiResponse } from 'next';
|
||||
import { badRequest, ok } from 'next-basics';
|
||||
import redis from '@umami/redis-client';
|
||||
import { setAuthKey } from 'lib/auth';
|
||||
|
||||
export default async (req: NextApiRequestAuth, res: NextApiResponse) => {
|
||||
await useAuth(req, res);
|
||||
|
||||
if (redis.enabled && req.auth.user) {
|
||||
const token = await setAuthKey(req.auth.user, 86400);
|
||||
|
||||
return ok(res, { user: req.auth.user, token });
|
||||
}
|
||||
|
||||
return badRequest(res);
|
||||
};
|
||||
10
src/pages/api/auth/verify.ts
Normal file
10
src/pages/api/auth/verify.ts
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import { NextApiRequestAuth } from 'lib/types';
|
||||
import { useAuth } from 'lib/middleware';
|
||||
import { NextApiResponse } from 'next';
|
||||
import { ok } from 'next-basics';
|
||||
|
||||
export default async (req: NextApiRequestAuth, res: NextApiResponse) => {
|
||||
await useAuth(req, res);
|
||||
|
||||
return ok(res, req.auth);
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue