Android Example [B4X] PocketBase - Authentification


Please read the official documentation about auth in pocketbase:

PocketBase authentication allows you to register users, log them in, manage their sessions, and perform various actions such as password resets and email changes. Below is an overview of the available functions with examples.

User Registration (SignUp)
Allows new users to create an account.
B4X:
    Wait For (xPocketbase.Auth.SignUp("test@example.com","Test123!","Test123!",Null)) Complete (NewUser As PocketbaseUser)
    If NewUser.Error.Success Then
        Log("successfully registered with " & NewUser.Email)
    Else
        Log("Error: " & NewUser.Error.ErrorMessage)
    End If

Parameters:

  • Email: The user's email address.
  • Password: The user's password.
  • PasswordConfirm: Password confirmation.
  • Options: A Map containing optional fields for additional user details.
B4X:
Dim options As Map
options.Initialize
options.Put("username", "testuser")

Wait For (xPocketbase.Auth.SignUp("test@example.com", "Test123!", "Test123!", options)) Complete (NewUser As PocketbaseUser)

Request Account Verification (RequestVerification)
Sends a verification request to the user’s email.
B4X:
    Wait For (xPocketbase.Auth.RequestVerification("test@example.com")) Complete (Success As PocketbaseError)
    If Success.Success Then
        Log("verification code send to email")
    Else
        Log("Error: " & Success.ErrorMessage)
    End If

Parameters:

  • Email: The user's email address.

Confirm Account Verification (ConfirmVerification)
Confirms the user’s account using the verification token received via email.
B4X:
    Wait For (xPocketbase.Auth.ConfirmVerification("xxx")) Complete (Success As PocketbaseError)
    If Success.Success Then
        Log("verification sucessfull")
    Else
        Log("Error: " & Success.ErrorMessage)
    End If
Parameters:
  • VerificationToken: The token received via email.

User Login with Email & Password (AuthWithPassword)
Authenticates a user using their email and password.
B4X:
    Wait For (xPocketbase.Auth.AuthWithPassword("test@example.com","Test123!")) Complete (User As PocketbaseUser)
    If User.Error.Success Then
        Log("successfully logged in with " & User.Email)
    Else
        Log("Error: " & User.Error.ErrorMessage)
    End If

Parameters:

  • Email: The user's email address.
  • Password: The user's password.

Logout
Removes user tokens and info from the device.
B4X:
    Wait For (xPocketbase.Auth.Logout) Complete (Result As PocketbaseError)
    If Result.Success Then
        Log("successfully logged out")
    Else
        Log("Error: " & Result.ErrorMessage)
    End If

Get User Information (GetUser)
Retrieves the current authenticated user’s information.
B4X:
    Wait For (xPocketbase.Auth.GetUser) Complete (User As PocketbaseUser)
    Log(User.Email)

Update User Information (UpdateUser)
Updates the user's profile details.
Wait For (xPocketbase.Auth.UpdateUser(CreateMap("name":"Test Name"))) Complete (Success As PocketbaseError)
Parameters:
  • Options: A Map containing the fields to be updated.

Delete User (DeleteUser)
Deletes the user’s account.
B4X:
    Wait For (xPocketbase.Auth.DeleteUser) Complete (Result As PocketbaseError)
    If Result.Success Then
        Log("User Account deleted")
    Else
        Log("Error: " & Result.ErrorMessage)
    End If

Request Password Reset (RequestPasswordReset)
Sends an email for resetting the user’s password.
B4X:
    Wait for (xPocketbase.Auth.RequestPasswordReset("test@example.com")) Complete (Response As PocketbaseError)
    If Response.Success Then
        Log("Recovery email sent successfully")
    Else
        Log("Error: " & Response.ErrorMessage)
    End If
Parameters:
  • Email: The user's email address.

Confirm Password Reset (ConfirmPasswordReset)
Confirms the password reset using the token received via email.
B4X:
    Wait For (xPocketbase.Auth.ConfirmPasswordReset("xxx","Test123!","Test123!")) Complete (Response As PocketbaseError)
    If Response.Success Then
        Log("Password change successfully")
    Else
        Log("Error: " & Response.ErrorMessage)
    End If
Parameters:
  • Token: The verification token from the email.
  • NewPassword: The new password.
  • NewPasswordConfirm: Confirmation of the new password.

Request Email Change (RequestEmailChange)
Sends a request to change the user’s email.
B4X:
    Wait For (xPocketbase.Auth.RequestEmailChange("test@example.com")) Complete (Success As PocketbaseError)
    If Success.Success Then
        Log("E-Mail change request sent")
    Else
        Log("Error: " & Success.ErrorMessage)
    End If
Parameters:
  • NewEmail: The new email address.

Confirm Email Change (ConfirmEmailChange)
Confirms the email change with a verification token.
B4X:
    Wait For (xPocketbase.Auth.ConfirmEmailChange("xxx","Test123!")) Complete (Response As PocketbaseError)
    If Response.Success Then
        Log("E-Mail change successfully")
    Else
        Log("Error: " & Response.ErrorMessage)
    End If
Parameters:
  • Token: The verification token from the email.
  • Password: The user's password.

Refresh Token (RefreshToken) - Optional
Refreshes the user's authentication token.

The B4X-Pocketbase library does this automatically internally, but if needed, you can update the access token this way
B4X:
Wait For (xPocketbase.Auth.RefreshToken) Complete (Success As Boolean)

If Success Then
    Log("Token refreshed successfully")
Else
    Log("Error refreshing token")
End If
 
Top