File tree Expand file tree Collapse file tree 3 files changed +25
-12
lines changed
Expand file tree Collapse file tree 3 files changed +25
-12
lines changed Original file line number Diff line number Diff line change 11import { NextFunction , Request , Response } from 'express' ;
22import { check , validationResult } from 'express-validator' ;
3- import { API_VAR } from '../../index.js' ;
43import { formatError } from '../utils/format_log.js' ;
4+ import { isValidClientVersion } from '../utils/app.js' ;
55
66export const appVersionChecker = ( ) => {
77 return async ( req : Request , res : Response , next : NextFunction ) => {
@@ -30,20 +30,16 @@ export const appVersionChecker = () => {
3030 return ;
3131 }
3232
33- const appMinimum = API_VAR . MINIMUM_APP_VERSION ;
33+ const validVersion = isValidClientVersion ( appVersion ) ;
3434
35- const majorOK = + appVersion . split ( '.' ) [ 0 ] > + appMinimum . split ( '.' ) [ 0 ] ;
36- const minorOK = + appVersion . split ( '.' ) [ 1 ] > + appMinimum . split ( '.' ) [ 1 ] ;
37- const patchOK = + appVersion . split ( '.' ) [ 2 ] > + appMinimum . split ( '.' ) [ 2 ] ;
38-
39- if ( appMinimum === appVersion || majorOK || ( ! majorOK && minorOK ) || ( ! minorOK && patchOK ) ) {
40- next ( ) ;
35+ if ( ! validVersion ) {
36+ res . locals . type = 'warn' ;
37+ res . locals . message = `client version outdated` ;
38+ res . status ( 400 ) . json ( { message : 'CLIENT_VERSION_OUTDATED' } ) ;
4139 return ;
4240 }
4341
44- res . locals . type = 'warn' ;
45- res . locals . message = `client version outdated` ;
46- res . status ( 400 ) . json ( { message : 'CLIENT_VERSION_OUTDATED' } ) ;
42+ next ( ) ;
4743 } catch ( err ) {
4844 next ( err ) ;
4945 }
Original file line number Diff line number Diff line change @@ -50,7 +50,7 @@ router.get('/logout', logoutAdmin);
5050router . get ( '/client-version' , getClientVersion ) ;
5151
5252// get minimum client
53- router . post ( '/client-version' , updateClientVersion ) ;
53+ router . post ( '/client-version' , body ( 'version' ) . isString ( ) . notEmpty ( ) , updateClientVersion ) ;
5454
5555// create new congregation
5656router . post (
Original file line number Diff line number Diff line change 11import { CookieOptions , Request } from 'express' ;
2+ import { API_VAR } from '../../index.js' ;
23
34const isLocalRequest = ( req : Request ) => {
45 const host = req . get ( 'host' ) ;
@@ -16,3 +17,19 @@ export const cookieOptions = (req: Request): CookieOptions => {
1617 maxAge : 400 * 24 * 60 * 60 * 1000 ,
1718 } ;
1819} ;
20+
21+ export const isValidClientVersion = ( version : string ) => {
22+ const parts1 = version . split ( '.' ) . map ( Number ) ;
23+ const parts2 = API_VAR . MINIMUM_APP_VERSION . split ( '.' ) . map ( Number ) ;
24+
25+ const maxLength = Math . max ( parts1 . length , parts2 . length ) ;
26+ for ( let i = 0 ; i < maxLength ; i ++ ) {
27+ const num1 = parts1 [ i ] ?? 0 ;
28+ const num2 = parts2 [ i ] ?? 0 ;
29+
30+ if ( num1 > num2 ) return true ;
31+ if ( num1 < num2 ) return false ;
32+ }
33+
34+ return true ;
35+ } ;
You can’t perform that action at this time.
0 commit comments