-
composer.json
Open in GitHub{ "php": "^7.4|^8.0", // "laravel/sanctum": "^2.8" }, }
-
app/Http/Kernel.php
Open in GitHubuse Illuminate\Foundation\Http\Kernel as HttpKernel; use Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful; class Kernel extends HttpKernel { // 'api' => [ EnsureFrontendRequestsAreStateful::class, 'throttle:api', \Illuminate\Routing\Middleware\SubstituteBindings::class, ], // }
-
app/Http/Controllers/LoginController.php
Open in GitHubuse App\Models\User; class LoginController extends Controller { // public function handleProviderCallback($provider) { // $IfExists = User::where('email', $user->getEmail())->first(); if ($IfExists) { Auth::loginUsingId($IfExists->id, $remember = true); $token = $IfExists->createToken('token-name')->plainTextToken; $output = ['access_token' => $token, 'data' => json_encode($IfExists), 'message' => 'Login Success!', 'success' => true, 'error' => false]; return view('callback', $output); } else { try { // $token = $userCreated->createToken('token-name')->plainTextToken; // } catch (Exception $e) { DB::connection($this->getConnectionName())->rollback(); } } return response()->json($userCreated, 200, ['Access-Token' => $token]); } // }
-
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 Illuminate\Support\Facades\Route; // Route::get('getroles', 'RoleController@getRole')->middleware(['auth:sanctum']); //
-
app/Http/Controllers/RoleController.php
Open in GitHubuse App\Models\Role; use Illuminate\Http\Request; class RoleController extends Controller { // public function getRole(Request $request) { // } // }