@@ -373,7 +373,7 @@ export function authRoutes(fastify: FastifyInstance) {
373373 reply . send ( {
374374 oauth : true ,
375375 success : true ,
376- ouath_url : `${ url } ?client_id=${ oauth . clientId } &redirect_uri=${ oauth . redirectUri } &state= ${ email } & login=${ email } &scope=user` ,
376+ ouath_url : `${ url } ?client_id=${ oauth . clientId } &redirect_uri=${ oauth . redirectUri } &login=${ email } &scope=user` ,
377377 } ) ;
378378 }
379379 ) ;
@@ -384,62 +384,73 @@ export function authRoutes(fastify: FastifyInstance) {
384384 async ( request : FastifyRequest , reply : FastifyReply ) => {
385385 const { code, state } = request . query as { code : string ; state : string } ;
386386
387- const provider = await prisma . provider . findFirst ( { } ) ;
387+ console . log ( "HIT" ) ;
388388
389- const data = await axios . post (
390- `https://github.com/login/oauth/access_token` ,
391- {
392- client_id : provider ?. clientId ,
393- client_secret : provider ?. clientSecret ,
394- code : code ,
395- redirect_uri : provider ?. redirectUri ,
396- } ,
397- {
398- headers : {
399- Accept : "application/json" ,
400- } ,
401- }
402- ) ;
389+ try {
390+ const provider = await prisma . provider . findFirst ( { } ) ;
391+ if ( ! provider ) throw new Error ( "Provider not found" ) ;
403392
404- const access_token = data . data ;
393+ const { clientId , clientSecret , redirectUri } = provider ;
405394
406- if ( access_token ) {
407- const gh = await axios . get ( `https://api.github.com/user/emails` , {
408- headers : {
409- Accept : "application/vnd.github+json" ,
410- Authorization : `token ${ access_token . access_token } ` ,
395+ const { data : github_user } = await axios . post (
396+ `https://github.com/login/oauth/access_token` ,
397+ {
398+ client_id : clientId ,
399+ client_secret : clientSecret ,
400+ code : code ,
401+ redirect_uri : redirectUri ,
411402 } ,
412- } ) ;
403+ {
404+ headers : {
405+ Accept : "application/json" ,
406+ } ,
407+ }
408+ ) ;
409+
410+ console . log ( github_user ) ;
411+
412+ // await new Promise((r) => setTimeout(r, 2000));
413+
414+ const { data : emails } = await axios . get (
415+ `https://api.github.com/user/emails` ,
416+ {
417+ headers : {
418+ Accept : "application/vnd.github+json" ,
419+ Authorization : `Bearer ${ github_user . access_token } ` ,
420+ } ,
421+ }
422+ ) ;
413423
414- const emails = gh . data ;
424+ console . log ( emails ) ;
415425
416- const filter = emails . filter ( ( e : any ) => e . primary === true ) ;
426+ const primaryEmail = emails . find ( ( e : any ) => e . primary === true ) ?. email ;
427+ if ( ! primaryEmail ) throw new Error ( "Primary email not found" ) ;
417428
418429 let user = await prisma . user . findUnique ( {
419- where : { email : filter [ 0 ] . email } ,
430+ where : { email : primaryEmail } ,
420431 } ) ;
421432
422433 if ( ! user ) {
423- reply . send ( {
434+ return reply . send ( {
424435 success : false ,
425436 message : "Invalid email" ,
426437 } ) ;
427438 }
428439
429440 var b64string = process . env . SECRET ;
430- var buf = new Buffer ( b64string ! , "base64" ) ; // Ta-da
441+ var secret = new Buffer ( b64string ! , "base64" ) ; // Ta-da
431442
432443 let token = jwt . sign (
433444 {
434- data : { id : user ! . id } ,
445+ data : { id : user . id } ,
435446 } ,
436- buf ,
447+ secret ,
437448 { expiresIn : "8h" }
438449 ) ;
439450
440451 await prisma . session . create ( {
441452 data : {
442- userId : user ! . id ,
453+ userId : user . id ,
443454 sessionToken : token ,
444455 expires : new Date ( Date . now ( ) + 8 * 60 * 60 * 1000 ) ,
445456 } ,
@@ -449,9 +460,11 @@ export function authRoutes(fastify: FastifyInstance) {
449460 token,
450461 success : true ,
451462 } ) ;
452- } else {
463+ } catch ( error : any ) {
464+ console . error ( "Authentication error:" , error ) ;
453465 reply . status ( 403 ) . send ( {
454466 success : false ,
467+ message : error . message || "Authentication failed" ,
455468 } ) ;
456469 }
457470 }
@@ -577,6 +590,57 @@ export function authRoutes(fastify: FastifyInstance) {
577590 }
578591 ) ;
579592
593+ // Reset password by admin
594+ fastify . post (
595+ "/api/v1/auth/admin/reset-password" ,
596+ async ( request : FastifyRequest , reply : FastifyReply ) => {
597+ let { password, user } = request . body as {
598+ password : string ;
599+ user : string ;
600+ } ;
601+
602+ console . log ( user ) ;
603+
604+ const bearer = request . headers . authorization ! . split ( " " ) [ 1 ] ;
605+ const token = checkToken ( bearer ) ;
606+
607+ if ( token ) {
608+ let session = await prisma . session . findUnique ( {
609+ where : {
610+ sessionToken : bearer ,
611+ } ,
612+ } ) ;
613+
614+ const check = await prisma . user . findUnique ( {
615+ where : { id : session ?. userId } ,
616+ } ) ;
617+
618+ if ( check ?. isAdmin === false ) {
619+ reply . code ( 401 ) . send ( {
620+ message : "Unauthorized" ,
621+ } ) ;
622+ }
623+
624+ const hashedPass = await bcrypt . hash ( password , 10 ) ;
625+
626+ await prisma . user . update ( {
627+ where : { id : user } ,
628+ data : {
629+ password : hashedPass ,
630+ } ,
631+ } ) ;
632+
633+ reply . send ( {
634+ success : true ,
635+ } ) ;
636+ } else {
637+ reply . send ( {
638+ success : false ,
639+ } ) ;
640+ }
641+ }
642+ ) ;
643+
580644 // Update a users profile/config
581645 fastify . put (
582646 "/api/v1/auth/profile" ,
0 commit comments