B4J Tutorial [ABMaterial] Enabling Users to get their lost passwords via email

Hi there...

I wanted users to be able to get their lost passwords via email. This does not reset their passwords but just sends them their passwords to their registered email addresses.

To enable reseting user passwords by the App administrator, you can see my previous article here. For the app to send the emails, I have used SMTP, you can follow this article here.

In my Sign In page, in the navigation bar, I have added a link that when selected, will check if the email address is entered, if not prompt a user for one. Once the user email address is entered, the app reads the email account settings that store the SMTP server settings to use, reads the user profile based on the provided email address and then sends the Sign In details for that user.

ForgotPasswordShow.png


Top Nav Bar Items are trapped using the Page_NavigationBarClicked event..

B4X:
Sub Page_NavigationbarClicked(Action As String, Value As String)
    page.SaveNavigationBarPosition
    If Action = "LogOff" Then
        ABMShared.LogOff(page)
        Return
    End If
    Select Case Action.ToLowerCase
    Case "signin"
        ExecuteSignIn
        Return
    Case "forgotpassword"
        ExecuteForgotPassword
        Return
    Case "goback"
        ExecuteGoBack
        Return
    End Select
    ABMShared.NavigateToPage(ws, ABMPageId, Value)
End Sub

This then runs this code..

B4X:
Private Sub ForgotPasswordProcess() As Boolean
    'define a map to hold the form contents
    Dim m As Map
    'read the form contents to a map
    m = GetContents
    Dim email As String
    email = m.get("email")
    If email = "null" Then email = ""
    If email.Length = 0 Then
        ABMShared.ShowMsgBox(page,"Email cannot be blank. Please enter a value.")
        Return False
    End If
    'the form contents are ok, send login credentials to user
    'define the search criteria, fields must equal
    Dim w As Map
    Dim nk As String
    w.Initialize
    nk = "email="
    w.put(nk, email)
    'Get connection from current pool if MySQL/MSSQL
    Dim jSQL As SQL = ABMShared.SQLGet
    Dim UserMap As Map = ABMShared.SQLSelectRecordWhereMap(jSQL,"drpw_users", w)
    If UserMap.IsInitialized = False Then
        ABMShared.ShowMsgBox(page,"The Email specified could not be found in our records, please specify the correct email.")
        Return False
    End If
    'The email has been found, send the details to the user
    Dim username As String
    username = UserMap.get("username")
    Dim password As String
    password = UserMap.get("password")
    Try
        ' start smtp to send the emails
        Dim sbody As String = "Good Day||Someone or you requested your Sign In credentials.||Your UserName is: " & username & "||Your Password is: " & password & "||Development Team|"
        sbody = ABMShared.Replace(sbody,"|",CRLF)
        SendEmail(jSQL,smtp,email, "DRPW: Sign In Credentials", sbody)
        'Close the connection to the database
        ABMShared.SQLClose(jSQL)
        Return True
    Catch
        myToastId = myToastId + 1
        page.ShowToast("toast" & myToastId, "toastred", "Email could not be sent, please try again.", 3000)
        Return False
    End Try
End Sub

The code also enables users to Sign In in your app, check and assign their permissions as discussed in this article.

That's all for now.
 
Top