Added lib/password. Small tweaks.
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
import bcrypt from 'bcryptjs';
|
||||
import debug from 'debug';
|
||||
import { ROLE_PERMISSIONS, ROLES, SHARE_TOKEN_HEADER } from '@/lib/constants';
|
||||
import { secret } from '@/lib/crypto';
|
||||
@@ -9,15 +8,6 @@ import redis from '@/lib/redis';
|
||||
import { getUser } from '@/queries/prisma/user';
|
||||
|
||||
const log = debug('umami:auth');
|
||||
const SALT_ROUNDS = 10;
|
||||
|
||||
export function hashPassword(password: string, rounds = SALT_ROUNDS) {
|
||||
return bcrypt.hashSync(password, rounds);
|
||||
}
|
||||
|
||||
export function checkPassword(password: string, passwordHash: string) {
|
||||
return bcrypt.compareSync(password, passwordHash);
|
||||
}
|
||||
|
||||
export function getBearerToken(request: Request) {
|
||||
const auth = request.headers.get('authorization');
|
||||
|
||||
11
src/lib/password.ts
Normal file
11
src/lib/password.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import bcrypt from 'bcryptjs';
|
||||
|
||||
const SALT_ROUNDS = 10;
|
||||
|
||||
export function hashPassword(password: string, rounds = SALT_ROUNDS) {
|
||||
return bcrypt.hashSync(password, rounds);
|
||||
}
|
||||
|
||||
export function checkPassword(password: string, passwordHash: string) {
|
||||
return bcrypt.compareSync(password, passwordHash);
|
||||
}
|
||||
@@ -2,11 +2,11 @@ export function ok() {
|
||||
return Response.json({ ok: true });
|
||||
}
|
||||
|
||||
export function json(data: any = {}) {
|
||||
export function json(data: Record<string, any> = {}) {
|
||||
return Response.json(data);
|
||||
}
|
||||
|
||||
export function badRequest(error?: any) {
|
||||
export function badRequest(error?: Record<string, any>) {
|
||||
return Response.json(
|
||||
{
|
||||
error: { message: 'Bad request', code: 'bad-request', status: 400, ...error },
|
||||
@@ -15,7 +15,7 @@ export function badRequest(error?: any) {
|
||||
);
|
||||
}
|
||||
|
||||
export function unauthorized(error?: any) {
|
||||
export function unauthorized(error?: Record<string, any>) {
|
||||
return Response.json(
|
||||
{
|
||||
error: {
|
||||
@@ -29,21 +29,21 @@ export function unauthorized(error?: any) {
|
||||
);
|
||||
}
|
||||
|
||||
export function forbidden(error?: any) {
|
||||
export function forbidden(error?: Record<string, any>) {
|
||||
return Response.json(
|
||||
{ error: { message: 'Forbidden', code: 'forbidden', status: 403, ...error } },
|
||||
{ status: 403 },
|
||||
);
|
||||
}
|
||||
|
||||
export function notFound(error?: any) {
|
||||
export function notFound(error?: Record<string, any>) {
|
||||
return Response.json(
|
||||
{ error: { message: 'Not found', code: 'not-found', status: 404, ...error } },
|
||||
{ status: 404 },
|
||||
);
|
||||
}
|
||||
|
||||
export function serverError(error?: any) {
|
||||
export function serverError(error?: Record<string, any>) {
|
||||
return Response.json(
|
||||
{
|
||||
error: {
|
||||
|
||||
Reference in New Issue
Block a user