-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuserInfo.ts
45 lines (42 loc) · 1.35 KB
/
userInfo.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { Context, Next } from "hono";
import { Hono } from "hono";
import User from "./models/users/users.ts";
import Session from "./models/users/sessions.ts";
import { sessionSchema } from "./models/users/sessions.ts";
import { Env } from "./_factory.ts";
import { userSchema } from "./models/users/users.ts";
import { InferSchemaType } from "mongoose";
import { getCookie } from "hono/cookie";
type userType = InferSchemaType<typeof userSchema>;
type SessionType = InferSchemaType<typeof sessionSchema>;
export type MyEnv = {
Variables: {
// Define the key's name and expected type
user: userType;
session: SessionType;
};
Bindings: Env;
};
// Authorization ヘッダーをコンテキストに入れるミドルウェア
export const authorizationMiddleware = async (
c: Context<MyEnv>,
next: Next,
) => {
// "Authorization" ヘッダーを取得
const sessionid = getCookie(c, "sessionid");
if (!sessionid) {
return c.json({ message: "Unauthorized" }, 401);
}
const session = await Session.findOne({ sessionid: sessionid });
if (!session) {
return c.json({ message: "Unauthorized" }, 401);
}
const userInfo = await User.findOne({ userName: session.userName });
if (!userInfo) {
return c.json({ message: "server error" }, 500);
}
c.set("user", userInfo);
c.set("session", session);
// 次の処理へ
await next();
};