Sub Handle(req As ServletRequest, resp As ServletResponse)
If req.Method <> "POST" Then
If req.Method <> "POST" Then
Utility.MapToResponse(Utility.GenerateErrorMap("Only Post Operation is allowed","You cannot perform any other operation apart from post operation"),404,resp)
End If
End If
Dim ReqMap As Map = Utility.RequestToMap(req.InputStream)
Dim EmailAddress As String
Dim Password As String
Dim Connection As SQL = Main.ConnectionPool.GetConnection()
Dim strQuery As String
Dim Result As ResultSet
Dim Cryptify As BCrypt
Cryptify.Initialize("")
EmailAddress = ReqMap.Get("email_address")
Password = ReqMap.Get("password")
'Check if any of the user or the password is empty
If EmailAddress.Trim = "" Then
Utility.MapToResponse(Utility.GenerateErrorMap("Email Address is required","You have to supply an email address. send a json as request body email_address"),400,resp)
Else if Utility.ValidateEmail(EmailAddress.Trim) = False Then
Utility.MapToResponse(Utility.GenerateErrorMap("Email Address format is invalid","You have to submit a valid email address with @ and ."),400,resp)
Else if Password.Trim = "" Then
Utility.MapToResponse(Utility.GenerateErrorMap("Password is required","You have to supply Password. send a json request body of password"),400,resp)
Else if Password.Trim.Length < 8 Then
Utility.MapToResponse(Utility.GenerateErrorMap("Incorrect Login Credentials","The Login Credentials you have submit in incorrect"),400,resp)
End If
'After here start a database connection
strQuery = "SELECT * FROM authentications WHERE email_address = ?"
Result = Connection.ExecQuery2(strQuery,Array(EmailAddress))
If Result.NextRow = False Then
Utility.MapToResponse(Utility.GenerateErrorMap("Incorrect Login Credentials","The Login Credentials you have submit in incorrect"),400,resp)
End If
'Get the Hashed Password and the compare it with the inserted password
If Cryptify.checkpw(Password,Result.GetString("password")) = False Then
Utility.MapToResponse(Utility.GenerateErrorMap("Incorrect Login Credentials","The Login Credentials you have submit in incorrect"),400,resp)
End If
Utility.MapToResponse(Utility.GenerateSuccessMap("Login Was Successful","You have logged in successfully",CreateMap()),200,resp)
Connection.Close
End Sub