This repository was archived by the owner on Jan 20, 2020. It is now read-only.
File tree 2 files changed +19
-1
lines changed
2 files changed +19
-1
lines changed Original file line number Diff line number Diff line change @@ -47,12 +47,23 @@ const generateToken = async (username, password) => {
47
47
} ;
48
48
49
49
const validateToken = async token => {
50
- const decodedToken = decodeJWT ( token ) ;
50
+ let decodedToken = '' ;
51
+
52
+ // Check if token can be decoded, is valid format
53
+ try {
54
+ decodedToken = decodeJWT ( token ) ;
55
+ } catch ( err ) {
56
+ return false ;
57
+ }
58
+
59
+ // Check if token has not expired
51
60
try {
52
61
validateJWT ( token ) ;
53
62
} catch ( err ) {
54
63
return false ;
55
64
}
65
+
66
+ // Check if user from payload exists
56
67
const tokenUserID = decodedToken . data . id ;
57
68
const tokenUser = await Users . getUser ( tokenUserID ) ;
58
69
if ( ! tokenUser ) {
Original file line number Diff line number Diff line change @@ -86,6 +86,13 @@ describe('Auth Controller', () => {
86
86
expect ( invalidTokenIsValid ) . toBe ( false ) ;
87
87
} ) ;
88
88
89
+ test ( 'validateToken should fail if token is invalid format' , async ( ) => {
90
+ expect . assertions ( 1 ) ;
91
+ const invalidToken = 'thisisaninvalidtoken' ;
92
+ const invalidTokenIsValid = await validateToken ( invalidToken ) ;
93
+ expect ( invalidTokenIsValid ) . toBe ( false ) ;
94
+ } ) ;
95
+
89
96
test ( 'validateToken should fail if token has expired' , async ( ) => {
90
97
expect . assertions ( 1 ) ;
91
98
const addedUser = await addUser ( testUser2 ) ;
You can’t perform that action at this time.
0 commit comments