2017 © Pedro Peláez
 

library liteauth

image

tlucas/liteauth

  • Friday, January 12, 2018
  • by tlucas
  • Repository
  • 1 Watchers
  • 1 Stars
  • 69 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

phpliteauth

Small authentication library for PHP, using SQLite as a database backend, (*1)

Installation

composer require tlucas/liteauth

In the project file (e.g. project.php) you wish to use it in make sure you have, (*2)

require_once('vendor/autoload.php');

Then initialise the authentication object with:, (*3)

$auth = new liteAuth\liteAuth('path/to/my/auth_database.db');

(Of course, you can call the object anything, but for the rest of this readme, we will assume you called it $auth), (*4)

Basic usage

User creation

The very first thing you will have to do, before logging in to liteAuth, is, of course, create a user., (*5)

This is done using:, (*6)

$auth->newUser($user, $pass, $email, $fname, $sname, $admin);

Only the $user and $pass parameters are required, the rest are optional., (*7)

So, to give an example:, (*8)

$auth->newUser('John', 'superstrongpassword');

We now have a user called John in the database, with the password superstrongpassword., (*9)

(If newUser() is successful it returns the new user's id , otherwise, it returns False), (*10)

(The other fields should be self-explanatory, with the posssible expception of the $admin. This is simply a True/False field, it has no special meaning within liteAuth, so you are free to use it however you will!), (*11)

From a form

There is also included a helper method registerFromPost(), to allow for easy registration form creation., (*12)

Place it at the target of a form, and it will look for the relevant post variables, to register a new user., (*13)

For example, you might have a file register.php containing, (*14)

$auth->registerFromPost();

And another file signup.html:, (*15)

<form action="register.php" method="post">
<input type="text" name="user" placeholder="Username"><br>
<input type="text" name="fname" placeholder="First name"><br>
<input type="text" name="sname" placeholder="Surname"><br>
<input type="email" name="email" placeholder="Email Address"><br>
<input type="password" name="pass" placeholder="Password"><br>
<input type="password" name="pass2" placeholder="Password"><br>
<input type="checkbox" name="admin"> Admin?<br>
<input type="submit" value="Register">
</form>

The register.php will take the data from signup.html and create a new user corresponding to the input., (*16)

BE CAREFUL: Anyone with access to the script calling registerFromPost() will be able to create a new user. Make sure this is only accessible by people who should have this authority!, (*17)

One example would be to require an admin user to be logged in:, (*18)

register.php:, (*19)

if($auth->user->admin)
    $auth->registerFromPost();

Logging in

Logging in is very similar to creating a user, you have the login() method you can call:, (*20)

$auth->login($user, $pass);

After which, if the password correctly matches the user, it will return True and populate $auth->user, (*21)

The following properties are available on the user object, once logged in:, (*22)

$auth->user->user       // User's username
$auth->user->first_name     // User's first name, if set
$auth->user->surname        // User's surname, if set
$auth->user->email      // User's email address, if set
$auth->user->admin      // If the user is set as an admin

There is also the special method, (*23)

$auth->name()

Which returns either the user's human name (ie. 'Firstname surname' or 'Firstname'), or falls back to username., (*24)

From a form

Just like with registration, there is a helper method for logging in from a form:, (*25)

$auth->loginFromPost();

If you put that in, for example, login.php, the form at loginform.html:, (*26)

<form action="login.php" method="post">
<input type="text" name="user" placeholder="Username"><br>
<input type="password" name="pass" placeholder="Password"><br>
<input type="submit" value="Login">
</form>

Will pass the appropriate values to sign in., (*27)

Modifying a user

To modify an existing user, you can edit any of the accessible properties, listed above, for example, (*28)

$auth->user->first_name = 'Stephen';

And then call, (*29)

$auth->user->save();

And it will update the current user's first name to Stephen in the database., (*30)

Other methods

$auth->countUsers();

Returns a count of users that exist in the database, (*31)

$auth->existUsers();

Returns False if no users exist (This is useful to allow for 'first run' setup procedures), (*32)

The Versions

12/01 2018

dev-master

9999999-dev

  Sources   Download

MIT

The Requires

 

The Development Requires

  • php ^7.0

by Thomas Lucas

12/01 2018

v0.1.2

0.1.2.0

  Sources   Download

MIT

The Requires

 

The Development Requires

  • php ^7.0

by Thomas Lucas

03/05 2017

v0.1.1

0.1.1.0

  Sources   Download

MIT

The Requires

 

The Development Requires

  • php ^7.0

by Thomas Lucas

03/05 2017

v0.1.0

0.1.0.0

  Sources   Download

MIT

The Requires

 

The Development Requires

  • php ^7.0

by Thomas Lucas