vielhuber / simpleauth
Simple php authentication library.
Installs: 181
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 2
Forks: 0
Open Issues: 0
pkg:composer/vielhuber/simpleauth
Requires
- php: >=7.4
- firebase/php-jwt: ^6.0
- guzzlehttp/guzzle: ^6.3
- vielhuber/comparehelper: >=1.0
- vielhuber/dbhelper: >=1.8
- vlucas/phpdotenv: *
Requires (Dev)
- phpunit/phpunit: ^9
- dev-master
- 1.6.0
- 1.5.9
- 1.5.8
- 1.5.7
- 1.5.6
- 1.5.5
- 1.5.4
- 1.5.3
- 1.5.2
- 1.5.1
- 1.5.0
- 1.4.9
- 1.4.8
- 1.4.7
- 1.4.6
- 1.4.5
- 1.4.4
- 1.4.3
- 1.4.2
- 1.4.1
- 1.4.0
- 1.3.9
- 1.3.8
- 1.3.7
- 1.3.6
- 1.3.5
- 1.3.4
- 1.3.3
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.9
- 1.2.8
- 1.2.7
- 1.2.6
- 1.2.5
- 1.2.4
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.7
- 1.1.6
- 1.1.5
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.9
- 1.0.8
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
This package is auto-updated.
Last update: 2025-12-09 10:18:48 UTC
README
🔒 simpleauth 🔒
simpleauth is a simple php based authentication library.
it leverages:
- json web tokens
- bcrypted passwords
- full api
installation
install once with composer:
composer require vielhuber/simpleauth
now simply create the following files in a new folder called auth inside your public directory:
/auth/index.php
<?php require_once __DIR__ . '/../vendor/autoload.php'; use vielhuber\simpleauth\simpleauth; $auth = new simpleauth(__DIR__ . '/../.env'); $auth->init();
/auth/.htaccess
RewriteEngine on
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /auth/index.php [L,QSA]
/.env
create a jwt secret (openssl rand -base64 64 | tr -d '\n' | xclip -selection clipboard)
and populate an .env file:
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=simpleauth DB_USERNAME=root DB_PASSWORD=root JWT_TABLE=users JWT_LOGIN=email JWT_TTL=30 JWT_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
if you want to migrate and seed data, simply run
php auth/index.php migrate php auth/index.php create "david@vielhuber.de" "secret"
and you should be done (a test user david@vielhuber.de with the password secret is created).
you can now fully authenticate with the routes below.
if you want to authenticate via username instead of email, simply change JWT_LOGIN to username.
routes
the following routes are provided automatically:
| route | method | arguments | header | response |
|---|---|---|---|---|
| /auth/login | POST | email password | -- | ([ 'success' => true, 'message' => 'auth successful', 'public_message' => '...', 'data' => [ 'access_token' => '...', 'expires_in' => 3600, 'user_id' => 42 ] ], 200) |
| /auth/refresh | POST | -- | Authorization: Bearer token | ([ 'success' => true, 'message' => 'auth successful', 'public_message' => '...', 'data' => [ 'access_token' => '...', 'expires_in' => 3600, 'user_id' => 42 ] ], 200) |
| /auth/logout | POST | -- | Authorization: Bearer token | ([ 'success' => true, 'message' => 'logout successful', 'public_message' => '...' ], 200) |
| /auth/check | POST | access_token | -- | ([ 'success' => true, 'message' => 'valid token', 'public_message' => '...', 'data' => [ 'expires_in' => 3600, 'user_id' => 42, 'client_id' => 7000000 ] ], 200) |
tests
php -S localhost:8000 -t auth ./vendor/bin/phpunit
further usage
you can use the following functions inside your own application (they do not need any database lookups):
require __DIR__ . '/vendor/autoload.php'; use vielhuber\simpleauth\simpleauth; $auth = new simpleauth(__DIR__ . '/.env'); $auth->isLoggedIn(); $auth->getCurrentUserId();
frontend
if you need a neat frontend library that works together with simpleauth seemlessly, try out jwtbutler.