Skip to content

Commit

Permalink
Manage session (#59)
Browse files Browse the repository at this point in the history
* [add] - セッション情報を取得するSocketハンドラと関数作成

* [add] - セッションデータをログアウト(削除)させる関数とSocketハンドラ作成

* [fix] - オフセットの位置調整がおかしかった

* [add] - セッション名を変更するSocketハンドラと関数作成
  • Loading branch information
NfoAlex committed Aug 10, 2024
1 parent ce00dda commit 73c6e6e
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 2 deletions.
43 changes: 43 additions & 0 deletions src/actionHandler/auth/changeSessionName.ts
Original file line number Diff line number Diff line change
@@ -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<boolean> {
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;

}
}
43 changes: 43 additions & 0 deletions src/actionHandler/auth/fetchSession.ts
Original file line number Diff line number Diff line change
@@ -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<IUserSession[] | null> {
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;

}
}
34 changes: 34 additions & 0 deletions src/actionHandler/auth/sessionLogout.ts
Original file line number Diff line number Diff line change
@@ -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<boolean> {
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;

}
}
89 changes: 87 additions & 2 deletions src/socketHandler/auth.ts
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down Expand Up @@ -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 });
}
});

});
}

0 comments on commit 73c6e6e

Please sign in to comment.