B4J Tutorial [Web] Plug n Play MySQL Database Authentication via PHP REST API in 5 minutes or less

Hi Fam

Download

You will recall that we did a tutorial on MySQL back-ends using a plug n play CRUD REST API (Related Topics below)

This is based on this GitHub Project

In this approach we will use Database Authentication.

1. Create a database called banano with a table named users, it must have column username and column password. You can set the username to be the primary key.

2. In the api.php file, update the details to be using your MySQL database connection. What is new here is the dbAuth middleware and its settings. With sslRedirect we are saying the server uses HTTPS.

B4X:
$config = new Config([
        'driver' => 'mysql',
        'address' => '127.0.0.1',
        'port' => '3306',
        'username' => 'root',
        'password' => '',
        'database' => 'banano',
        'debug' => true,
        'tables' => 'all',
        'middlewares' => 'sslRedirect,sanitation,dbAuth',
        'dbAuth.usersTable' => 'users',
        'dbAuth.loginTable' => 'users',
        'dbAuth.usernameColumn' => 'username',
        'dbAuth.passwordColumn' => 'password',
        'dbAuth.registerUser' => '1',
        'dbAuth.loginAfterRegistration' => '0',
        'dbAuth.passwordLength' => '12',
    ]);

What we are saying here is that we will use a database for authorization,

Related Content


 
Last edited:

Mashiane

Expert
Licensed User
Longtime User
Registering a new user

To register a user we need to call the REGISTER route. The location of our api.php file is in the assets folder.

We pass the username & password parameters to this route and execute a POST.

1731863169466.png



Should you create the same user, an error is raised.

B4X:
{
    "code": 1020,
    "message": "User 'AneleMbanga3' already exists"
}

Checking your users table, the user is created with a hashed password.

1731863311106.png
 

Mashiane

Expert
Licensed User
Longtime User
Lets add some nice UI to this...

1731865596953.png


B4X:
dbConn.Initialize(Me, "dbConn", Main.DatabaseIP, "users")
    Dim b As Boolean = banano.Await(dbConn.USER_LOGIN(email.Value, password.value))
    If b Then
        pgIndex.UpdateUserName("Anele 'Mashy' Mbanga")
        pgIndex.UpdateUserAvatar("./assets/mashy.jpg")
        pgIndex.IsAuthenticated(True)
        'hide the modal
        mdlSignIn.Hide
    Else    
        banano.Await(app.ShowSwalErrorWait("Login", "The login credentials could not be verified!", "Ok"))
    End If
 

Mashiane

Expert
Licensed User
Longtime User
The sign up screen

1731870952709.png


B4X:
Dim dbConn As SDUIMySQLREST
    dbConn.Initialize(Me, "dbConn", Main.DatabaseIP, "users")
    dbConn.SchemaAddText(Array("username", "password", "email", "name"))
    dbConn.UseApiKey = False
    Dim b As Boolean = banano.Await(dbConn.USER_CREATE(email.Value, password.value))
    If b Then
        banano.Await(app.ShowSwalSuccessWait("Sign Up", $"The user profile for ${fullname.value} has been created!"$, "Ok"))
        dbConn.PrepareRecord
        dbConn.PrimaryKey = "username"
        dbConn.SetField("username", email.Value)
        dbConn.SetField("name", fullname.Value)
        banano.Await(dbConn.update)
    Else
        banano.Await(app.ShowSwalErrorWait("Sign Up Error", "The user profile could not be created!", "Ok"))
        Return    
    End If
 
Top