arjunyuvanesh / common-auth
v1.0.1
2026-07-14 06:56 UTC
Requires
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
A highly professional, dynamic, and enterprise-grade Laravel Authentication package. Built specifically to support Email, Mobile, or Username logins seamlessly, integrated deeply with spatie/laravel-permission, Dual Email Verification, and Dynamic Rate Limiting.
Features
- Headless Design: Provides 100% of the backend logic. You build the UI in React, Vue, Blade, or Mobile SDKs.
- Dynamic Login: Users log in using Email, Mobile, or Username via a single input field.
- Dual Architecture: Native support for both Web Browsers (Session/CSRF) and Mobile Apps (Sanctum Tokens).
- Dual Email Verification: Supports both Magic Links and stateless 6-digit OTPs (stored efficiently in Cache).
- Account Management: Endpoints for updating profiles, changing passwords securely, and account deletion.
- Enterprise Security: Mathematically strict password enforcement and dynamic Brute-Force Rate Limiting.
Installation & Setup
- Install the package:
composer require arjunyuvanesh/common-auth
- Publish config and migrations:
php artisan common-auth:install
- Run migrations:
php artisan migrate
- Update your User Model (
app/Models/User.php): Add theHasCommonAuthtrait to inject the package engine into your app.
namespace App\Models; use Arjunyuvanesh\CommonAuth\Traits\HasCommonAuth; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use HasCommonAuth; // Add this line! // Add any custom fields you want users to submit during registration protected $fillable = [ 'name', 'email', 'password', 'username', 'mobile', 'address' ]; }
How to Connect Your Frontend
This package exposes two identical sets of endpoints.
- Use the
/common-auth/*endpoints for Web Browsers (Blade, React/Vue SPAs using Sanctum stateful domains). - Use the
/api/common-auth/*endpoints for Mobile Apps (iOS/Android/Flutter using Bearer Tokens).
Scenario 1: Connecting a Web Browser (React / Vue / Blade)
Web browsers rely on Cookies and CSRF tokens.
- Submit the Login Request:
Your frontend should make an AJAX request to
/common-auth/login.
// Example using Axios axios.post('/common-auth/login', { login: 'arjun15247@gmail.com', // Can be email, mobile, or username password: 'SuperSecretPassword123!', remember: true }).then(response => { console.log(response.data.message); // "Successfully logged in." console.log(response.data.user); // The user's profile });
- Accessing Authenticated Routes:
Because Web Browsers use cookies, once you log in, your browser automatically saves the session. You can immediately call
/common-auth/mewithout passing any tokens!
Scenario 2: Connecting a Mobile App (React Native / Flutter / Swift)
Mobile apps cannot use cookies or CSRF tokens. They must use the /api prefix.
- Submit the Login Request:
Make a JSON request to
/api/common-auth/login.
fetch('https://yourwebsite.com/api/common-auth/login', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Accept': 'application/json' }, body: JSON.stringify({ login: 'arjun15247@gmail.com', password: 'SuperSecretPassword123!' }) }) .then(res => res.json()) .then(data => { // SAVE THIS TOKEN IN YOUR MOBILE APP (e.g. AsyncStorage or SecureStorage) const token = data.token; });
- Accessing Authenticated API Routes:
To get the user's profile or change their password, you must pass the token in the
Authorizationheader.
fetch('https://yourwebsite.com/api/common-auth/me', { method: 'GET', headers: { 'Accept': 'application/json', 'Authorization': 'Bearer ' + token // Pass the token here! } });
Complete API Reference
Guest Endpoints
Available under both /common-auth/ and /api/common-auth/
POST /login- Acceptslogin(email/mobile/username) andpassword. Returns a Token if called via/api.POST /register- Accepts your dynamically configured registration fields (seeconfig/common-auth.php).POST /forgot-password- Sends a password reset link (requiresemail).POST /reset-password- Resets the password (requirestoken,email,password,password_confirmation).
Authenticated Endpoints
Web uses Cookies. APIs require Authorization: Bearer <token>
GET /me- Returns the current user's profile.POST /logout- Logs the user out and securely revokes API tokens.PUT /profile- Updates the user profile. Acceptsname,email,mobile,username.PUT /password- Securely changes the password. Acceptscurrent_password,password,password_confirmation.DELETE /account- Permanently deletes the user and revokes all tokens.
Verification Endpoints
GET /email/verify/{id}/{hash}- Magic Link verification endpoint.POST /email/verify-otp- Submits the 6-digit OTP. Acceptsotp.POST /email/verification-notification- Resends the OTP/Link to the user's email.