-
composer.json
Open in GitHub{ // "require": { "php": "^7.4 || ^8.0", // "laravel/sanctum": "^2.6" } // }
-
app/Models/User.php
Open in GitHubuse Laravel\Sanctum\HasApiTokens; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use HasApiTokens; // }
-
routes/api.php
Open in GitHubuse Crater\Http\Controllers\V1\Mobile\AuthController; use Illuminate\Support\Facades\Route; // Route::group(['prefix' => 'auth'], function () { Route::post('login', [AuthController::class, 'login']); Route::post('logout', [AuthController::class, 'logout'])->middleware('auth:sanctum'); // }); //
-
app/Http/Controllers/V1/Mobile/AuthController.php
Open in GitHubuse Crater\Http\Controllers\Controller; use Crater\Models\User; use Illuminate\Http\Request; use Illuminate\Support\Facades\Hash; use Illuminate\Validation\ValidationException; class AuthController extends Controller { class AuthController extends Controller { public function login(Request $request) { $request->validate([ 'username' => 'required|email', 'password' => 'required', 'device_name' => 'required', ]); $user = User::where('email', $request->username)->first(); if (! $user || ! Hash::check($request->password, $user->password)) { throw ValidationException::withMessages([ 'email' => ['The provided credentials are incorrect.'], ]); } return response()->json([ 'type' => 'Bearer', 'token' => $user->createToken($request->device_name)->plainTextToken, ]); } public function logout(Request $request) { $request->user()->currentAccessToken()->delete(); return response()->json([ 'success' => true, ]); } // }