B4J Tutorial [Web] SithasoDaisy - Pocketbase User Authentication

Hi Fam

Download

One is able to perform User Authentication using Email / Password combination in PocketBase. The user must be defined in the users collection in pocketbase.



The project in the download is a B4J / BANano based project that shows one how to perform user authentication using an email and a password.

1. The administrator user is admin@pocketbase.com and password is admin@pocketbase.com123! This is the same as a user in the users collection
2. The user details are stored in the beautify.json file of the project. This is used to create other users in the app.
3. A user must exist on the users table to be able to log in usign their credentials.
4. To run the demo, double click the bat file, this login screen will appear, login using admin@pocketbase.com and admin@pocketbase.com123!

This template can be used to create Pocketbase based WebApps with user authentication.

For the forgot password screen to work, email settings should be set on pocketbase. You can use your SMTP details for that.

This is the example of the sign in code

B4X:
Private Sub btnSignIn_Click (e As BANanoEvent)
    e.PreventDefault
    'reset the validations
    mdlSignIn.ResetValidation
    'validate each of the elements
    mdlSignIn.Validate(email.IsBlank)
    mdlSignIn.Validate(password.IsBlank)
    'check the form status
    If mdlSignIn.IsValid = False Then 
        app.ShowSwalError("Please provide all required information!")
        Return
    End If
    '    
    app.pagepause
    dblUsers.Initialize(Me, "dbusers", Main.ServerIP, "users")
    banano.await(dblUsers.USER_AUTH_WITH_PASSWORD(email.value, password.value))
    Dim up As profileType = dblUsers.UserProfile
    If up.Size = 0 Then
        app.pageresume
        app.ShowSwalError("These login details could not be authenticated, please contact Admin!")
        Return
    End If
    'get the data on form
    Dim data As Map = mdlSignIn.GetData
    'do we remember
    If rememberme.Checked Then
        'save settings using app name
        SDUIShared.SetLocalStorage(Main.AppName, data)
    Else
        'remove settings
        SDUIShared.DeleteLocalStorage(Main.AppName)
    End If
    'show nav & drawer
    pgIndex.UpdateUserName(up.name)
    pgIndex.UpdateUserAvatar(up.avatar)
    pgIndex.IsAuthenticated(True)
    'hide the modal
    mdlSignIn.Hide
    app.PageResume
End Sub


You can use this guide.


Related Content

 

Mashiane

Expert
Licensed User
Longtime User
Resetting Passwords for a User who has forgotten their password
c
When the email configuration is set up, when a user forgets his/her passwords these can be sent.

The admin credentials are used to send the forgot password credentials for any user.

The beautify.json file stores the email and password of the administrator. This is read and used for authentication first. After that a user pasword request is made using the email address entered by the user.

This will activate the process and send an email to the user using the built it user api.

For this to work, the email settings should be set in PocketBase.



B4X:
Private Sub btnReset_Click (e As BANanoEvent)
    e.PreventDefault
    'reset the validations
    mdlSignIn.ResetValidation
    'validate each of the elements
    mdlSignIn.Validate(email.IsBlank)
    'check the form status
    If mdlSignIn.IsValid = False Then 
        app.ShowSwalError("Please enter your email address!")
        Return
    End If
    
    app.pagepause
    Dim bmabeauty As Map = banano.Await(banano.GetFileAsJSON($"./assets/bmabeauty.json?${DateTime.now}"$, Null))
    Dim ex As String = bmabeauty.Get("e")
    Dim px As String = bmabeauty.Get("p")
    dblUsers.Initialize(Me, "dbusers", Main.ServerIP, "users")
    banano.await(dblUsers.ADMIN_AUTH_WITH_PASSWORD(ex, px))
    Dim admin As profileType = dblUsers.UserProfile
    If admin.Size = 0 Then
        app.pageresume
        app.ShowSwalError("The Administrator could not sign in, please try again later!")
        Return
    End If
    'request the password to be reset
    Dim vEmail As Boolean = banano.await(dblUsers.USER_REQUEST_PASSWORD_RESET(email.value))
    If vEmail = False Then
        app.pageresume
        app.ShowSwalError("Password reset could not be requested!")
        Return
    End If
    app.pageresume
    app.ShowSwalSuccess("Password reset request sent!")
End Sub
 

Mashiane

Expert
Licensed User
Longtime User
Sign Up Users



The crucial thing when signing up users is the email, password and confirm password. As this is an admin function, the administrator needs to be authenticated first and a user can be created.

A newly registered user needs to verify their email and also be authorized. When signing up users, you can send a request to verify the account and then authorize them

B4X:
Private Sub btnSignUp_Click (e As BANanoEvent)
    e.PreventDefault
    'reset the validations
    mdlSignIn.ResetValidation
    'validate each of the elements
    mdlSignIn.Validate(fullname.IsBlank)
    mdlSignIn.Validate(email.IsBlank)
    mdlSignIn.Validate(password.IsBlank)
    mdlSignIn.Validate(confirmpassword.IsBlank)
    'check the form status
    If mdlSignIn.IsValid = False Then Return
    'get the data on form
    '
    app.pagepause
    Dim bmabeauty As Map = banano.Await(banano.GetFileAsJSON($"./assets/bmabeauty.json?${DateTime.now}"$, Null))
    Dim ex As String = bmabeauty.Get("e")
    Dim px As String = bmabeauty.Get("p")
    dblUsers.Initialize(Me, "dbusers", Main.ServerIP, "users")
    banano.await(dblUsers.ADMIN_AUTH_WITH_PASSWORD(ex, px))
    Dim admin As profileType = dblUsers.UserProfile
    If admin.Size = 0 Then
        app.pageresume
        app.ShowSwalError("The Administrator could not sign in, please try again later!")
        Return
    End If
    
    'generate a password
    Dim user As Map = CreateMap()
    user.put("email", email.value)
    user.put("password", password.value)
    user.put("passwordConfirm", confirmpassword.value)
    user.put("name", fullname.value)
    banano.Await(dblUsers.CREATE_USER(user))
    Dim up As profileType = dblUsers.UserProfile
    If up.size = 0 Then
        app.pageresume
        app.ShowSwalError($"${fullname.value}, user could not be created, please try again later!"$)
        Return
    Else
        banano.Await(dblUsers.USER_REQUEST_VERIFICATION(email.value))
        banano.Await(dblUsers.USER_AUTH_WITH_PASSWORD(email.Value, password.value))
        app.pageresume
        app.ShowSwalSuccess($"${fullname.value} user account was created successfully!"$)
    End If
End Sub
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…