@@ -49,26 +49,7 @@ router.post('/', async (req, res) => {
4949} ) ;
5050
5151router . get ( '/me' , auth , async ( req , res ) => {
52- //TODO get user from database
53- let user = {
54- id : 3 ,
55- username : "||" ,
56- email : "email@example.com" ,
57- num_credits : 69 ,
58- first_name : "Name" ,
59- last_name : "Name" ,
60- birth_date : "2023-10-28" ,
61- password : "coolerHash" ,
62- address : "Too lazy 4, 36912 This" ,
63- organization : false ,
64- region : 6 ,
65- join_date : "2023-10-27" ,
66- email_confirmed : true ,
67- totp_secret : "32 Zeichen" ,
68- totp_confirmed : true ,
69- banned : true ,
70- last_security_change : Date . now ( )
71- }
52+ let user = await dbAdapter . getUserById ( req . user . sub ) ;
7253 user = _ . pick ( user , [ 'id' , 'username' , 'email' , 'num_credits' , 'first_name' , 'last_name' , 'birth_date' , 'address' , 'organization' , 'region' , 'join_date' , 'email_confirmed' , 'totp_confirmed' , 'banned' , 'last_security_change' ] ) ;
7354 res . send ( user ) ;
7455} ) ;
@@ -94,21 +75,18 @@ router.put('/me/password', auth, async (req, res) => {
9475 return res . status ( 400 ) . send ( error . details [ 0 ] . message ) ;
9576 }
9677
97- req . user . sub
98- //TODO get user from database
99- //const validPassword = await bcrypt.compare(req.body.oldPassword, user.password);
100- //if(!validPassword) {
101- // debugRoute("PUT /api/users/me/password - 400 - Invalid old password");
102- // return res.status(400).send("Invalid old password");
103- //}
78+ let user = await dbAdapter . getUserById ( req . user . sub ) ;
79+ const validPassword = await bcrypt . compare ( req . body . oldPassword , user . password ) ;
80+ if ( ! validPassword ) {
81+ debugRoute ( "PUT /api/users/me/password - 400 - Invalid old password" ) ;
82+ return res . status ( 400 ) . send ( "Invalid old password" ) ;
83+ }
10484
10585 const salt = bcrypt . genSaltSync ( 10 ) ;
106- let user = { } ; //TODO remove it
107- user . sub = 1234
10886 user . password = await bcrypt . hash ( req . body . newPassword , salt ) ;
10987 user . lastSecurityUpdate = Date . now ( ) ;
11088
111- //TODO upload to server
89+ await dbAdapter . updateUser ( user ) ;
11290
11391 debugRoute ( "PUT /api/users/me/password - 200 - Password changed" ) ;
11492
@@ -177,4 +155,18 @@ router.put('/password/reset/:code', async (req, res) => {
177155 return res . status ( 200 ) . send ( "Password updated" ) ;
178156} ) ;
179157
158+ router . get ( '/id/:id' , auth , async ( req , res ) => {
159+ let user = await dbAdapter . getUserById ( req . params . id ) ;
160+
161+ if ( ! user ) {
162+ debugRoute ( "GET /api/users/user/:id - 404 - User not found" ) ;
163+ return res . status ( 404 ) . send ( "User not found" ) ;
164+ }
165+
166+ debugRoute ( "GET /api/users/user/:id - 200 - User found" ) ;
167+
168+ user = _ . pick ( user , [ 'id' , 'username' , 'num_credits' , 'organization' , 'region' , 'join_date' , 'banned' ] ) ;
169+ res . send ( user ) ;
170+ } ) ;
171+
180172module . exports = router ;
0 commit comments