From de0b8512171335e50797c023d3f8a8465212c751 Mon Sep 17 00:00:00 2001 From: Hide_D Date: Fri, 18 Aug 2023 13:11:59 +0000 Subject: [PATCH] =?UTF-8?q?api=20=EA=B5=AC=EC=A1=B0=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/api/Login/ChangePassword.ts | 15 +++++----- server/api/Login/LoginByID.ts | 37 +++++++++++++++--------- server/api/Login/LoginByToken.ts | 34 +++++++++++++--------- server/api/Login/ReqNonce.ts | 19 ++++++------ server/api/Login/test.ts | 11 ++++--- server/api/ProcDecorator/ReqGameLogin.ts | 2 +- server/api/ProcDecorator/ReqLogin.ts | 3 +- server/apiStructure/sammoRootAPI.ts | 2 +- 8 files changed, 70 insertions(+), 53 deletions(-) diff --git a/server/api/Login/ChangePassword.ts b/server/api/Login/ChangePassword.ts index 0007399..d8286b7 100644 --- a/server/api/Login/ChangePassword.ts +++ b/server/api/Login/ChangePassword.ts @@ -11,12 +11,11 @@ type EType = ExtractError; type QType = ExtractQuery; const argValidator = undefined; -export const ReqNonce = GET - (argValidator) - (declProcDecorators([ - StartSession, - ReqLogin, - ] as const)) - ((query, ctx) => { +export const ReqNonce = GET(argValidator)(declProcDecorators( + StartSession, + ReqLogin, +))( + (query, ctx) => { throw new Error("Method not implemented."); - }); \ No newline at end of file + } +); \ No newline at end of file diff --git a/server/api/Login/LoginByID.ts b/server/api/Login/LoginByID.ts index c1b2f7b..7f4c3da 100644 --- a/server/api/Login/LoginByID.ts +++ b/server/api/Login/LoginByID.ts @@ -5,25 +5,23 @@ import { StartSession } from "../ProcDecorator/StartSession.js"; import { IsString } from "class-validator"; import { delay } from "../../util/delay.js"; import { declProcDecorators } from "../ProcDecorator/base.js"; +import { z } from "zod"; +import { LoginCtx, loginCtxSessionKey } from "../ProcDecorator/ReqLogin.js"; type BaseAPI = typeof structure.Login.LoginByID; type RType = ExtractResponse; type EType = ExtractError; type QType = ExtractQuery; -class ArgValidator implements QType { - @IsString() - username!: string; - @IsString() - password!: string; -} +const LoginByIDReq = z.object({ + id: z.string(), + password: z.string(), +}) satisfies z.ZodType -export const LoginByID = POST - (ArgValidator) - (declProcDecorators([ - StartSession, - ] as const)) - (async (query, ctx, req, res) => { - const username = query.username; +export const LoginByID = POST(LoginByIDReq)(declProcDecorators( + StartSession, +))( + async (query, ctx, req, res) => { + const id = query.id; const password = query.password; //TODO: DB에서 뭔가 가져와야 함 @@ -46,9 +44,20 @@ export const LoginByID = POST } const userID = 1; + const userName = "test"; + const userLevel = 1; const nextToken: [number, string] = [1, "1234567890"]; + const loginCtx: LoginCtx = { + userID, + userName, + userLevel, + allowServerAction: {}, + loginDate: new Date(), + } + + ctx.session.setItem(loginCtxSessionKey, loginCtx); + - await ctx.setValue("userID", userID); return { result: true, nextToken, diff --git a/server/api/Login/LoginByToken.ts b/server/api/Login/LoginByToken.ts index f637eb4..d470b2c 100644 --- a/server/api/Login/LoginByToken.ts +++ b/server/api/Login/LoginByToken.ts @@ -5,34 +5,42 @@ import { StartSession } from "../ProcDecorator/StartSession.js"; import { IsNumber, IsString } from "class-validator"; import { delay } from "../../util/delay.js"; import { declProcDecorators } from "../ProcDecorator/base.js"; +import { LoginCtx, loginCtxSessionKey } from "../ProcDecorator/ReqLogin.js"; +import { z } from "zod"; type BaseAPI = typeof structure.Login.LoginByToken; type RType = ExtractResponse; type EType = ExtractError; type QType = ExtractQuery; -class ArgValidator implements QType { - @IsNumber() - token_id!: number; - @IsString() - hashedToken!: string; -} +const LoginByTokenReq = z.object({ + token_id: z.number(), + hashedToken: z.string(), +}) satisfies z.ZodType -export const LoginByToken = POST - (ArgValidator) - (declProcDecorators([ - StartSession, - ] as const)) +export const LoginByToken = POST(LoginByTokenReq)(declProcDecorators( + StartSession, +)) (async (query, ctx) => { query.hashedToken; - ctx.clearSession(); + ctx.session.clear(); await delay(1); //무언가 로그인 //TODO: DB는 어디서 들고옴? const userID = 1; + const userName = "test"; + const userLevel = 1; const nextToken: [number, string] = [1, "1234567890"]; - await ctx.setValue("userID", userID); + const loginCtx: LoginCtx = { + userID, + userName, + userLevel, + allowServerAction: {}, + loginDate: new Date(), + } + + ctx.session.setItem(loginCtxSessionKey, loginCtx); //throw new Error("Method not implemented."); diff --git a/server/api/Login/ReqNonce.ts b/server/api/Login/ReqNonce.ts index 6bbf168..a9a8d51 100644 --- a/server/api/Login/ReqNonce.ts +++ b/server/api/Login/ReqNonce.ts @@ -9,13 +9,13 @@ type RType = ExtractResponse; type EType = ExtractError; type QType = ExtractQuery; -export const ReqNonce = GET - (undefined) - (declProcDecorators([ - StartSession, - ] as const)) - (async (query, ctx) => { - const nonce = await ctx.getValue("nonce"); +export const ReqNonceSessionKey = 'loginNonce'; + +export const ReqNonce = GET(undefined)(declProcDecorators( + StartSession, +))( + async (query, ctx) => { + const nonce = ctx.session.getItem(ReqNonceSessionKey); if (nonce !== undefined) { return { loginNonce: nonce, @@ -24,9 +24,10 @@ export const ReqNonce = GET } const newNonce = "1234567890"; - await ctx.setValue("nonce", newNonce); + ctx.session.setItem(ReqNonceSessionKey, newNonce); return { loginNonce: newNonce, result: true, } - }); \ No newline at end of file + } +) \ No newline at end of file diff --git a/server/api/Login/test.ts b/server/api/Login/test.ts index 8b50b72..943fe42 100644 --- a/server/api/Login/test.ts +++ b/server/api/Login/test.ts @@ -1,16 +1,15 @@ import { GET } from "../defs.js"; import type { structure } from "../../apiStructure/sammoRootAPI.js"; import type { ExtractError, ExtractQuery, ExtractResponse } from "../../apiStructure/defs.js"; -import { declProcDecorators } from "../ProcDecorator/base.js"; +import { EmptyProcDecorator } from "../ProcDecorator/base.js"; type BaseAPI = typeof structure.Login.test; type RType = ExtractResponse; type EType = ExtractError; type QType = ExtractQuery; -export const test = GET - (undefined) - (declProcDecorators([] as const)) - (() => { +export const test = GET(undefined)(EmptyProcDecorator)( + () => { throw new Error("Method not implemented."); - }); \ No newline at end of file + } +); \ No newline at end of file diff --git a/server/api/ProcDecorator/ReqGameLogin.ts b/server/api/ProcDecorator/ReqGameLogin.ts index e32df85..12ff76a 100644 --- a/server/api/ProcDecorator/ReqGameLogin.ts +++ b/server/api/ProcDecorator/ReqGameLogin.ts @@ -1,6 +1,6 @@ import type { LoginCtx } from "./ReqLogin.js"; import type { SessionCtx } from "./StartSession.js"; -import type { DecoratorResult, ProcDecoratorGenerator } from "./base.js"; +import type { ProcDecoratorGenerator } from "./base.js"; export type GameLoginCtx = { generalID: number; diff --git a/server/api/ProcDecorator/ReqLogin.ts b/server/api/ProcDecorator/ReqLogin.ts index f8bbf71..ea45cbc 100644 --- a/server/api/ProcDecorator/ReqLogin.ts +++ b/server/api/ProcDecorator/ReqLogin.ts @@ -8,10 +8,11 @@ export type LoginCtx = { allowServerAction: Record; loginDate: Date; } +export const loginCtxSessionKey = 'loginCtx'; export function ReqLogin(): ProcDecorator { return (ctx) => { - const loginCtx = ctx.session.getItem('loginCtx'); + const loginCtx = ctx.session.getItem(loginCtxSessionKey); if(!loginCtx){ return [{ result: false, diff --git a/server/apiStructure/sammoRootAPI.ts b/server/apiStructure/sammoRootAPI.ts index 8279692..24a3508 100644 --- a/server/apiStructure/sammoRootAPI.ts +++ b/server/apiStructure/sammoRootAPI.ts @@ -33,7 +33,7 @@ export type AutoLoginFailed = { export const structure = { Login: { LoginByID: POST<{ - username: string, + id: string, password: string, }, LoginResponse, LoginFailed>(), LoginByToken: POST<{