added jwt authentication token

This commit is contained in:
Abhishek Mali
2025-11-05 15:44:04 +05:30
parent dbe47a23c7
commit ccbef09484
9 changed files with 748 additions and 25 deletions

View File

@@ -0,0 +1,69 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use PHPOpenSourceSaver\JWTAuth\Facades\JWTAuth;
use Illuminate\Support\Facades\Auth;
class AuthController extends Controller
{
// ✅ Register
public function register(Request $request)
{
$request->validate([
'name' => 'required|string|max:255',
'email' => 'required|string|email|unique:users',
'password' => 'required|string|min:6',
]);
$user = User::create([
'name' => $request->name,
'email' => $request->email,
'password' => Hash::make($request->password),
]);
$token = JWTAuth::fromUser($user);
return response()->json([
'status' => true,
'message' => 'User registered successfully',
'user' => $user,
'token' => $token
]);
}
// ✅ Login
public function login(Request $request)
{
$credentials = $request->only('email', 'password');
if (!$token = Auth::guard('api')->attempt($credentials)) {
return response()->json(['error' => 'Invalid credentials'], 401);
}
return response()->json([
'status' => true,
'message' => 'Login successful',
'token' => $token,
'user' => Auth::guard('api')->user()
]);
}
// ✅ Logout
public function logout()
{
Auth::guard('api')->logout();
return response()->json(['message' => 'Successfully logged out']);
}
// ✅ Refresh token
public function refresh()
{
return response()->json([
'token' => Auth::guard('api')->refresh()
]);
}
}

View File

@@ -2,42 +2,26 @@
namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use PHPOpenSourceSaver\JWTAuth\Contracts\JWTSubject;
class User extends Authenticatable
class User extends Authenticatable implements JWTSubject
{
/** @use HasFactory<\Database\Factories\UserFactory> */
use HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var list<string>
*/
protected $fillable = [
'name',
'email',
'password',
];
/**
* The attributes that should be hidden for serialization.
*
* @var list<string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
@@ -45,4 +29,15 @@ class User extends Authenticatable
'password' => 'hashed',
];
}
// 🔑 Required for JWT
public function getJWTIdentifier()
{
return $this->getKey();
}
public function getJWTCustomClaims()
{
return [];
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace App\Providers;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Route;
class RouteServiceProvider extends ServiceProvider
{
/**
* Define your route model bindings, pattern filters, and other route configuration.
*/
public function boot(): void
{
$this->routes(function () {
Route::prefix('api')
->middleware('api')
->group(base_path('routes/api.php'));
Route::middleware('web')
->group(base_path('routes/web.php'));
});
}
}