File tree Expand file tree Collapse file tree 6 files changed +47
-16
lines changed Expand file tree Collapse file tree 6 files changed +47
-16
lines changed Original file line number Diff line number Diff line change 1
1
import { StatusCodes } from 'http-status-codes' ;
2
2
3
- import { COOKIE_NAME , isCookieValid } from '@/lib/cookies' ;
3
+ import { COOKIE_NAME , decodeCookie , isCookieValid } from '@/lib/cookies' ;
4
4
import { IRouteHandler } from '@/types' ;
5
+ import { logger } from '@/lib/utils' ;
5
6
6
7
export const checkIsAuthed : IRouteHandler = async ( req , res ) => {
7
8
const cookie : string | undefined = req . cookies [ COOKIE_NAME ] ;
8
9
if ( cookie && isCookieValid ( cookie ) ) {
9
- return res . status ( StatusCodes . OK ) . json ( 'OK' ) ;
10
+ const decoded = decodeCookie ( cookie ) ;
11
+ const expireTimeInMillis = decoded . exp * 1000 ;
12
+ logger . info (
13
+ '[/auth-check/check-is-authed]: Expires At ' + new Date ( expireTimeInMillis ) . toLocaleString ( )
14
+ ) ;
15
+ return res . status ( StatusCodes . OK ) . json ( {
16
+ message : 'OK' ,
17
+ expiresAt : expireTimeInMillis ,
18
+ } ) ;
10
19
}
11
20
return res . status ( StatusCodes . UNAUTHORIZED ) . json ( 'Unauthorised' ) ;
12
21
} ;
Original file line number Diff line number Diff line change @@ -15,8 +15,19 @@ export const isCookieValid = (cookie: string) => {
15
15
} ) ;
16
16
} ;
17
17
18
+ export type CookiePayload = {
19
+ id : string ;
20
+ } ;
21
+
22
+ type CookieType < T > = T & {
23
+ iat : number ;
24
+ exp : number ;
25
+ } ;
26
+
18
27
export const decodeCookie = ( cookie : string ) => {
19
- return jwt . decode ( cookie ) ;
28
+ const decoded = jwt . decode ( cookie ) as CookieType < CookiePayload > ;
29
+
30
+ return decoded ;
20
31
} ;
21
32
22
33
// TODO: Insert proper cookie validity logic and middleware
Original file line number Diff line number Diff line change @@ -3,7 +3,7 @@ import { StatusCodes } from 'http-status-codes';
3
3
4
4
import { db , users } from '@/lib/db' ;
5
5
import type { ILoginPayload , ILoginResponse } from './types' ;
6
- import { generateCookie } from '@/lib/cookies' ;
6
+ import { CookiePayload , generateCookie } from '@/lib/cookies' ;
7
7
import { getIsPasswordValid } from '@/lib/passwords' ;
8
8
9
9
const _FAILED_ATTEMPTS_ALLOWED = 3 ;
@@ -74,7 +74,7 @@ export const loginService = async (payload: ILoginPayload): Promise<ILoginRespon
74
74
unlockTime : null ,
75
75
} ) ;
76
76
}
77
- const jwtToken = generateCookie ( { id : user . id } ) ;
77
+ const jwtToken = generateCookie < CookiePayload > ( { id : user . id } ) ;
78
78
return {
79
79
code : StatusCodes . OK ,
80
80
data : {
Original file line number Diff line number Diff line change @@ -27,8 +27,10 @@ export const loader =
27
27
queryFn : async ( ) => {
28
28
return await checkIsAuthed ( ) ;
29
29
} ,
30
- staleTime : ( _query ) => {
31
- return 1000 * 60 * 30 ;
30
+ staleTime : ( { state : { data } } ) => {
31
+ const now = new Date ( ) ;
32
+ const expiresAt = data ?. expiresAt ?? now ;
33
+ return Math . max ( expiresAt . getTime ( ) - now . getTime ( ) , 0 ) ;
32
34
} ,
33
35
} ) ,
34
36
authedRoute : authedRoutes . includes ( path ) ,
@@ -43,7 +45,7 @@ export const RouteGuard = () => {
43
45
< Await resolve = { data } >
44
46
{ ( { isAuthed, authedRoute, path } ) => {
45
47
usePageTitle ( path ) ;
46
- return isAuthed ? (
48
+ return isAuthed . isAuthed ? (
47
49
authedRoute ? (
48
50
// Route is authed and user is authed - proceed
49
51
< Outlet />
Original file line number Diff line number Diff line change 1
1
import { QueryClient } from '@tanstack/react-query' ;
2
2
3
- const ONE_SECOND_IN_MILLIS = 1000 ;
3
+ // const ONE_SECOND_IN_MILLIS = 1000;
4
4
export const queryClient = new QueryClient ( {
5
- defaultOptions : {
6
- queries : {
7
- staleTime : 10 * ONE_SECOND_IN_MILLIS ,
8
- } ,
9
- } ,
5
+ // defaultOptions: {
6
+ // queries: {
7
+ // staleTime: 10 * ONE_SECOND_IN_MILLIS,
8
+ // },
9
+ // },
10
10
} ) ;
Original file line number Diff line number Diff line change @@ -33,9 +33,18 @@ export const checkIsAuthed = (param?: { signal: AbortSignal }) => {
33
33
. catch ( ( err ) => {
34
34
if ( err !== null ) {
35
35
console . error ( err ) ;
36
+ return { status : HttpStatusCode . Unauthorized , data : undefined } ;
36
37
}
37
38
} )
38
- . then ( ( response ) => {
39
- return response ?. status === HttpStatusCode . Ok ;
39
+ . then ( async ( response ) => {
40
+ if ( response && response . status < 400 ) {
41
+ return {
42
+ isAuthed : true ,
43
+ expiresAt : response . data ? new Date ( response . data . expiresAt ) : new Date ( ) ,
44
+ } ;
45
+ }
46
+ return {
47
+ isAuthed : false ,
48
+ } ;
40
49
} ) ;
41
50
} ;
You can’t perform that action at this time.
0 commit comments