I have one requirement , Where after new user registration ,admin person has to approve based on his provided details in registration form. After approval only user should be able to login. Please help me achieve this requirement.
Thanks in advance…
I added one more boolean value in user schema (for ex: “approved”) and default value is false. I want to trigger an event to corresponding admin after new user registration, where admin can approve ( then “approved” value will be true), so that new user can login. User shouldn’t be able to login without (approval : true)…
how can i achieve this …
Hello, Karani Anand,
you can create beforeLogin event handler https://backendless.com/documentation/business-logic/java/bl_user_service_handler.htm#login
then write code check if approved true example for JAVA sdk 4.0 :
public class GenericUserEventHandler extends com.backendless.servercode.extension.UserExtender
{
@Override
public void beforeLogin( RunnerContext context, String login, String password ) throws Exception
{
List<BackendlessUser> users = Backendless.Data.of( BackendlessUser.class ).find( DataQueryBuilder.create().setWhereClause( "your-identity='" + login +"'" ) );
if(users.size() < 1)
return; //ignore it because system will throw correct exception
BackendlessUser user = users.get( 0 );
if(user.getProperty( "approved" ).equals( Boolean.FALSE ))
throw new BackendlessException( "User should be approved" );
//else server logic login the use
}
}
Thanks Sergey for prompt response. I would like to know one more thing… I have 0.2 million users for my App. Can i store all the User data in “User Table” using Free plan ? or else should i purchase any package? please explain.
Please can you provide me JS code for above logic… I was struck …
Thanks
Backendless.ServerCode.User.beforeLogin(function(req) {
var whereClause = “email = '” + req.login + “’ and approval=true”;
var queryBuilder = Backendless.DataQueryBuilder.create().setWhereClause( whereClause );
return Backendless.Data.of( Backendless.User ).find( queryBuilder )
.then( function( result ) {
if( result.length < 1 )
return { error: “you can not login” }
})
.catch( function( fault ) {
// an error has occurred, the error code can be retrieved with fault.statusCode
});
});
also check your app, I have changed your business logic
Thanks a lot Sergey. I am new to Backendless , That’s why i am struggling … Thanks for prompt response and your guidance… I checked it ,still not working.
Anand,
Please use the “local debugging” feature to diagnose the code and see where the logic does not work as expected. Saying that something does not work, does not help us help you in any way. Please do everything you can to figure out what’s going on and then report your findings through the support forum.
Regards,
Mark
I have checked, and it is works fine, if approval true user login if false, you will get object with error message
Thanks Sergey, It’s working now. I tested with Rest Console. I deleted all user data and tested with new user data ,it’s working fine now… Thanks for your support.
Problem is, it’s not working with User data ( which was present before Event creation)… with new data it’s working fine… i was trying with existed data whole day… Thanks Sergey once more…