{"id":1126,"date":"2024-08-07T05:38:33","date_gmt":"2024-08-07T05:38:33","guid":{"rendered":"https:\/\/www.appfinz.com\/blogs\/?p=1126"},"modified":"2025-05-15T04:52:37","modified_gmt":"2025-05-15T04:52:37","slug":"login-with-api-using-laravel-11","status":"publish","type":"post","link":"https:\/\/www.appfinz.com\/blogs\/login-with-api-using-laravel-11\/","title":{"rendered":"User Login with API Using Laravel 11"},"content":{"rendered":"\n<p>Hello Artisans, making <strong>User Login with API Using Laravel 11<\/strong> is very easy, In this blog, i will complete explain you that how you can implement and create a <strong>User Login with API Using Laravel 11<\/strong>, This in detailed Laravel API for User Creations involves setting up authentication, creating the necessary routes, controllers, and request handling. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Install Laravel Sanctum<\/h3>\n\n\n\n<p>Laravel Sanctum provides a lightweight authentication system for SPAs (single page applications), mobile applications, and simple, token-based APIs.<\/p>\n\n\n\n<p>First, install Sanctum via <strong><a href=\"https:\/\/getcomposer.org\/download\/\">Composer:<\/a><\/strong><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">composer require laravel\/sanctum<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Publish the Sanctum Configuration<\/h3>\n\n\n\n<p>Publish the <a href=\"https:\/\/laravel.com\/docs\/11.x\/sanctum\">Sanctum configuration<\/a> file using the following command:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">php artisan vendor:publish --provider=&quot;Laravel\\Sanctum\\SanctumServiceProvider&quot;<\/pre><\/div>\n\n\n\n<p>Configure Database First and Then we will do migrations:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">DB_CONNECTION=mysql\nDB_HOST=127.0.0.1\nDB_PORT=3306\nDB_DATABASE=your_database_name\nDB_USERNAME=your_database_username\nDB_PASSWORD=your_database_password\n<\/pre><\/div>\n\n\n\n<p>Run the Sanctum migrations:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">php artisan migrate<\/pre><\/div>\n\n\n\n<p>Read Also:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><a href=\"https:\/\/www.appfinz.com\/blogs\/whats-the-differences-between-put-and-patch-in-laravel\/\"><strong>Differences between PUT and PATCH? in laravel<\/strong><\/a><\/p>\n<\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong><a href=\"https:\/\/www.appfinz.com\/blogs\/paypal-payment-gateway-integration-in-laravel-11\/\">Paypal Payment Gateway Integration in Laravel 11<\/a><\/strong><\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Configure Sanctum Middleware<\/h3>\n\n\n\n<p>Add Sanctum&#8217;s middleware to your <code>api<\/code> middleware group within your <code>app\/Http\/Kernel.php<\/code> file:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">'api' =&gt; [\n    \\Laravel\\Sanctum\\Http\\Middleware\\EnsureFrontendRequestsAreStateful::class,\n    'throttle:api',\n    \\Illuminate\\Routing\\Middleware\\SubstituteBindings::class,\n],\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Update User Model<\/h3>\n\n\n\n<p>Ensure your <code>User<\/code> model uses the <code>HasApiTokens<\/code> trait provided by Sanctum:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">namespace App\\Models;\n\nuse Illuminate\\Foundation\\Auth\\User as Authenticatable;\nuse Laravel\\Sanctum\\HasApiTokens;\n\nclass User extends Authenticatable\n{\n    use HasApiTokens, Notifiable;\n\n    \/\/ Other model properties and methods\n}\n<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Step 5: Create Auth Controller<\/h3>\n\n\n\n<p>Create an <code>AuthController<\/code> to handle user registration, login, and logout:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">php artisan make:controller AuthController<\/pre><\/div>\n\n\n\n<p>In <code>AuthController.php<\/code>, implement the following methods:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">namespace App\\Http\\Controllers;\n\nuse Illuminate\\Http\\Request;\nuse Illuminate\\Support\\Facades\\Auth;\nuse App\\Models\\User;\nuse Illuminate\\Support\\Facades\\Hash;\nuse Illuminate\\Validation\\ValidationException;\n\nclass AuthController extends Controller\n{\n    public function register(Request $request)\n    {\n        $request-&gt;validate([\n            'name' =&gt; 'required|string|max:255',\n            'email' =&gt; 'required|string|email|max:255|unique:users',\n            'password' =&gt; 'required|string|min:8|confirmed',\n        ]);\n\n        $user = User::create([\n            'name' =&gt; $request-&gt;name,\n            'email' =&gt; $request-&gt;email,\n            'password' =&gt; Hash::make($request-&gt;password),\n        ]);\n\n        return response()-&gt;json(['message' =&gt; 'User registered successfully']);\n    }\n\n    public function login(Request $request)\n    {\n        $request-&gt;validate([\n            'email' =&gt; 'required|string|email',\n            'password' =&gt; 'required|string',\n        ]);\n\n        $user = User::where('email', $request-&gt;email)-&gt;first();\n\n        if (! $user || ! Hash::check($request-&gt;password, $user-&gt;password)) {\n            throw ValidationException::withMessages([\n                'email' =&gt; ['The provided credentials are incorrect.'],\n            ]);\n        }\n\n        $token = $user-&gt;createToken('auth_token')-&gt;plainTextToken;\n\n        return response()-&gt;json(['access_token' =&gt; $token, 'token_type' =&gt; 'Bearer']);\n    }\n\n    public function logout(Request $request)\n    {\n        $request-&gt;user()-&gt;tokens()-&gt;delete();\n\n        return response()-&gt;json(['message' =&gt; 'Logged out successfully']);\n    }\n}\n<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Step 6: Define Routes<\/h3>\n\n\n\n<p>Add routes for user registration, login, and logout in <code>routes\/api.php<\/code>:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">use App\\Http\\Controllers\\AuthController;\n\nRoute::post('register', [AuthController::class, 'register']);\nRoute::post('login', [AuthController::class, 'login']);\n\nRoute::middleware('auth:sanctum')-&gt;group(function () {\n    Route::post('logout', [AuthController::class, 'logout']);\n});\n<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Step 7: Test Your API<\/h3>\n\n\n\n<p>You can use tools like <strong><a href=\"https:\/\/www.postman.com\/\">Postman <\/a><\/strong>or cURL to test your API endpoints.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>POST \/api\/register<\/strong>: Register a new user.<\/li>\n\n\n\n<li><strong>POST \/api\/login<\/strong>: Login a user.<\/li>\n\n\n\n<li><strong>POST \/api\/logout<\/strong>: Logout a user (requires authentication).<\/li>\n<\/ul>\n\n\n\n<p>In this blog, we have covered about API generation in Laravel, User Login API generation in Laravel<\/p>\n\n\n\n<p>If you\u2019re planning to revamp your online presence, don\u2019t miss exploring our\u00a0<a href=\"https:\/\/www.appfinz.com\/website-designing-company-in-delhi\"><strong>website designing services<\/strong><\/a>\u00a0and custom\u00a0<strong><a href=\"https:\/\/www.appfinz.com\/website-development-company-in-delhi\">website development<\/a><\/strong>\u00a0solutions tailored to your business needs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello Artisans, making User Login with API Using Laravel 11 is very easy, In this<\/p>\n","protected":false},"author":1,"featured_media":1193,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,21],"tags":[184,183,181,182],"class_list":["post-1126","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","category-laravel","tag-api-for-mobile-apps","tag-api-in-laravel","tag-user-login","tag-user-login-with-api"],"_links":{"self":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts\/1126","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/comments?post=1126"}],"version-history":[{"count":13,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts\/1126\/revisions"}],"predecessor-version":[{"id":1350,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts\/1126\/revisions\/1350"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/media\/1193"}],"wp:attachment":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/media?parent=1126"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/categories?post=1126"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/tags?post=1126"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}