// routes/login.tsx

/** @jsx h */
import { h } from "preact";
import { setting } from 'setting';
import { Handlers, PageProps } from "$fresh/server.ts";
import Login from '../components/front/login.tsx';
import userdb from "../models/user.ts";

userdb.createRootUser();

export const handler: Handlers = {
  async GET(req, ctx) {
    return await ctx.render({ "setting":setting() });
  },
}


export default function Template(props: PageProps){
  return (
      <Login data={props.data} />
  )
}

 

// models/user.ts

import { mongo_db } from "setting";
import { bcrypt } from "bcrypt";


interface UserSchema {
    _id: ObjectId;
    id: string; 
    title: string;
    content: string;
    thumb: string;
    date: string;
    role: string;
    email: string;
    password: string;
}

class User{
    async createRootUser(){
        const id = crypto.randomUUID();
        const salt = await bcrypt.genSalt(8);
        const hashPassword = bcrypt.hashSync('xxxxxxxxxxxxxxxxxx', salt);
        const newUser = {
            id: id, 
            title: 'Guest',
            content: '',
            thumb: '',
            date: '',
            role: 'Guest',
            email: 'guest@khmerweb.app',
            password: hashPassword,
        }
     
        const users = mongo_db.collection<UserSchema>("users");
        await users.insertOne(newUser);
    }
}

export default new User();

 

GitHub: https://github.com/Sokhavuth/khmerweb-fresh

Deno Deploy: https://khmerweb.deno.dev/login