https://github.com/shubham16g/php-fast-auth
Easy and Fast PHP authentication (MySQL Database)
https://github.com/shubham16g/php-fast-auth
auth authentication mysql-database otp-verification php
Last synced: 4 months ago
JSON representation
Easy and Fast PHP authentication (MySQL Database)
- Host: GitHub
- URL: https://github.com/shubham16g/php-fast-auth
- Owner: shubham16g
- License: apache-2.0
- Created: 2021-03-27T15:15:10.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2022-12-08T12:41:51.000Z (over 3 years ago)
- Last Synced: 2025-02-24T03:46:29.138Z (over 1 year ago)
- Topics: auth, authentication, mysql-database, otp-verification, php
- Language: PHP
- Homepage:
- Size: 148 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Fast-Auth
Easy and Fast PHP authentication (MySQL Database)
## Docs
### Initialize
PHPFastAuth requires mysqli object to connect with your MYSQL database.
You can use $mysqli_db = new mysqli($host, $username, $password, $dbname);
try {
$auth = new PHPFastAuth($mysqli_db);
} catch (Exception $e) {
echo $e->getMessage();
}
or
try {
$options = new PHPFastAuth\Options();
$options->setOTPLength(4);
$auth = new PHPFastAuth($mysqli_db, $options);
} catch (Exception $e) {
echo $e->getMessage();
}
### Install
This code must be called once to create required tables in your database.
try {
$auth = new PHPFastAuth($mysqli_db);
$auth->install();
} catch (Exception $e) {
echo $e->getMessage();
}
### OTP Sign up with Mobile
try {
$auth = new PHPFastAuth($db);
$signUp = new PHPFastAuth\SignUpWithMobile($mobile);
$signUp->setName($name);
$signUp->setPassword($password);
$key = $auth->signUpRequest($signUp);
$otpData = $auth->decodeOTP($key);
$mobile = $otpData->getMobile();
$otp = $otpData->getOTP();
sendOTPviaSMS($mobile, $otp);
} catch (Exception $e) {
echo $e->getMessage();
}
### OTP Sign up with Email
try {
$auth = new PHPFastAuth($db);
$signUp = new PHPFastAuth\SignUpWithEmail($email);
$signUp->setName($name);
$signUp->setPassword($password);
$key = $auth->signUpRequest($signUp);
$otpData = $auth->decodeOTP($key);
$email = $otpData->getEmail();
$otp = $otpData->getOTP();
sendOTPviaEmail($email, $otp);
} catch (Exception $e) {
echo $e->getMessage();
}
### Verify OTP
$result = $auth->verifyOTP($key, $otp);
switch ($result['case']) {
case PHPFastAuth::CASE_UPDATE_PASSWORD:
$passwordUpdateKey = $result['passwordUpdateKey'];
header("Location: reset_password.php?passwordUpdateKey=" . urlencode($passwordUpdateKey));
die();
break;
case PHPFastAuth::CASE_NEW_USER:
$signIn = new PHPFastAuth\SignInWithUID($result['uid']);
$signInResult = $auth->signInWithoutPassword($signIn);
$_SESSION['token'] = $signInResult['token'];
$_SESSION['isAnonymous'] = $signInResult['isAnonymous'];
// $content = json_encode($signInResult);
$title = "Account verification successful";
break;
case PHPFastAuth::CASE_UPDATE_EMAIL:
$title = "Email update successful";
break;
case PHPFastAuth::CASE_UPDATE_MOBILE:
$title = "Mobile number update successful";
break;
default:
throw new Exception("Error in verify_otp.php: no such case", 1);
break;
}