11import { Request , Response , NextFunction } from 'express' ;
2- import { verifySessionToken , getUserSafeById , verifyPassword } from '../services/auth.service' ;
2+ import { verifySessionToken , getUserSafeById , verifyPassword } from '../services/auth.service' ;
33import * as AuthService from '../services/auth.service' ;
44import path from 'path' ;
55import dotenv from 'dotenv' ;
66dotenv . config ( { path : path . resolve ( __dirname , '../../..' , '.env.local' ) } ) ;
77
8+ const isProd = process . env . NODE_ENV === 'production' ;
9+ const COOKIE_DOMAIN = process . env . COOKIE_DOMAIN || undefined ; // เช่น ".example.com"
10+ const cookieOpts = {
11+ httpOnly : true ,
12+ secure : isProd ,
13+ sameSite : ( process . env . COOKIE_SAMESITE as any ) || ( isProd ? 'lax' : 'lax' ) ,
14+ path : '/' ,
15+ domain : COOKIE_DOMAIN ,
16+ } ;
17+
818/**
919 * จัดการการเข้าสู่ระบบของผู้ใช้
1020 *
@@ -20,28 +30,24 @@ dotenv.config({ path: path.resolve(__dirname, '../../..', '.env.local') });
2030 * @author Wanasart
2131 */
2232export async function login ( req : Request , res : Response , next : NextFunction ) {
23- console . log ( 'LOGIN body:' , req . body ) ;
24-
25- try {
26- const { usernameOrEmail, password } = req . body ;
27- const user = await AuthService . authenticateUser ( usernameOrEmail , password ) ;
28-
29- const token = AuthService . createSessionToken ( { id : user . usr_id , role : user . usr_role } ) ; // Generate a token for the user
30-
31- const isProd = process . env . NODE_ENV === 'production' ;
32- res . cookie ( 'access_token' , token , {
33- httpOnly : true , // Prevents client-side JavaScript from accessing the token
34- secure : isProd , // Use secure cookies in production
35- sameSite : 'lax' , // Helps prevent CSRF attacks
36- path : '/' , // Cookie is accessible on all routes
37- // domain: 'dekdee2.informatics.buu.ac.th', // ใส่เมื่อโปรดักชันถ้าจำเป็น
38- maxAge : 60 * 60 * 1000 // 1 hour expiration
39- } ) ;
40-
41- return res . json ( { message : 'Login successful' , success : true , user } ) ;
42- } catch ( err ) {
43- next ( err ) ;
44- }
33+ console . log ( 'LOGIN body:' , req . body ) ;
34+
35+ try {
36+ const { usernameOrEmail, password } = req . body ;
37+ const user = await AuthService . authenticateUser ( usernameOrEmail , password ) ;
38+
39+ const token = AuthService . createSessionToken ( { id : user . usr_id , role : user . usr_role } ) ; // Generate a token for the user
40+
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+ } ) ;
46+
47+ return res . json ( { message : 'Login successful' , success : true , user } ) ;
48+ } catch ( err ) {
49+ next ( err ) ;
50+ }
4551}
4652
4753/**
@@ -58,13 +64,14 @@ export async function login(req: Request, res: Response, next: NextFunction) {
5864 * @author Wanasart
5965 */
6066export async function logout ( req : Request , res : Response , next : NextFunction ) {
61- try {
62- // Clear the session or token here
63- res . clearCookie ( 'access_token' , { path : '/' } ) ;
64- return res . json ( { message : 'Logout successful' } ) ;
65- } catch ( err ) {
66- next ( err ) ;
67- }
67+ try {
68+ // ลบด้วย option เดิมทุกตัว + เขียนทับให้หมดอายุ
69+ res . clearCookie ( 'access_token' , cookieOpts ) ;
70+ res . cookie ( 'access_token' , '' , { ...cookieOpts , maxAge : 0 } ) ;
71+ return res . json ( { message : 'Logout successful' } ) ;
72+ } catch ( err ) {
73+ next ( err ) ;
74+ }
6875}
6976
7077/**
@@ -83,15 +90,15 @@ export async function logout(req: Request, res: Response, next: NextFunction) {
8390 * @author Wanasart
8491 */
8592export async function register ( req : Request , res : Response , next : NextFunction ) {
86- try {
87- const { username, email, password, role } = req . body ;
93+ try {
94+ const { username, email, password, role } = req . body ;
8895
89- const { user, token } = await AuthService . registerUser ( username , email , password , role ) ;
96+ const { user, token } = await AuthService . registerUser ( username , email , password , role ) ;
9097
91- return res . json ( { message : 'Register successful' , user, token } ) ;
92- } catch ( err ) {
93- next ( err ) ;
94- }
98+ return res . json ( { message : 'Register successful' , user, token } ) ;
99+ } catch ( err ) {
100+ next ( err ) ;
101+ }
95102}
96103
97104/**
@@ -109,6 +116,12 @@ export async function register(req: Request, res: Response, next: NextFunction)
109116 * @author Wanasart
110117 */
111118export async function me ( req : Request , res : Response , next : NextFunction ) {
119+ res . set ( {
120+ 'Cache-Control' : 'no-store' ,
121+ 'Pragma' : 'no-cache' ,
122+ 'Vary' : 'Cookie' ,
123+ } ) ;
124+
112125 try {
113126 const token = req . cookies ?. access_token ;
114127 if ( ! token ) return res . status ( 401 ) . json ( { error : 'Unauthenticated' } ) ;
@@ -158,7 +171,7 @@ export async function recheckPassword(req: Request, res: Response) {
158171 if ( ! ok ) {
159172 return res . status ( 401 ) . json ( { error : "Password incorrect" } ) ;
160173 }
161-
174+
162175 return res . json ( { success : true } ) ;
163176 } catch ( err ) {
164177 console . error ( "recheckPassword error:" , err ) ;
0 commit comments