From 73c6e6ec2d230482e49ce4094efb9d4eae39b60f Mon Sep 17 00:00:00 2001 From: "Alex.exe" <40430040+NfoAlex@users.noreply.github.com> Date: Sat, 10 Aug 2024 16:29:53 +0900 Subject: [PATCH] Manage session (#59) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [add] - セッション情報を取得するSocketハンドラと関数作成 * [add] - セッションデータをログアウト(削除)させる関数とSocketハンドラ作成 * [fix] - オフセットの位置調整がおかしかった * [add] - セッション名を変更するSocketハンドラと関数作成 --- src/actionHandler/auth/changeSessionName.ts | 43 ++++++++++ src/actionHandler/auth/fetchSession.ts | 43 ++++++++++ src/actionHandler/auth/sessionLogout.ts | 34 ++++++++ src/socketHandler/auth.ts | 89 ++++++++++++++++++++- 4 files changed, 207 insertions(+), 2 deletions(-) create mode 100644 src/actionHandler/auth/changeSessionName.ts create mode 100644 src/actionHandler/auth/fetchSession.ts create mode 100644 src/actionHandler/auth/sessionLogout.ts diff --git a/src/actionHandler/auth/changeSessionName.ts b/src/actionHandler/auth/changeSessionName.ts new file mode 100644 index 00000000..7212b585 --- /dev/null +++ b/src/actionHandler/auth/changeSessionName.ts @@ -0,0 +1,43 @@ +import sqlite3 from "sqlite3"; +const db = new sqlite3.Database("./records/USER.db"); + +/** + * セッション名を変更 + * @param _userId + * @param _targetSessionId + * @param _newName + * @returns + */ +export default async function changeSessionName( + _userId: string, + _targetSessionId: string, + _newName: string +):Promise { + try { + + return new Promise((resolve) => { + db.run( + ` + UPDATE USERS_SESSION SET sessionName=? + WHERE userId=? AND sessionId=? + `, + [_newName, _userId, _targetSessionId], + (err:Error) => { + //エラーハンドラ + if (err) { + console.log("changeSessionname :: db(セッション名変更) : エラー->", err); + return false; + } + + return true; + } + ); + }); + + } catch(e) { + + console.log("changeSessionname: エラー->", e); + return false; + + } +} \ No newline at end of file diff --git a/src/actionHandler/auth/fetchSession.ts b/src/actionHandler/auth/fetchSession.ts new file mode 100644 index 00000000..abe66e24 --- /dev/null +++ b/src/actionHandler/auth/fetchSession.ts @@ -0,0 +1,43 @@ +import sqlite3 from "sqlite3"; +const db = new sqlite3.Database("./records/USER.db"); + +import type { IUserSession } from "../../type/User"; + +/** + * 指定したユーザーのセッションデータを取得 + * @param _userId + * @param _indexNumber + * @returns + */ +export default async function fetchSession(_userId: string, _indexNumber: number) +:Promise { + try { + + //オフセットでずらすデータ数 + const offsetNum = 10 * (_indexNumber-1 || 0); + + return new Promise((resolve) => { + db.all( + "SELECT * FROM USERS_SESSION WHERE userId=? LIMIT 10 OFFSET ?", + [_userId, offsetNum], + (err:Error, sessionData:IUserSession[]) => { + //エラーハンドラ + if (err) { + resolve(null); + return; + } + + //データを返す + resolve(sessionData); + return; + } + ); + }); + + } catch(e) { + + console.log("fetchSession :: エラー->", e); + return null; + + } +} \ No newline at end of file diff --git a/src/actionHandler/auth/sessionLogout.ts b/src/actionHandler/auth/sessionLogout.ts new file mode 100644 index 00000000..aadb7e5e --- /dev/null +++ b/src/actionHandler/auth/sessionLogout.ts @@ -0,0 +1,34 @@ +import sqlite3 from "sqlite3"; +const db = new sqlite3.Database("./records/USER.db"); + +/** + * セッションをログアウトさせる + */ +export default async function sessionLogout(_userId:string, _targetSessionId:string):Promise { + try { + + return new Promise((resolve) => { + db.run( + "DELETE FROM USERS_SESSION WHERE userId=? AND sessionId=?", + [_userId, _targetSessionId], + (err:Error) => { + //エラーハンドラ + if (err) { + console.log("sessionLogout :: db(削除) : エラー->", err); + resolve(false); + return; + } + + resolve(true); + return; + } + ) + }) + + } catch(e) { + + console.log("sessionLogout :: エラー->", e); + return false; + + } +} \ No newline at end of file diff --git a/src/socketHandler/auth.ts b/src/socketHandler/auth.ts index baa029c3..eb957077 100644 --- a/src/socketHandler/auth.ts +++ b/src/socketHandler/auth.ts @@ -1,15 +1,18 @@ -import { Socket, Server } from "socket.io"; +import type { Socket, Server } from "socket.io"; import authLogin from "../actionHandler/auth/authLogin"; import authRegister from "../actionHandler/auth/authRegister"; -import IRequestSender from "../type/requestSender"; import changePassword from "../actionHandler/auth/changePassword"; import checkSession from "../actionHandler/auth/checkSession"; +import fetchSession from "../actionHandler/auth/fetchSession"; import { ServerInfo } from "../db/InitServer"; import fetchUser from "../actionHandler/User/fetchUser"; import addUserOnline from "../util/onlineUsers/addUserOnline"; +import type IRequestSender from "../type/requestSender"; import type { IUserInfo } from "../type/User"; +import sessionLogout from "../actionHandler/auth/sessionLogout"; +import changeSessionName from "../actionHandler/auth/changeSessionname"; module.exports = (io:Server) => { io.on("connection", (socket:Socket) => { @@ -188,5 +191,87 @@ module.exports = (io:Server) => { } }); + //セッション情報を取得 + socket.on("fetchSession", async (dat:{RequestSender:IRequestSender, indexNum:number}) => { + //セッション確認 + if (!(await checkSession(dat.RequestSender))) { + socket.emit("RESULT::fetchSession", { result:"ERROR_SESSION_ERROR", data:null }); + return; + } + + try { + //セッション情報を取得 + const sessionData = await fetchSession(dat.RequestSender.userId, dat.indexNum); + //nullじゃなければ送信 + if (sessionData !== null) { + socket.emit("RESULT::fetchSession", { result:"SUCCESS", data:sessionData }); + return; + } + + //エラーなら + socket.emit("RESULT::fetchSession", { result:"ERROR_DB_THING", data:null }); + return; + } catch(e) { + console.log("auth :: socket(fetchSession) : エラー->", e); + socket.emit("RESULT::fetchSession", { result:"ERROR_INTERNAL_THING", data:null }); + return; + } + }); + + //セッション名前 + socket.on("changeSessionName", async ( + dat: { + RequestSender: IRequestSender, + targetSessionId: string, + newName: string + } + ) => { + //セッション確認 + if (!(await checkSession(dat.RequestSender))) { + socket.emit("RESULT::changeSessionName", { result:"ERROR_SESSION_ERROR", data:null }); + return; + } + + try { + //セッション名を変更 + const changeSessionname:boolean = await changeSessionName(dat.RequestSender.userId, dat.targetSessionId, dat.newName); + //成功ならそう送信 + if (changeSessionname) { + socket.emit("RESULT::changeSessionName", { result:"SUCCESS", data:null }); + return; + } + + socket.emit("RESULT::changeSessionName", { result:"ERROR_DB_THING", data:null }); + + } catch(e) { + console.log("auth :: socket(changeSessionName) : エラー->", e); + socket.emit("RESULT::changeSessionName", { result:"ERROR_INTERNAL_THING", data:null }); + return; + } + }); + + //セッション情報をログアウトさせる + socket.on("sessionLogout", async (dat:{RequestSender: IRequestSender, targetSessionId:string}) => { + //セッション確認 + if (!(await checkSession(dat.RequestSender))) { + socket.emit("RESULT::sessionLogout", { result:"ERROR_SESSION_ERROR", data:null }); + return; + } + + try { + //セッションデータを削除する + const sessionLogoutResult = await sessionLogout(dat.RequestSender.userId, dat.targetSessionId); + + if (sessionLogoutResult) { + socket.emit("RESULT::sessionLogout", { result:"SUCCESS", data:null }); + } else { + socket.emit("RESULT::sessionLogout", { result:"ERROR_DB_THING", data:null }); + } + } catch(e) { + console.log("auth :: socket(sessionLogout) : エラー->", e); + socket.emit("RESULT::sessionLogout", { result:"ERROR_INTERNAL_THING", data:null }); + } + }); + }); }