File tree Expand file tree Collapse file tree 2 files changed +17
-11
lines changed
Expand file tree Collapse file tree 2 files changed +17
-11
lines changed Original file line number Diff line number Diff line change 11import { Elysia , status , t } from '../src'
22
3- status ( 401 )
4-
5- const app = new Elysia ( )
6- . macro ( {
7- multiple : {
8- resolve ( { status } ) {
9- if ( Math . random ( ) > 0.5 ) return status ( 401 )
10- return status ( 403 )
3+ const auth = ( app : Elysia ) =>
4+ app . derive ( ( { headers, status } ) => {
5+ try {
6+ const token = headers [ 'authorization' ] ?. replace ( 'Bearer ' , '' ) || ''
7+ return {
8+ isAuthenticated : true
119 }
10+ } catch ( e ) {
11+ const error = e as Error
12+ console . error ( 'Authentication error:' , error . message )
13+ return status ( 401 , 'Unauthorized' )
1214 }
1315 } )
14- . get ( '/multiple' , ( ) => 'Ok' , { multiple : true } )
1516
16- app [ '~Routes' ] [ 'multiple' ] [ 'get' ] [ 'response' ]
17+ const app = new Elysia ( )
18+ . use ( auth )
19+ . get ( '/' , ( { isAuthenticated } ) => isAuthenticated )
20+ . listen ( 5000 )
21+
22+ app [ '~Routes' ]
Original file line number Diff line number Diff line change 11{
22 "name" : " elysia" ,
33 "description" : " Ergonomic Framework for Human" ,
4- "version" : " 1.4.15 " ,
4+ "version" : " 1.4.16 " ,
55 "author" : {
66 "name" : " saltyAom" ,
77 "url" : " https://github.com/SaltyAom" ,
You can’t perform that action at this time.
0 commit comments