From 8d072c82c6af5121db584a0d3b704cb80d632e29 Mon Sep 17 00:00:00 2001 From: monoid Date: Sun, 10 Jan 2021 14:45:34 +0900 Subject: [PATCH] electron client auto login --- app.ts | 11 ++++++++++- settings.json | 1 - src/login.ts | 9 +++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) delete mode 100644 settings.json diff --git a/app.ts b/app.ts index adac428..5e1e29d 100644 --- a/app.ts +++ b/app.ts @@ -1,6 +1,7 @@ -import { app, BrowserWindow } from "electron"; +import { app, BrowserWindow, session } from "electron"; import { get_setting } from "./src/setting"; import { create_server, start_server } from "./src/server"; +import { getAdminCookieValue } from "./src/login"; const get_loading_html = (content?:string)=> ` @@ -46,6 +47,14 @@ if (!setting.cli) { useContentSize: true, }); 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{ const server = await create_server(); start_server(server); diff --git a/settings.json b/settings.json deleted file mode 100644 index 91736e8..0000000 --- a/settings.json +++ /dev/null @@ -1 +0,0 @@ -{"path":[],"localmode":true,"guest":false,"jwt_secretkey":"itsRandom","port":8080,"mode":"production","cli":true} \ No newline at end of file diff --git a/src/login.ts b/src/login.ts index c678ec5..27478d9 100644 --- a/src/login.ts +++ b/src/login.ts @@ -10,6 +10,15 @@ import { IUser } from './model/mod'; 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)=>{ const userController = createKnexUserController(knex); return async (ctx: Koa.Context,next: Koa.Next)=>{