@@ -6,6 +6,21 @@ const jwt = require('jsonwebtoken');
66const { Owner } = require ( "../models/OwnerSchema" ) ;
77const { JWT_SECRET } = require ( '../constants/config' ) ;
88
9+
10+ function prepareJWT ( owner ) {
11+ return jwt . sign (
12+ {
13+ _id : owner . _id ,
14+ restaurant_name : owner . restaurant_name ,
15+ address : owner . address ,
16+ phone : owner . phone ,
17+ gstin : owner . gstin ,
18+ } ,
19+ JWT_SECRET ,
20+ { expiresIn : '2d' }
21+ ) ;
22+ }
23+
924router . post ( '/api/auth/signup' , async ( req , res ) => {
1025 try {
1126 // Check for mandatory fields
@@ -19,12 +34,35 @@ router.post('/api/auth/signup', async (req, res) => {
1934
2035 // Create new owner object
2136 const owner = new Owner ( req . body ) ;
37+
38+ console . log ( "New owner" , owner ) ;
2239
2340 // Save owner to database
2441 const savedOwner = await owner . save ( ) ;
25-
26- // Return success response with ID of new owner
27- res . status ( 201 ) . json ( { message : 'New owner created successfully' , id : savedOwner . _id } ) ;
42+
43+ // Prepare the JWT with the owner's fields
44+ const token = prepareJWT ( savedOwner ) ;
45+
46+ // Send the JWT in a cookie
47+ // res.cookie('jwt', token, {
48+ // // httpOnly: true,
49+ // maxAge: 2 * 24 * 60 * 60 * 1000, // 2 days
50+ // sameSite: 'lax',
51+ // });
52+
53+ // Return a success response with the owner's fields
54+ return res . status ( 201 ) . json ( {
55+ success : true ,
56+ message : "New owner created successfully" ,
57+ profile : {
58+ _id : savedOwner . _id ,
59+ restaurant_name : savedOwner . restaurant_name ,
60+ address : savedOwner . address ,
61+ phone : savedOwner . phone ,
62+ gstin : savedOwner . gstin ,
63+ } ,
64+ token,
65+ } ) ;
2866 } catch ( err ) {
2967 // Handle errors
3068 console . error ( err ) ;
@@ -64,17 +102,7 @@ router.post('/api/auth/login', async (req, res) => {
64102 }
65103
66104 // Prepare the JWT with the owner's fields
67- const token = jwt . sign (
68- {
69- _id : owner . _id ,
70- restaurant_name : owner . restaurant_name ,
71- address : owner . address ,
72- phone : owner . phone ,
73- gstin : owner . gstin ,
74- } ,
75- JWT_SECRET ,
76- { expiresIn : '2d' }
77- ) ;
105+ const token = prepareJWT ( owner ) ;
78106
79107 // Send the JWT in a cookie
80108 // res.cookie('jwt', token, {
0 commit comments