api 구조 변경
This commit is contained in:
@@ -11,12 +11,11 @@ type EType = ExtractError<BaseAPI>;
|
||||
type QType = ExtractQuery<BaseAPI>;
|
||||
const argValidator = undefined;
|
||||
|
||||
export const ReqNonce = GET<RType, EType, QType>
|
||||
(argValidator)
|
||||
(declProcDecorators([
|
||||
StartSession,
|
||||
ReqLogin,
|
||||
] as const))
|
||||
((query, ctx) => {
|
||||
export const ReqNonce = GET<RType, EType, QType>(argValidator)(declProcDecorators(
|
||||
StartSession,
|
||||
ReqLogin,
|
||||
))(
|
||||
(query, ctx) => {
|
||||
throw new Error("Method not implemented.");
|
||||
});
|
||||
}
|
||||
);
|
||||
@@ -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<BaseAPI>;
|
||||
type EType = ExtractError<BaseAPI>;
|
||||
type QType = ExtractQuery<BaseAPI>;
|
||||
class ArgValidator implements QType {
|
||||
@IsString()
|
||||
username!: string;
|
||||
@IsString()
|
||||
password!: string;
|
||||
}
|
||||
const LoginByIDReq = z.object({
|
||||
id: z.string(),
|
||||
password: z.string(),
|
||||
}) satisfies z.ZodType<QType>
|
||||
|
||||
export const LoginByID = POST<RType, EType, QType>
|
||||
(ArgValidator)
|
||||
(declProcDecorators([
|
||||
StartSession,
|
||||
] as const))
|
||||
(async (query, ctx, req, res) => {
|
||||
const username = query.username;
|
||||
export const LoginByID = POST<RType, EType, QType>(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<RType, EType, QType>
|
||||
}
|
||||
|
||||
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,
|
||||
|
||||
@@ -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<BaseAPI>;
|
||||
type EType = ExtractError<BaseAPI>;
|
||||
type QType = ExtractQuery<BaseAPI>;
|
||||
|
||||
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<QType>
|
||||
|
||||
export const LoginByToken = POST<RType, EType, QType>
|
||||
(ArgValidator)
|
||||
(declProcDecorators([
|
||||
StartSession,
|
||||
] as const))
|
||||
export const LoginByToken = POST<RType, EType, QType>(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.");
|
||||
|
||||
@@ -9,13 +9,13 @@ type RType = ExtractResponse<BaseAPI>;
|
||||
type EType = ExtractError<BaseAPI>;
|
||||
type QType = ExtractQuery<BaseAPI>;
|
||||
|
||||
export const ReqNonce = GET<RType, EType, QType>
|
||||
(undefined)
|
||||
(declProcDecorators([
|
||||
StartSession,
|
||||
] as const))
|
||||
(async (query, ctx) => {
|
||||
const nonce = await ctx.getValue<string>("nonce");
|
||||
export const ReqNonceSessionKey = 'loginNonce';
|
||||
|
||||
export const ReqNonce = GET<RType, EType, QType>(undefined)(declProcDecorators(
|
||||
StartSession,
|
||||
))(
|
||||
async (query, ctx) => {
|
||||
const nonce = ctx.session.getItem<string>(ReqNonceSessionKey);
|
||||
if (nonce !== undefined) {
|
||||
return {
|
||||
loginNonce: nonce,
|
||||
@@ -24,9 +24,10 @@ export const ReqNonce = GET<RType, EType, QType>
|
||||
}
|
||||
|
||||
const newNonce = "1234567890";
|
||||
await ctx.setValue("nonce", newNonce);
|
||||
ctx.session.setItem(ReqNonceSessionKey, newNonce);
|
||||
return {
|
||||
loginNonce: newNonce,
|
||||
result: true,
|
||||
}
|
||||
});
|
||||
}
|
||||
)
|
||||
@@ -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<BaseAPI>;
|
||||
type EType = ExtractError<BaseAPI>;
|
||||
type QType = ExtractQuery<BaseAPI>;
|
||||
|
||||
export const test = GET<RType, EType, QType>
|
||||
(undefined)
|
||||
(declProcDecorators([] as const))
|
||||
(() => {
|
||||
export const test = GET<RType, EType, QType>(undefined)(EmptyProcDecorator)(
|
||||
() => {
|
||||
throw new Error("Method not implemented.");
|
||||
});
|
||||
}
|
||||
);
|
||||
@@ -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;
|
||||
|
||||
@@ -8,10 +8,11 @@ export type LoginCtx = {
|
||||
allowServerAction: Record<string, number>;
|
||||
loginDate: Date;
|
||||
}
|
||||
export const loginCtxSessionKey = 'loginCtx';
|
||||
|
||||
export function ReqLogin<Q extends SessionCtx>(): ProcDecorator<LoginCtx & Q, Q> {
|
||||
return (ctx) => {
|
||||
const loginCtx = ctx.session.getItem<LoginCtx>('loginCtx');
|
||||
const loginCtx = ctx.session.getItem<LoginCtx>(loginCtxSessionKey);
|
||||
if(!loginCtx){
|
||||
return [{
|
||||
result: false,
|
||||
|
||||
@@ -33,7 +33,7 @@ export type AutoLoginFailed = {
|
||||
export const structure = {
|
||||
Login: {
|
||||
LoginByID: POST<{
|
||||
username: string,
|
||||
id: string,
|
||||
password: string,
|
||||
}, LoginResponse, LoginFailed>(),
|
||||
LoginByToken: POST<{
|
||||
|
||||
Reference in New Issue
Block a user