Enable public website sharing.

This commit is contained in:
Mike Cao 2020-08-15 01:17:15 -07:00
parent 48a524e09c
commit 560f1316c1
36 changed files with 294 additions and 61 deletions

View file

@ -5,6 +5,7 @@ import { JWT, JWE, JWK } from 'jose';
const SALT_ROUNDS = 10;
const KEY = JWK.asKey(Buffer.from(secret()));
const CHARS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
export function hash(...args) {
return crypto.createHash('sha512').update(args.join('')).digest('hex');
@ -24,6 +25,14 @@ export function isValidId(s) {
return validate(s);
}
export function getRandomChars(n) {
let s = '';
for (let i = 0; i < n; i++) {
s += CHARS[Math.floor(Math.random() * CHARS.length)];
}
return s;
}
export async function hashPassword(password) {
return bcrypt.hash(password, SALT_ROUNDS);
}

View file

@ -28,6 +28,16 @@ export async function getWebsiteByUuid(website_uuid) {
);
}
export async function getWebsiteByShareId(share_id) {
return runQuery(
prisma.website.findOne({
where: {
share_id,
},
}),
);
}
export async function getUserWebsites(user_id) {
return runQuery(
prisma.website.findMany({

View file

@ -8,22 +8,26 @@ export function redirect(res, url) {
return res.status(303).end();
}
export function badRequest(res, msg) {
export function badRequest(res, msg = '400 Bad Request') {
return res.status(400).end(msg);
}
export function unauthorized(res, msg) {
export function unauthorized(res, msg = '401 Unauthorized') {
return res.status(401).end(msg);
}
export function forbidden(res, msg) {
export function forbidden(res, msg = '403 Forbidden') {
return res.status(403).end(msg);
}
export function methodNotAllowed(res, msg) {
export function notFound(res, msg = '404 Not Found') {
return res.status(404).end(msg);
}
export function methodNotAllowed(res, msg = '405 Method Not Allowed') {
res.status(405).end(msg);
}
export function serverError(res, msg) {
export function serverError(res, msg = '500 Internal Server Error') {
res.status(500).end(msg);
}