Configure the Basic I/O Function

Now, we will begin coding the authorization portal application by configuring the function component.

The function’s directory, (functions/authorization_portal_function), contains:

  • The Custom_User_Validation.java main function file
  • The catalyst-config.json configuration file
  • Java library files in the lib directory
  • .classpath and .project dependency files

You will be adding code in the Custom_User_Validation.java file.

The Basic I/O function contains the following functionalities:

  • The end-user’s details will provided as a JSON input to the Custom User Validation function.
  • The function is coded to implement a custom logic based upon which the end-user will either be authenticated or denied.
Note: For the purpose of this tutorial, we have coded a logic for the Custom User Validation function where if the user's email provider is anyone other than Zylker Technology (@zylker.com) the end-user will not be able to sign up to the application. You can either use the same or code a logic of your preference.

Now, let’s begin coding the Basic I/O function.

Copy the code given below and paste it in the Custom_User_Validation.java file in the functions/authorization_portal_function directory of your project, and save the file. You can use any IDE of your choice to work with the application’s files.

Note: Please go through the code in this section to make sure you fully understand it.
    
Custom_User_Validation.java
copy
import com.catalyst.Context; import com.catalyst.basic.BasicIO; import com.catalyst.basic.ZCFunction; import com.fasterxml.jackson.databind.ObjectMapper; import java.util.logging.Logger; import java.util.logging.Level; import com.zc.api.APIConstants.ZCSignupValidationStatus; import com.zc.common.ZCProject; import com.zc.component.auth.ZCSignupResponseUserDetails; import com.zc.component.auth.ZCSignupUserValidationRequest; import com.zc.component.auth.ZCSignupUserValidationResponse; import com.zc.component.users.ZCSignupUserService; public class Custom_User_Validation_Java implements ZCFunction { private static final Logger LOGGER = Logger.getLogger(Custom_User_Validation_Java.class.getName()); @Override public void runner(Context context, BasicIO basicIO) throws Exception { try { ZCProject.initProject(); ZCSignupUserValidationRequest requestDetails = ZCSignupUserService.getSignupValidationRequest(basicIO); if(requestDetails != null) { ZCSignupUserValidationResponse validationResponse = ZCSignupUserValidationResponse.getInstance(); if(requestDetails.getUserDetails().getEmailId().contains("@zylker.com")) { validationResponse.setStatus(ZCSignupValidationStatus.FAILURE); // The user has failed authentication } else { // The actions that occur in the event of a successful authentication can be customized validationResponse.setStatus(ZCSignupValidationStatus.SUCCESS); ZCSignupResponseUserDetails respUserDetails = ZCSignupResponseUserDetails.getInstance(); respUserDetails.setRoleIdentifier("App User"); // If you want to override the default role, you can specify the role id/name here. validationResponse.setUserDetails(respUserDetails); } basicIO.setStatus(200); basicIO.write(new ObjectMapper().writeValueAsString(validationResponse)); } } catch(Exception e) { LOGGER.log(Level.SEVERE,"Exception in Custom_User_Validation_Java",e); basicIO.setStatus(500); basicIO.write("An error has occured. Please try again after sometime."); } } }
View more
Note: You can also use this SDK to code your own custom logic instead of the one implemented above.

The function directory is now configured. We can now proceed to configuring the client directory.

Last Updated 2024-10-16 12:41:15 +0530 +0530