1- import express , { Application , Request , Response } from 'express' ;
1+ import express from 'express' ;
22import cors from 'cors' ;
33import morgan from 'morgan' ;
44import bodyParser from 'body-parser' ;
55import healthRoutes from './routes/healthRoutes' ;
66import userRoutes from "./routes/user.routes" ;
7-
8-
9- // Consolidated Imports
107import productRoutes from "./routes/product.routes" ;
11- import cartRoutes from "./routes/cartRoutes" ;
8+ import cartRoutes from "./routes/cartRoutes" ;
129import mongoose from "mongoose" ;
13- import bcrypt from "bcryptjs" ;
10+
1411import cookieParser from "cookie-parser" ;
15- import { signAccessToken , signRefreshToken , verifyRefreshToken } from "./controllers/auth" ;
16- import { authenticate , AuthRequest } from "./middleware/authMiddleware" ;
1712
18- const app : Application = express ( ) ;
13+ const app = express ( ) ;
14+
1915import dotenv from "dotenv" ;
2016dotenv . config ( ) ;
2117
@@ -28,112 +24,19 @@ app.use(bodyParser.urlencoded({ extended: true }));
2824app . use ( cookieParser ( ) ) ;
2925
3026
31- // 3. User Interface and Mock Data
32- interface User {
33- id : string ;
34- username : string ;
35- passwordHash : string ;
36- role : string ;
37- }
38-
39- const users : User [ ] = [
40- { id : "1" , username : "alice" , passwordHash : bcrypt . hashSync ( "password" , 8 ) , role : "admin" } ,
41- ] ;
42-
43- const refreshTokens = new Map < string , string > ( )
44-
45-
46- // 4. Authentication API Routes
47- app . post ( "/login" , async ( req : Request , res : Response ) => {
48- const { username, password } = req . body ;
49- const user = users . find ( ( u ) => u . username === username ) ;
50- if ( ! user ) return res . status ( 401 ) . json ( { error : "Invalid credentials" } ) ;
51-
52- const match = await bcrypt . compare ( password , user . passwordHash ) ;
53- if ( ! match ) return res . status ( 401 ) . json ( { error : "Invalid credentials" } ) ;
54-
55- const payload = { sub : user . id , username : user . username , role : user . role } ;
56- const accessToken = signAccessToken ( payload ) ;
57- const refreshToken = signRefreshToken ( payload ) ;
5827
59- refreshTokens . set ( user . id , refreshToken ) ;
6028
61- res . cookie ( "refreshToken" , refreshToken , {
62- httpOnly : true ,
63- sameSite : "strict" ,
64- secure : process . env . NODE_ENV === "production" ,
65- maxAge : 7 * 24 * 60 * 60 * 1000 ,
66- } ) ;
29+ // app.get("/protected", authenticate, (req: AuthRequest, res: Response) => {
30+ // res.json({ message: "Protected route accessed", user: req.user });
31+ // });
6732
68- res . json ( { accessToken } ) ;
69- } ) ;
70-
71- app . post ( "/refresh" , ( req : Request , res : Response ) => {
72- const token = req . cookies ?. refreshToken || req . body . refreshToken ;
73-
74- if ( ! token ) {
75- return res . status ( 401 ) . json ( { error : "Refresh token is missing" } ) ;
76- }
77-
78- try {
79- const payload = verifyRefreshToken ( token ) ;
80- const storedToken = refreshTokens . get ( payload . sub ) ;
81-
82- if ( ! storedToken ) {
83- return res . status ( 401 ) . json ( { error : "Session not found or already logged out" } ) ;
84- }
85-
86- if ( storedToken !== token ) {
87- return res . status ( 401 ) . json ( { error : "Token used is not the latest valid token" } ) ;
88- }
89-
90- const cleanPayload = {
91- sub : payload . sub ,
92- username : payload . username ,
93- role : payload . role
94- } ;
95-
96- const newAccess = signAccessToken ( cleanPayload ) ;
97- const newRefresh = signRefreshToken ( cleanPayload ) ;
98-
99- refreshTokens . set ( cleanPayload . sub , newRefresh ) ;
100-
101- res . cookie ( "refreshToken" , newRefresh , {
102- httpOnly : true ,
103- sameSite : "strict" ,
104- secure : process . env . NODE_ENV === "production" ,
105- maxAge : 7 * 24 * 60 * 60 * 1000 ,
106- } ) ;
107-
108- res . json ( { accessToken : newAccess } ) ;
109-
110- } catch ( error ) {
111- console . error ( "Refresh token verification failed:" , error ) ;
112- res . status ( 401 ) . json ( { error : "Refresh token is expired or invalid" } ) ;
113- }
114- } ) ;
115-
116- app . post ( "/logout" , authenticate , ( req : AuthRequest , res : Response ) => {
117- // req.user is guaranteed to exist by the 'authenticate' middleware
118- refreshTokens . delete ( req . user ! . sub ) ;
119- res . clearCookie ( "refreshToken" ) ;
120- res . status ( 204 ) . send ( ) ;
121- } ) ;
122-
123- app . get ( "/protected" , authenticate , ( req : AuthRequest , res : Response ) => {
124- res . json ( { message : "Protected route accessed" , user : req . user } ) ;
125- } ) ;
12633
12734// Existing API Routes
12835app . use ( '/api/health' , healthRoutes ) ;
12936app . use ( "/api/products" , productRoutes ) ;
13037app . use ( "/api/users" , userRoutes ) ;
13138app . use ( "/api/cart" , cartRoutes ) ;
132-
133- // Default
134- app . get ( "/" , ( _req , res ) => {
135- res . send ( "API is running " ) ;
136- } ) ;
39+ app . use ( "/api/users" , userRoutes ) ;
13740
13841// MongoDB connect (optional)
13942const mongoUri = process . env . MONGO_URI ;
0 commit comments