File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -10,28 +10,32 @@ export interface AuthRequest extends Request<ParamsFlatDictionary> {
1010 } ;
1111}
1212
13+ const JWT_SECRET = process . env . JWT_SECRET ;
14+ if ( ! JWT_SECRET ) {
15+ console . error ( 'FATAL: JWT_SECRET ortam değişkeni tanımlanmamış!' ) ;
16+ process . exit ( 1 ) ;
17+ }
18+
19+ const extractToken = ( authHeader : unknown ) : string | null => {
20+ if ( typeof authHeader !== 'string' ) return null ;
21+ const parts = authHeader . split ( ' ' ) ;
22+ if ( parts . length !== 2 || parts [ 0 ] !== 'Bearer' ) return null ;
23+ return parts [ 1 ] ;
24+ } ;
25+
1326export const authenticate = async (
1427 req : AuthRequest ,
1528 res : Response ,
1629 next : NextFunction
1730) => {
1831 try {
19- const authHeader = req . headers . authorization ;
32+ const token = extractToken ( req . headers . authorization ) ;
2033
21- if ( ! authHeader || ! authHeader . startsWith ( 'Bearer ' ) ) {
34+ if ( ! token ) {
2235 return res . status ( 401 ) . json ( { error : 'Yetkilendirme gerekli' } ) ;
2336 }
2437
25- const token = authHeader . split ( ' ' ) [ 1 ] ;
26-
27- if ( ! process . env . JWT_SECRET ) {
28- console . error ( 'FATAL: JWT_SECRET ortam değişkeni tanımlanmamış!' ) ;
29- return res . status ( 500 ) . json ( { error : 'Sunucu yapılandırma hatası' } ) ;
30- }
31-
32- const secret = process . env . JWT_SECRET ;
33-
34- const decoded = jwt . verify ( token , secret ) as { userId : string ; username : string } ;
38+ const decoded = jwt . verify ( token , JWT_SECRET ) as { userId : string ; username : string } ;
3539
3640 const user = await prisma . user . findUnique ( {
3741 where : { id : decoded . userId } ,
You can’t perform that action at this time.
0 commit comments