@@ -13,21 +13,18 @@ router.post('/login', (req, res) => {
13
13
const secret = req . app . get ( 'jwt-secret' ) ;
14
14
const data = req . body ;
15
15
try {
16
- let users = await db . user . getByUsername ( data . username ) ;
17
- if ( users . length !== 1 ) throw new Error ( 'no_user' ) ;
18
- let user = users [ 0 ] ;
16
+ let user = ( await db . user . getByUsername ( data . username ) ) [ 0 ] ;
17
+ if ( ! user ) throw new Error ( 'no_user' ) ;
19
18
if ( user . password !== hash_password ( user . username , data . password ) &&
20
19
! TwinBcrypt . compareSync ( data . password , user . password ) ) throw new Error ( 'wrong_password' ) ;
21
-
22
- let teams = await db . team . getByTeamId ( user . teamid ) ;
23
- if ( teams . length !== 1 ) throw new Error ( 'no_team' ) ;
24
- let team = teams [ 0 ] ;
20
+
21
+ let team = ( await db . team . getByTeamId ( user . teamid ) ) [ 0 ] ;
22
+ if ( ! team ) throw new Error ( 'no_team' ) ;
25
23
26
24
let contests = await db . contest . getListByTeam ( team . teamid ) ;
27
25
if ( contests . length === 0 ) throw new Error ( 'no_contest' ) ;
28
26
29
- let affils = await db . affiliation . getByAffilId ( team . affilid ) ;
30
- let affiliation = affils . length === 1 ? affils [ 0 ] : null ;
27
+ let affiliation = ( await db . affiliation . getByAffilId ( team . affilid ) ) [ 0 ] || null ;
31
28
let userdata = {
32
29
userid : user . userid ,
33
30
username : user . username ,
@@ -75,14 +72,25 @@ router.get('/logout', (req, res) => {
75
72
router . get ( '/user' , ( req , res ) => {
76
73
( async function ( req , res ) {
77
74
if ( ! req . user ) throw Error ( ) ;
78
- let users = await db . user . getByUsername ( req . user . username ) ;
79
- if ( users . length !== 1 ) throw Error ( ) ;
80
- let user = users [ 0 ] ;
75
+ let user = ( await db . user . getByUsername ( req . user . username ) ) [ 0 ] ;
76
+ if ( ! user ) throw Error ( ) ;
77
+ let team = ( await db . team . getByTeamId ( user . teamid ) ) [ 0 ] ;
78
+ if ( ! team ) throw Error ( ) ;
79
+ let affiliation = ( await db . affiliation . getByAffilId ( team . affilid ) ) [ 0 ] || null ;
81
80
82
- let teams = await db . team . getByTeamId ( user . teamid ) ;
83
- if ( teams . length !== 1 ) throw Error ( ) ;
84
-
85
- res . json ( req . user ) ;
81
+ let userdata = {
82
+ userid : user . userid ,
83
+ username : user . username ,
84
+ name : user . name ,
85
+ teamname : team . name ,
86
+ teamid : team . teamid ,
87
+ affiliation : affiliation
88
+ } ;
89
+ // If userdata has been updated
90
+ if ( JSON . stringify ( userdata ) !== JSON . stringify ( req . user ) )
91
+ throw Error ( ) ; // reject the token
92
+
93
+ res . send ( req . user ) ;
86
94
} ) ( req , res )
87
95
. catch ( ( ) => res . json ( null ) ) ;
88
96
} ) ;
0 commit comments