@@ -2,7 +2,6 @@ import { isNil } from 'lodash-es';
22import { AuthUtils } from '../utils/index.js' ;
33import { TokenType } from '../constants/index.js' ;
44import { v4 as uuid } from 'uuid' ;
5- import { Config } from '../config/index.js' ;
65import {
76 ConduitGrpcSdk ,
87 ConduitRouteActions ,
@@ -26,6 +25,7 @@ import {
2625} from '@conduitplatform/module-tools' ;
2726import { createHash } from 'crypto' ;
2827import { merge } from 'lodash-es' ;
28+ import { authenticateChecks , changePassword } from './utils.js' ;
2929
3030export class LocalHandlers implements IAuthenticationStrategy {
3131 private emailModule : Email ;
@@ -156,7 +156,7 @@ export class LocalHandlers implements IAuthenticationStrategy {
156156 middlewares : [ 'authMiddleware' , 'denyAnonymousMiddleware' ] ,
157157 } ,
158158 new ConduitRouteReturnDefinition ( 'ChangePasswordResponse' , 'String' ) ,
159- this . changePassword . bind ( this ) ,
159+ changePassword . bind ( this ) ,
160160 ) ;
161161
162162 routingManager . route (
@@ -387,7 +387,7 @@ export class LocalHandlers implements IAuthenticationStrategy {
387387 ) ;
388388 if ( isNil ( user ) )
389389 throw new GrpcError ( status . UNAUTHENTICATED , 'Invalid login credentials' ) ;
390- await this . _authenticateChecks ( password , config , user ) ;
390+ await authenticateChecks ( password , config , user ) ;
391391 ConduitGrpcSdk . Metrics ?. increment ( 'logged_in_users_total' ) ;
392392 return TokenProvider . getInstance ( ) . provideUserTokens ( {
393393 user,
@@ -503,20 +503,6 @@ export class LocalHandlers implements IAuthenticationStrategy {
503503 return 'Password reset successful' ;
504504 }
505505
506- async changePassword ( call : ParsedRouterRequest ) : Promise < UnparsedRouterResponse > {
507- if ( ! call . request . context . jwtPayload . sudo ) {
508- throw new GrpcError (
509- status . PERMISSION_DENIED ,
510- 'Re-login required to enter sudo mode' ,
511- ) ;
512- }
513- const { user } = call . request . context ;
514- const { newPassword } = call . request . bodyParams ;
515- const hashedPassword = await AuthUtils . hashPassword ( newPassword ) ;
516- await User . getInstance ( ) . findByIdAndUpdate ( user . _id , { hashedPassword } ) ;
517- return 'Password changed successfully' ;
518- }
519-
520506 async changeEmail ( call : ParsedRouterRequest ) : Promise < UnparsedRouterResponse > {
521507 if ( ! call . request . context . jwtPayload . sudo ) {
522508 throw new GrpcError (
@@ -723,25 +709,6 @@ export class LocalHandlers implements IAuthenticationStrategy {
723709 return 'OK' ;
724710 }
725711
726- private async _authenticateChecks ( password : string , config : Config , user : User ) {
727- if ( ! user . active ) throw new GrpcError ( status . PERMISSION_DENIED , 'Inactive user' ) ;
728- if ( ! user . hashedPassword )
729- throw new GrpcError (
730- status . PERMISSION_DENIED ,
731- 'User does not use password authentication' ,
732- ) ;
733- const passwordsMatch = await AuthUtils . checkPassword ( password , user . hashedPassword ) ;
734- if ( ! passwordsMatch )
735- throw new GrpcError ( status . UNAUTHENTICATED , 'Invalid login credentials' ) ;
736-
737- if ( config . local . verification . required && ! user . isVerified ) {
738- throw new GrpcError (
739- status . PERMISSION_DENIED ,
740- 'You must verify your account to login' ,
741- ) ;
742- }
743- }
744-
745712 private async initDbAndEmail ( ) {
746713 const config = ConfigController . getInstance ( ) . config ;
747714
0 commit comments