Skip to content

User_Auth

StefansArya edited this page Apr 21, 2019 · 2 revisions

You may need to import this feature on top of your code.

use \Scarlets\Library\User\Auth;

The authentication configuration are stored on /config/auth.php.
When you're using this library, it will automatically add users table on the database if not exist.

Column Description
user_id User ID to maintain data relation
username User's defined ID
password User's secret key
email User's email address
failed_login Number of failed login try
login_time Login try timestamp
last_created Timestamp when this was created

Registering new user

For user privacy reason you must put the login data into an array. This to avoid data leaking when debugging. You can also utilize API::obtain for doing this. All the obtained data will be inserted to matched table column.

Auth::register($data);

# Example
API::obtain($data, ['username', 'name', 'password', 'email']);
$msg = Auth::register($data);

// new UserID was returned
if(is_int($msg) === true)
    # $msg == UserID

// Returning error message
else die($msg);

Handle user login

After the user was registered on the database, you can let them logged in after obtaining their username and password. The framework will try to recognize an email, so email should be aliased to username.

Auth::login($data, $where = false, $beforeVerify = false, $onSuccess = false);

# Example
API::alias($data, ['username', 'password', 'email'=>'username']);
Auth::login($data, ['blocked'=>0], function($row){
    // Called before verifying password

    $row == ['user_id'=>0, 'password'=>'', 'username'=>'']

    // return true; <- block login
}, function($row){
    // Called after the login was success

    // return true; <- don't save current session to database/cookie
});

Get user login status

After the user was logged in via AccessToken or current User Session, you can obtain the login data with this feature.

$data = Auth::getLoginData($returnBool = false);

# Example
$data = Auth::getLoginData();
// Output: ['userID'=>1, 'username'=>'hello']
Clone this wiki locally