Stub out all SSR, built login page
This commit is contained in:
23
mod.ts
23
mod.ts
@@ -1,10 +1,14 @@
|
|||||||
import { customAlphabet } from '@nanoid';
|
import { customAlphabet } from '@nanoid';
|
||||||
import { STATUS_CODE, STATUS_TEXT, StatusCode } from '@std/http';
|
import { STATUS_CODE, STATUS_TEXT, StatusCode } from '@std/http/status';
|
||||||
|
|
||||||
import config from '~config';
|
import config from '~config';
|
||||||
|
|
||||||
import dbClient from 'db/client.ts';
|
import dbClient from 'db/client.ts';
|
||||||
|
|
||||||
|
import buildPage from './ssr/buildPage.ts';
|
||||||
|
import buildLogin from './ssr/buildLogin.ts';
|
||||||
|
import buildHome from './ssr/buildHome.ts';
|
||||||
|
|
||||||
// Using custom alphabet to exclude - and _
|
// Using custom alphabet to exclude - and _
|
||||||
const alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
|
const alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
|
||||||
const nanoid = customAlphabet(alphabet, 20);
|
const nanoid = customAlphabet(alphabet, 20);
|
||||||
@@ -29,12 +33,23 @@ Deno.serve({ port: config.api.port }, async (req) => {
|
|||||||
|
|
||||||
let failed = false;
|
let failed = false;
|
||||||
if (req.method === 'GET') {
|
if (req.method === 'GET') {
|
||||||
if (path === '/home') {
|
if (path === '') {
|
||||||
|
const redirectHeaders = new Headers();
|
||||||
|
redirectHeaders.set('Location', '/api/home');
|
||||||
|
return genericResponse(STATUS_CODE.PermanentRedirect, undefined, redirectHeaders);
|
||||||
|
} else if (path === '/home') {
|
||||||
// SSR "login page"
|
// SSR "login page"
|
||||||
return genericResponse(STATUS_CODE.Unauthorized, 'Please sign in.');
|
return buildPage(buildLogin);
|
||||||
} else if (path.startsWith('/home/')) {
|
} else if (path.startsWith('/home/')) {
|
||||||
// SSR "home page"
|
// SSR "home page"
|
||||||
return genericResponse(STATUS_CODE.NotImplemented, 'WIP');
|
const userId = path.replace('/home/', '');
|
||||||
|
const userMatch = await dbClient.query('SELECT id FROM users WHERE id = ?', [userId]).catch(() => {
|
||||||
|
failed = true;
|
||||||
|
});
|
||||||
|
if (failed) return buildPage("Couldn't read DB. Please try again.");
|
||||||
|
if (!userMatch.length) return buildPage('User ID does not exist. Please click on the page header to go back to the sign in page.');
|
||||||
|
|
||||||
|
return buildPage(buildHome(userId));
|
||||||
} else if (path.startsWith('/read/')) {
|
} else if (path.startsWith('/read/')) {
|
||||||
const planId = path.replace('/read/', '');
|
const planId = path.replace('/read/', '');
|
||||||
|
|
||||||
|
|||||||
3
ssr/buildHome.ts
Normal file
3
ssr/buildHome.ts
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
export default (userId: string) => {
|
||||||
|
return `${userId}`;
|
||||||
|
};
|
||||||
19
ssr/buildLogin.ts
Normal file
19
ssr/buildLogin.ts
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
export default `<div>
|
||||||
|
<script>
|
||||||
|
function doLogin() {
|
||||||
|
fetch('/api/auth', {
|
||||||
|
method:'POST',
|
||||||
|
body:JSON.stringify({ name:document.getElementById('name').value.trim(), pin:document.getElementById('pin').value.trim() })
|
||||||
|
}).catch((e)=>{e.text().then((text)=>{alert(text);});}).then((r) => {
|
||||||
|
if(r.status===200){r.json().then((j)=>{localStorage.setItem('name', document.getElementById('name').value.trim());window.location.replace('/api/home/'+j.id);});}
|
||||||
|
else{r.text().then((text)=>{alert(text);});}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
<p>Please sign in:</p>
|
||||||
|
<label>Name: <input id="name" type="text" autocomplete="off"/></label>
|
||||||
|
<br/>
|
||||||
|
<label>PIN: <input id="pin" type="password" autocomplete="off"/></label>
|
||||||
|
<br/>
|
||||||
|
<button onclick="doLogin()">Sign In</button>
|
||||||
|
</div>`;
|
||||||
36
ssr/buildPage.ts
Normal file
36
ssr/buildPage.ts
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
import { STATUS_CODE, STATUS_TEXT } from '@std/http/status';
|
||||||
|
|
||||||
|
// Utilize the pre-existing stylesheets, do a little tweaking to make it ours
|
||||||
|
const buildPage = (str: string) =>
|
||||||
|
`<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
|
||||||
|
<title>XIVPlan+DB Manager</title>
|
||||||
|
<meta name="distribution" content="web">
|
||||||
|
<meta name="web_author" content="Ean Milligan (ean@milligan.dev)">
|
||||||
|
<meta name="author" content="Ean Milligan (ean@milligan.dev)">
|
||||||
|
<meta name="designer" content="Ean Milligan (ean@milligan.dev)">
|
||||||
|
<meta name="publisher" content="Ean Milligan (ean@milligan.dev)">
|
||||||
|
<meta name="robots" content="noindex, nofollow">
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
background-color:black;
|
||||||
|
color:white;
|
||||||
|
}
|
||||||
|
a {
|
||||||
|
text-decoration:none;
|
||||||
|
color:white;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<header><h1><a href="/api/home">XIVPlan+DB Manager</a></h1></header>
|
||||||
|
${str}
|
||||||
|
</body>
|
||||||
|
</html>`;
|
||||||
|
|
||||||
|
const htmlHeaders = new Headers();
|
||||||
|
htmlHeaders.append('Content-Type', 'text/html');
|
||||||
|
|
||||||
|
export default (str: string) => new Response(buildPage(str), { status: STATUS_CODE.OK, statusText: STATUS_TEXT[STATUS_CODE.OK], headers: htmlHeaders });
|
||||||
Reference in New Issue
Block a user