electron client auto login

This commit is contained in:
monoid 2021-01-10 14:45:34 +09:00
parent f5464b1507
commit 8d072c82c6
3 changed files with 19 additions and 2 deletions

11
app.ts
View File

@ -1,6 +1,7 @@
import { app, BrowserWindow } from "electron"; import { app, BrowserWindow, session } from "electron";
import { get_setting } from "./src/setting"; import { get_setting } from "./src/setting";
import { create_server, start_server } from "./src/server"; import { create_server, start_server } from "./src/server";
import { getAdminCookieValue } from "./src/login";
const get_loading_html = (content?:string)=> `<!DOCTYPE html> const get_loading_html = (content?:string)=> `<!DOCTYPE html>
<html lang="ko"><head> <html lang="ko"><head>
@ -46,6 +47,14 @@ if (!setting.cli) {
useContentSize: true, useContentSize: true,
}); });
await window.loadURL(`data:text/html;base64,`+Buffer.from(get_loading_html()).toString('base64')); await window.loadURL(`data:text/html;base64,`+Buffer.from(get_loading_html()).toString('base64'));
await session.defaultSession.cookies.set({
url:`http://localhost:${setting.port}`,
name:"access_token",
value:getAdminCookieValue(),
httpOnly: true,
secure: false,
sameSite:"strict"
});
try{ try{
const server = await create_server(); const server = await create_server();
start_server(server); start_server(server);

View File

@ -1 +0,0 @@
{"path":[],"localmode":true,"guest":false,"jwt_secretkey":"itsRandom","port":8080,"mode":"production","cli":true}

View File

@ -10,6 +10,15 @@ import { IUser } from './model/mod';
const loginTokenName = 'access_token' const loginTokenName = 'access_token'
export const getAdminCookieValue = ()=>{
const setting = get_setting();
const secretKey = setting.jwt_secretkey;
return sign({
username: "admin",
permission: []
},secretKey,{expiresIn:'3d'});
}
export const createLoginMiddleware = (knex: Knex)=>{ export const createLoginMiddleware = (knex: Knex)=>{
const userController = createKnexUserController(knex); const userController = createKnexUserController(knex);
return async (ctx: Koa.Context,next: Koa.Next)=>{ return async (ctx: Koa.Context,next: Koa.Next)=>{