-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement session but cookie don't work
- Loading branch information
Showing
5 changed files
with
80 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
"imports": { | ||
"denodb": "https://raw.githubusercontent.com/Aiko-Suzuki/denodb/master/mod.ts", | ||
"aqua": "https://deno.land/x/[email protected]/mod.ts", | ||
"nanoid": "https://deno.land/x/[email protected]/async.ts", | ||
"discordeno": "./deps.ts", | ||
"config": "./config.ts", | ||
"db": "./database/mod.ts", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
import "./sessions.ts"; | ||
import "./lore/[name].ts"; | ||
import "./login/index.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { MiddlewareType, Request } from "aqua"; | ||
import { nanoid } from "nanoid"; | ||
import app from "./app.ts"; | ||
|
||
app.register(async (req) => { | ||
if (Session.isSessionInvalid(req)) { | ||
console.log("Invalid session", req.cookies["session"]); | ||
console.log("a", req.headers.cookie); | ||
console.log("b", req.cookies); | ||
req.cookies["session"] = await nanoid(10); | ||
} | ||
return req; | ||
}, MiddlewareType.Incoming); | ||
|
||
app.register((req, res) => { | ||
if (!res.cookies) { | ||
res.cookies = {}; | ||
} | ||
if (!res.cookies["session"]) { | ||
res.cookies["session"] = req.cookies["session"]; | ||
} | ||
return res; | ||
}, MiddlewareType.Outgoing); | ||
/** | ||
* Memory-based session system. | ||
*/ | ||
export default class Session { | ||
public id: string; | ||
private static storage: Record<string, Record<string, string>> = {}; | ||
constructor(req: Request) { | ||
this.id = req.cookies["session"]; | ||
if (!this.id) { | ||
throw new Error("Session id is not defined"); | ||
} | ||
if (!Session.storage[this.id]) { | ||
Session.storage[this.id] = {}; | ||
} | ||
} | ||
|
||
public static isSessionInvalid(req: Request): boolean { | ||
const sessionId: string = req.cookies["session"] || ""; | ||
return !sessionId.match(/^[A-Za-z0-9_-]{10}$/); | ||
} | ||
|
||
set(key: string, value: string): void { | ||
Session.storage[this.id][key] = value; | ||
} | ||
get(key: string): string { | ||
return Session.storage[this.id][key]; | ||
} | ||
} |