'Handler class
' ejemplo de acceso al login desde b4j
Sub Class_Globals
End Sub
Public Sub Initialize
End Sub
Sub Handle(req As ServletRequest, resp As ServletResponse)
#if debug
Main.templates.clearTemplateCache
#End If
resp.CharacterEncoding ="utf-8"
If req.Method = "POST" Then
Dim m As Map = req.ParameterMap ' aqui revices en umn map todas la equiquetas
Dim phone As String = ""
Dim password As String = ""
For Each k As String In m.Keys
Dim v() As String = m.Get(k)
Select k
Case "phone"
phone = v(0)
Case "password"
password = v(0)
End Select
Next
If phone = "" Or password = "" Then
Errores1
req.GetSession.SetAttribute("error", "user and password are required")
resp.SendRedirect("/login")
Return
End If
Dim success As Boolean = Checkuser(req,phone,password)
If success Then
req.GetSession.SetAttribute("registered", True)
req.GetSession.SetAttribute("error", "")
resp.SendRedirect("/portal/main")
Else
req.GetSession.SetAttribute("error", "Invalid Username or Password")
resp.SendRedirect("/login")
End If
Else
Dim root As Map
root.Initialize
root.Put("loginError",req.GetSession.GetAttribute2("login error",""))
req.GetSession.RemoveAttribute("login error")
Dim t As Template = Main.templates.getTemplate("login.ftl")
resp.Write(t.stdOut(root))
End If
End Sub
Sub Checkuser(req As ServletRequest,username As String, password As String) As Boolean
Dim result As Boolean
Dim passwordcheck As String
Dim sql As SQL = utils.pool.GetConnection
Try
Dim query As String = $"
SELECT userID
, CONCAT(first_name,' ',last_name) as userName
, password
FROM user_db
WHERE phone = ?
"$
Dim params(1) As String
params(0) = username
Dim dt As dataTable
dt.Initialize(sql,query,params)
If dt.RowsCount > 0 Then
passwordcheck=utils.DecryptText(dt.getCellValue(0,"password"),Main.passwordPassword)
Log(passwordcheck)
result = passwordcheck = password
If result Then
req.GetSession.SetAttribute("userID",dt.getCellValue(0,"userID"))
req.GetSession.SetAttribute("name", dt.getCellValue(0,"userName"))
End If
End If
Catch
Log(LastException)
End Try
sql.Close
Return result
End Sub
Sub Errores1
End Sub