Skip to content

Commit 0de45d5

Browse files
committed
Merge branch 'develop'
2 parents 31c901f + df93337 commit 0de45d5

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

server/src/controllers/auth.controller.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@ import dotenv from 'dotenv';
66
dotenv.config({ path: path.resolve(__dirname, '../../..', '.env.local') });
77

88
const isProd = process.env.NODE_ENV === 'production';
9+
const COOKIE_NAME = process.env.COOKIE_NAME || 'access_token';
910
const COOKIE_DOMAIN = process.env.COOKIE_DOMAIN || undefined; // เช่น ".example.com"
10-
const cookieOpts = {
11+
const COOKIE_SAMESITE = (process.env.COOKIE_SAMESITE as 'lax' | 'none' | 'strict') || 'lax';
12+
const COOKIE_MAX_AGE_MS = Number(process.env.COOKIE_MAX_AGE_MS ?? 60 * 60 * 1000);
13+
14+
const cookieBase = {
1115
httpOnly: true,
1216
secure: isProd,
13-
sameSite: (process.env.COOKIE_SAMESITE as any) || (isProd ? 'lax' : 'lax'),
17+
sameSite: COOKIE_SAMESITE,
1418
path: '/',
1519
domain: COOKIE_DOMAIN,
16-
};
20+
} as const;
1721

1822
/**
1923
* จัดการการเข้าสู่ระบบของผู้ใช้
@@ -38,11 +42,7 @@ export async function login(req: Request, res: Response, next: NextFunction) {
3842

3943
const token = AuthService.createSessionToken({ id: user.usr_id, role: user.usr_role }); // Generate a token for the user
4044

41-
// ใช้ option เดียวกับด้านบนของไฟล์
42-
res.cookie('access_token', token, {
43-
...cookieOpts,
44-
maxAge: 60 * 60 * 1000, // หรืออ่านจาก ENV: Number(process.env.COOKIE_MAX_AGE_MS) ?? 3600000
45-
});
45+
res.cookie(COOKIE_NAME, token, { ...cookieBase, maxAge: COOKIE_MAX_AGE_MS });
4646

4747
return res.json({ message: 'Login successful', success: true, user });
4848
} catch (err) {
@@ -65,9 +65,19 @@ export async function login(req: Request, res: Response, next: NextFunction) {
6565
*/
6666
export async function logout(req: Request, res: Response, next: NextFunction) {
6767
try {
68-
// ลบด้วย option เดิมทุกตัว + เขียนทับให้หมดอายุ
69-
res.clearCookie('access_token', cookieOpts);
70-
res.cookie('access_token', '', { ...cookieOpts, maxAge: 0 });
68+
// ✅ เวอร์ชันต่าง ๆ ที่อาจหลงเหลือมาจากการตั้งค่าเก่า
69+
const variants = [
70+
{ ...cookieBase }, // มี domain (ถ้าตั้ง)
71+
{ path: '/' }, // host-only (ไม่มี domain)
72+
{ path: '/api' }, // เผื่อเคยตั้ง path = /api
73+
...(COOKIE_DOMAIN ? [{ path: '/', domain: undefined as any }] : []), // ลบ host-only แม้ปัจจุบันมี domain
74+
];
75+
76+
for (const v of variants) {
77+
res.clearCookie(COOKIE_NAME, v);
78+
res.cookie(COOKIE_NAME, '', { ...v, maxAge: 0 });
79+
}
80+
7181
return res.json({ message: 'Logout successful' });
7282
} catch (err) {
7383
next(err);
@@ -121,7 +131,7 @@ export async function me(req: Request, res: Response, next: NextFunction) {
121131
'Pragma': 'no-cache',
122132
'Vary': 'Cookie',
123133
});
124-
134+
125135
try {
126136
const token = req.cookies?.access_token;
127137
if (!token) return res.status(401).json({ error: 'Unauthenticated' });

0 commit comments

Comments
 (0)