// routes/admin/post.jsx
/** @jsx h */
import { h } from "preact";
import { Handlers, PageProps } from "$fresh/server.ts";
import VPost from '../../components/admin/post.tsx';
import CPost from "../../controllers/admin/post.ts";
export const handler: Handlers = {
async GET(req, ctx){
return await CPost.getPage(req, ctx);
},
async POST(req, ctx){
return await CPost.createPost(req, ctx);
},
}
export default function Template(props: PageProps){
return (
<VPost data={ props.data } />
)
}
// controllers/admin/post.ts
import { getCookies, deleteCookie } from "cookies";
import { setting, secret_key, myredis } from 'setting';
import { verify } from "jwt";
import postdb from "../../models/post.ts";
class Post{
async getPage(req, ctx){
const cookies = getCookies(req.headers);
if((cookies)&&(cookies.session_id)){
const jwt = await myredis.get(cookies.session_id);
try{
const payload = await verify(jwt, secret_key, "HS512");
if(payload.user){
const config = setting();
config.page_title = "ទំព័រការផ្សាយ";
config.username = payload.user.title;
return await ctx.render({"setting": config});
}
}catch(error){
console.log(error);
const config = setting();
config.page_title = "ទំព័រចុះឈ្មោះ";
const resp = new Response();
deleteCookie(resp.headers, "session_id");
return new Response(undefined, { headers: {location: `/login`}, status: 302 });
}
}
return new Response(undefined, { headers: {location: `/login`}, status: 302 });
}
async createPost(req, ctx){
const cookies = getCookies(req.headers);
if((cookies)&&(cookies.session_id)){
const jwt = await myredis.get(cookies.session_id);
try{
const payload = await verify(jwt, secret_key, "HS512");
if(payload.user.role in {'Admin':1,'Editor':1,'Author':1}){
await postdb.insertPost(req, payload.user.id);
}
return new Response(undefined, { headers: {location: `/admin/post`}, status: 302 });
}catch(error){
console.log(error);
const resp = new Response(undefined, { headers: {location: `/login`}, status: 302 });
deleteCookie(resp.headers, "session_id");
return resp;
}
}
return new Response(undefined, { headers: {location: `/login`}, status: 302 });
}
}
export default new Post();
GitHub: https://github.com/Sokhavuth/khmerweb-fresh
Deno Deploy: https://khmerweb.deno.dev/login