[{"data":1,"prerenderedAt":3574},["ShallowReactive",2],{"series-\u002Fseries\u002Fempower-and-dynamize-our-vitepress-blog-with-a-laravel-api\u002Feffortless-social-authentication-with-laravel-socialite":3,"series-context-\u002Fseries\u002Fempower-and-dynamize-our-vitepress-blog-with-a-laravel-api":3362},{"id":4,"title":5,"body":6,"contentId":3347,"date":3348,"description":3349,"extension":3350,"github":3351,"meetTheInstructor":3351,"meta":3352,"navigation":547,"part":167,"path":3353,"rawbody":3354,"robots":3351,"schemaOrg":3351,"seo":3355,"seriesSlug":3356,"sitemap":3357,"stem":3360,"subtitle":3351,"topics":3351,"__hash__":3361},"series\u002Fseries\u002Fempower-and-dynamize-our-vitepress-blog-with-a-laravel-api\u002Feffortless-social-authentication-with-laravel-socialite.md","Effortless Social Authentication with Laravel Socialite",{"type":7,"value":8,"toc":3337},"minimark",[9,13,30,33,36,39,44,47,50,53,56,84,92,101,106,110,113,140,147,201,212,234,237,240,260,285,290,293,323,334,345,356,366,371,390,397,416,422,425,429,432,437,442,448,466,469,483,486,504,525,532,605,617,627,630,646,649,721,732,786,793,812,828,831,940,943,1009,1011,1063,1074,1085,1089,1092,1103,1115,1126,1129,1347,1358,1365,1368,1378,1393,1401,1483,1498,1515,1523,1645,1653,1664,1669,1676,1679,1707,1717,1754,1759,1768,1775,1927,1930,1951,1956,1961,1968,1977,1987,2002,2164,2175,2195,2202,2212,2287,2296,2299,2302,2305,2308,2405,2411,2414,2542,2552,2799,2806,2812,2821,2835,2942,2945,2948,2997,3003,3006,3061,3064,3068,3071,3076,3079,3202,3209,3281,3287,3306,3309,3312,3317,3321,3327,3330,3333],[10,11,12],"p",{},"With the project structure and quality tools in place, we can start building the API's first user-facing feature: authentication. Since comments should belong to real users, this is a natural next step.",[10,14,15,16,23,24,29],{},"The problem is that authentication is hard, really hard. You have to deal with passwords, email verification, password resets, two-factor authentication, and more. Fortunately, frameworks such as Laravel make this process easier with ",[17,18,22],"a",{"href":19,"rel":20},"https:\u002F\u002Flaravel.com\u002Fdocs\u002F12.x\u002Fauthentication",[21],"nofollow","built-in authentication"," features and first-party packages such as ",[17,25,28],{"href":26,"rel":27},"https:\u002F\u002Flaravel.com\u002Fdocs\u002F12.x\u002Ffortify",[21],"Fortify",".",[10,31,32],{},"Even with these tools, we still have to build and manage many parts ourselves: registration, login, password resets, profile management, and the related edge cases. We also need a mail service to send emails.",[10,34,35],{},"All of this is feasible, but it takes time and effort. For a simple blog like ours, we can use GitHub for social authentication instead. This simplifies the implementation and lets users sign in with an account they already have.",[10,37,38],{},"Before we dive into the implementation, let's take a moment to understand what social authentication is and why it is useful.",[40,41,43],"h2",{"id":42},"social-authentication-in-a-nutshell","Social authentication in a nutshell",[10,45,46],{},"Traditionally, when a user wants to create an account on a website, they have to fill out a registration form with their email address and password. This process has worked well for years, but it has its drawbacks. On our side, we have to manage all the data, and on the user side, they have to remember yet another password.",[10,48,49],{},"With social authentication, users can log in using their existing accounts on platforms like GitHub, Google, Apple, or Facebook. This means they don't have to create a new account or remember another password. Instead, they can use their existing credentials to log in quickly and easily, which reduces friction and improves the user experience.",[10,51,52],{},"For the more technical among you, here's a schema of how it works:",[10,54,55],{},"The flow is straightforward:",[57,58,59,63,66,69,72,75,78,81],"ol",{},[60,61,62],"li",{},"The user clicks on the \"Log in with GitHub\" button on your website.",[60,64,65],{},"The user is redirected to GitHub's authorization page, where they can grant permission for your application to access their GitHub account.",[60,67,68],{},"If the user grants permission, GitHub redirects them back to your website with an authorization code.",[60,70,71],{},"Your website exchanges the authorization code for an access token, which can be used to access the user's GitHub account.",[60,73,74],{},"Your website can now use the access token to fetch the user's profile information from GitHub.",[60,76,77],{},"Your website creates a new user account, or updates an existing one, and logs in the user based on the information received from GitHub.",[60,79,80],{},"Your website redirects the user to the appropriate page after login.",[60,82,83],{},"The user is now logged in and can interact with your website.",[85,86,89],"callout",{"color":87,"icon":88},"info","i-ph-info-duotone",[10,90,91],{},"You have to note that the user is logged in via our application, not via GitHub. This is super important to avoid sending the access token to the client or to manage multiple providers, or a provider in addition to simple email\u002Fpassword authentication.",[10,93,94,95,100],{},"Simple, right? Laravel has a first-party package called ",[17,96,99],{"href":97,"rel":98},"https:\u002F\u002Flaravel.com\u002Fdocs\u002F12.x\u002Fsocialite",[21],"Socialite"," that makes this process even easier. It handles the provider-specific OAuth details so that our application can focus on its own users.",[85,102,103],{"color":87,"icon":88},[10,104,105],{},"Keep in mind that social authentication is not a miracle solution for every project. Even if it can be appealing at first, it can also come with its own set of challenges.",[40,107,109],{"id":108},"adding-socialite-to-the-project","Adding Socialite to the project",[10,111,112],{},"First, let's install the package:",[114,115,121],"pre",{"className":116,"code":117,"filename":118,"language":119,"meta":120,"style":120},"language-bash shiki shiki-themes github-light github-light github-dark","composer require laravel\u002Fsocialite\n","terminal","bash","",[122,123,124],"code",{"__ignoreMap":120},[125,126,129,133,137],"span",{"class":127,"line":128},"line",1,[125,130,132],{"class":131},"snPdu","composer",[125,134,136],{"class":135},"sIIMD"," require",[125,138,139],{"class":135}," laravel\u002Fsocialite\n",[10,141,142,143,146],{},"Next, configure it in ",[122,144,145],{},"config\u002Fservices.php",":",[114,148,152],{"className":149,"code":150,"filename":145,"language":151,"meta":120,"style":120},"language-php shiki shiki-themes github-light github-light github-dark","return [\n  \u002F\u002F ...\n  'github' => [\n      'client_id' => env('GITHUB_CLIENT_ID'),\n      'client_secret' => env('GITHUB_CLIENT_SECRET'),\n      'redirect' => env('GITHUB_REDIRECT'),\n  ],\n];\n","php",[122,153,154,159,165,171,177,183,189,195],{"__ignoreMap":120},[125,155,156],{"class":127,"line":128},[125,157,158],{},"return [\n",[125,160,162],{"class":127,"line":161},2,[125,163,164],{},"  \u002F\u002F ...\n",[125,166,168],{"class":127,"line":167},3,[125,169,170],{},"  'github' => [\n",[125,172,174],{"class":127,"line":173},4,[125,175,176],{},"      'client_id' => env('GITHUB_CLIENT_ID'),\n",[125,178,180],{"class":127,"line":179},5,[125,181,182],{},"      'client_secret' => env('GITHUB_CLIENT_SECRET'),\n",[125,184,186],{"class":127,"line":185},6,[125,187,188],{},"      'redirect' => env('GITHUB_REDIRECT'),\n",[125,190,192],{"class":127,"line":191},7,[125,193,194],{},"  ],\n",[125,196,198],{"class":127,"line":197},8,[125,199,200],{},"];\n",[10,202,203,204,207,208,211],{},"The ",[122,205,206],{},"env"," function reads these values from ",[122,209,210],{},".env",". Add the GitHub credentials and callback URL:",[114,213,217],{"className":214,"code":215,"filename":210,"language":216,"meta":120,"style":120},"language-ini shiki shiki-themes github-light github-light github-dark","GITHUB_CLIENT_ID=your-client-id\nGITHUB_CLIENT_SECRET=your-client-secret\nGITHUB_REDIRECT=http:\u002F\u002Flocalhost:8000\u002Fauth\u002Fgithub\u002Fcallback\n","ini",[122,218,219,224,229],{"__ignoreMap":120},[125,220,221],{"class":127,"line":128},[125,222,223],{},"GITHUB_CLIENT_ID=your-client-id\n",[125,225,226],{"class":127,"line":161},[125,227,228],{},"GITHUB_CLIENT_SECRET=your-client-secret\n",[125,230,231],{"class":127,"line":167},[125,232,233],{},"GITHUB_REDIRECT=http:\u002F\u002Flocalhost:8000\u002Fauth\u002Fgithub\u002Fcallback\n",[10,235,236],{},"The user is logged in through our application, not through GitHub. This distinction matters because we do not send the provider's access token to the client and can support multiple authentication providers later.",[10,238,239],{},"At this point, two questions usually arise:",[57,241,242,253],{},[60,243,244,245,248,249,252],{},"How do we get the ",[122,246,247],{},"GITHUB_CLIENT_ID"," and ",[122,250,251],{},"GITHUB_CLIENT_SECRET","?",[60,254,255,256,259],{},"Why do we need a ",[122,257,258],{},"GITHUB_REDIRECT"," URL and how do we get it?",[10,261,262,263,248,265,267,268,273,274,277,278,281,282,29],{},"GitHub provides ",[122,264,247],{},[122,266,251],{}," when you create an OAuth application. Open your ",[17,269,272],{"href":270,"rel":271},"https:\u002F\u002Fgithub.com\u002Fsettings\u002Fprofile",[21],"GitHub settings",", go to ",[122,275,276],{},"Developer settings",", select ",[122,279,280],{},"OAuth Apps",", and click ",[122,283,284],{},"New OAuth App",[85,286,287],{"color":87,"icon":88},[10,288,289],{},"An OAuth application is an application that uses the OAuth protocol to authenticate users. In our case, we are using GitHub as the OAuth provider. Having an OAuth application allows us to use GitHub's authentication system to log in users on our website.",[10,291,292],{},"Fill in the form. GitHub is quite permissive about the values you provide during local development.",[57,294,295,301,311],{},[60,296,203,297,300],{},[122,298,299],{},"Application name"," can be anything you want. I recommend using the name of your website because it is shown to users when they log in, at least the first time.",[60,302,203,303,306,307,310],{},[122,304,305],{},"Homepage URL"," is the URL of your website. You can use ",[122,308,309],{},"http:\u002F\u002Flocalhost:8000"," for local development.",[60,312,203,313,316,317,319,320,29],{},[122,314,315],{},"Authorization callback URL"," is where GitHub redirects users after they authorize the application. It must match the value in ",[122,318,210],{},". For local development, use ",[122,321,322],{},"http:\u002F\u002Flocalhost:8000\u002Fauth\u002Fgithub\u002Fcallback",[10,324,325,326,329,330,333],{},"Click ",[122,327,328],{},"Register application"," to create the OAuth application. On its page, you will find the ",[122,331,332],{},"Client ID",", which looks like this:",[114,335,339],{"className":336,"code":337,"filename":332,"language":338,"meta":120,"style":120},"language-txt shiki shiki-themes github-light github-light github-dark","a1b2c3d4e5f6g7h8i9j0\n","txt",[122,340,341],{"__ignoreMap":120},[125,342,343],{"class":127,"line":128},[125,344,337],{},[10,346,347,348,351,352,355],{},"A new ",[122,349,350],{},"Client secret"," must be generated. Click on ",[122,353,354],{},"Generate a new client secret"," and copy the value. It looks like this:",[114,357,360],{"className":336,"code":358,"filename":359,"language":338,"meta":120,"style":120},"d13f84af84688a69a71bc8b3808f8836e4c23774\n","Client Secret",[122,361,362],{"__ignoreMap":120},[125,363,364],{"class":127,"line":128},[125,365,358],{},[10,367,368,369,146],{},"Finally, add these values to ",[122,370,210],{},[114,372,374],{"className":214,"code":373,"filename":210,"language":216,"meta":120,"style":120},"GITHUB_CLIENT_ID=a1b2c3d4e5f6g7h8i9j0\nGITHUB_CLIENT_SECRET=d13f84af84688a69a71bc8b3808f8836e4c23774\nGITHUB_REDIRECT=http:\u002F\u002Flocalhost:8000\u002Fauth\u002Fgithub\u002Fcallback\n",[122,375,376,381,386],{"__ignoreMap":120},[125,377,378],{"class":127,"line":128},[125,379,380],{},"GITHUB_CLIENT_ID=a1b2c3d4e5f6g7h8i9j0\n",[125,382,383],{"class":127,"line":161},[125,384,385],{},"GITHUB_CLIENT_SECRET=d13f84af84688a69a71bc8b3808f8836e4c23774\n",[125,387,388],{"class":127,"line":167},[125,389,233],{},[10,391,392,393,396],{},"Don't forget to also add the environment variables in your ",[122,394,395],{},".env.example"," file.",[114,398,400],{"className":214,"code":399,"filename":395,"language":216,"meta":120,"style":120},"GITHUB_CLIENT_ID=\nGITHUB_CLIENT_SECRET=\nGITHUB_REDIRECT=http:\u002F\u002Flocalhost:8000\u002Fauth\u002Fgithub\u002Fcallback\n",[122,401,402,407,412],{"__ignoreMap":120},[125,403,404],{"class":127,"line":128},[125,405,406],{},"GITHUB_CLIENT_ID=\n",[125,408,409],{"class":127,"line":161},[125,410,411],{},"GITHUB_CLIENT_SECRET=\n",[125,413,414],{"class":127,"line":167},[125,415,233],{},[10,417,418,419,421],{},"Do not commit the client ID or secret. Leave those values empty in ",[122,420,395],{},", but keep the redirect URL so that anyone cloning the repository knows how to configure a local OAuth application.",[10,423,424],{},"Now that we have everything set up, let's write the code to handle the authentication.",[40,426,428],{"id":427},"handling-the-authentication","Handling the authentication",[10,430,431],{},"For the rest of the project, we will use a test-first approach: write a test, follow its failure, and add only the code needed to make it pass. This helps us define the behavior before implementation and avoids surprises later. It can feel intimidating at first, but Laravel makes the workflow approachable with its built-in testing tools.",[85,433,434],{"color":87,"icon":88},[10,435,436],{},"This may be test-driven development (TDD), but the label is less important here than the workflow: write the test first, then implement only what is necessary to make it pass.",[438,439,441],"h3",{"id":440},"the-redirect","The redirect",[10,443,444,445,146],{},"Let's create a new controller called ",[122,446,447],{},"RegisteredViaGitHubController",[114,449,451],{"className":116,"code":450,"filename":118,"language":119,"meta":120,"style":120},"php artisan make:controller RegisteredViaGitHubController\n",[122,452,453],{"__ignoreMap":120},[125,454,455,457,460,463],{"class":127,"line":128},[125,456,151],{"class":131},[125,458,459],{"class":135}," artisan",[125,461,462],{"class":135}," make:controller",[125,464,465],{"class":135}," RegisteredViaGitHubController\n",[10,467,468],{},"This controller will contain two methods:",[57,470,471,477],{},[60,472,473,476],{},[122,474,475],{},"create",": This method will handle the redirection to GitHub.",[60,478,479,482],{},[122,480,481],{},"store",": This method will handle the callback from GitHub.",[10,484,485],{},"I tend to use traditional method names for readability and consistency. Keeping these names in mind is useful because the tests will be organized around them.",[10,487,488,489,492,493,496,497,500,501,29],{},"In the folder ",[122,490,491],{},"tests\u002FHttp",", we can safely remove the ",[122,494,495],{},"ExampleTest.php"," file, and then create a new folder called ",[122,498,499],{},"RegisteredViaGitHub"," and a new test file called ",[122,502,503],{},"CreateTest.php",[114,505,507],{"className":116,"code":506,"filename":118,"language":119,"meta":120,"style":120},"mkdir tests\u002FHttp\u002FRegisteredViaGitHub\ntouch tests\u002FHttp\u002FRegisteredViaGitHub\u002FCreateTest.php\n",[122,508,509,517],{"__ignoreMap":120},[125,510,511,514],{"class":127,"line":128},[125,512,513],{"class":131},"mkdir",[125,515,516],{"class":135}," tests\u002FHttp\u002FRegisteredViaGitHub\n",[125,518,519,522],{"class":127,"line":161},[125,520,521],{"class":131},"touch",[125,523,524],{"class":135}," tests\u002FHttp\u002FRegisteredViaGitHub\u002FCreateTest.php\n",[10,526,527,528],{},"Within this file, let's write our first test. ",[529,530,531],"em",{},"Really exciting!",[114,533,536],{"className":149,"code":534,"filename":535,"language":151,"meta":120,"style":120},"\u003C?php\n\ndeclare(strict_types=1);\n\nuse Laravel\\Socialite\\Facades\\Socialite;\n\ntest('redirects for authorization', function () {\n    Socialite::shouldReceive('driver->redirect')->once();\n\n    $this->get(route('auth.github.redirect'));\n\n    expect(true)->toBeTrue();\n});\n","tests\u002FHttp\u002FRegisteredViaGitHub\u002FCreateTest.php",[122,537,538,543,549,554,558,563,567,572,577,582,588,593,599],{"__ignoreMap":120},[125,539,540],{"class":127,"line":128},[125,541,542],{},"\u003C?php\n",[125,544,545],{"class":127,"line":161},[125,546,548],{"emptyLinePlaceholder":547},true,"\n",[125,550,551],{"class":127,"line":167},[125,552,553],{},"declare(strict_types=1);\n",[125,555,556],{"class":127,"line":173},[125,557,548],{"emptyLinePlaceholder":547},[125,559,560],{"class":127,"line":179},[125,561,562],{},"use Laravel\\Socialite\\Facades\\Socialite;\n",[125,564,565],{"class":127,"line":185},[125,566,548],{"emptyLinePlaceholder":547},[125,568,569],{"class":127,"line":191},[125,570,571],{},"test('redirects for authorization', function () {\n",[125,573,574],{"class":127,"line":197},[125,575,576],{},"    Socialite::shouldReceive('driver->redirect')->once();\n",[125,578,580],{"class":127,"line":579},9,[125,581,548],{"emptyLinePlaceholder":547},[125,583,585],{"class":127,"line":584},10,[125,586,587],{},"    $this->get(route('auth.github.redirect'));\n",[125,589,591],{"class":127,"line":590},11,[125,592,548],{"emptyLinePlaceholder":547},[125,594,596],{"class":127,"line":595},12,[125,597,598],{},"    expect(true)->toBeTrue();\n",[125,600,602],{"class":127,"line":601},13,[125,603,604],{},"});\n",[85,606,607],{"color":87,"icon":88},[10,608,609,612,613,616],{},[122,610,611],{},"expect(true)->toBeTrue()"," gives the test an assertion without adding behavior that we need to verify. If ",[122,614,615],{},"Socialite::shouldReceive('driver->redirect')->once()"," is not satisfied, the mock will fail the test anyway.",[10,618,619,620,622,623,626],{},"This is our first test, but not our simplest one. It mocks the ",[122,621,99],{}," facade and verifies that ",[122,624,625],{},"redirect"," is called once. We use a mock because the test should not send us to GitHub; it only needs to verify that our code asks Socialite to perform the redirect.",[10,628,629],{},"Let's run the test:",[114,631,633],{"className":116,"code":632,"filename":118,"language":119,"meta":120,"style":120},"php artisan test tests\u002FHttp\u002FRegisteredViaGitHub\u002FCreateTest.php\n",[122,634,635],{"__ignoreMap":120},[125,636,637,639,641,644],{"class":127,"line":128},[125,638,151],{"class":131},[125,640,459],{"class":135},[125,642,643],{"class":135}," test",[125,645,524],{"class":135},[10,647,648],{},"It fails, as expected. In a test-first workflow, the failure tells us what to implement next. We follow the error message and add only the code needed to make the test pass.",[114,650,652],{"className":116,"code":651,"filename":118,"language":119,"meta":120,"style":120},"➜ php artisan test tests\u002FHttp\u002FRegisteredViaGitHub\u002FCreateTest.php\n\nFAILED  Tests\\Http\\RegisteredViaGitHub\\CreateTest > redirects for authorization\n  Route [auth.github.redirect] not defined.\n",[122,653,654,668,672,712],{"__ignoreMap":120},[125,655,656,659,662,664,666],{"class":127,"line":128},[125,657,658],{"class":131},"➜",[125,660,661],{"class":135}," php",[125,663,459],{"class":135},[125,665,643],{"class":135},[125,667,524],{"class":135},[125,669,670],{"class":127,"line":161},[125,671,548],{"emptyLinePlaceholder":547},[125,673,674,677,680,684,687,690,693,696,699,703,706,709],{"class":127,"line":167},[125,675,676],{"class":131},"FAILED",[125,678,679],{"class":135},"  Tests",[125,681,683],{"class":682},"sBjJW","\\H",[125,685,686],{"class":135},"ttp",[125,688,689],{"class":682},"\\R",[125,691,692],{"class":135},"egisteredViaGitHub",[125,694,695],{"class":682},"\\C",[125,697,698],{"class":135},"reateTest",[125,700,702],{"class":701},"s8jYJ"," >",[125,704,705],{"class":135}," redirects",[125,707,708],{"class":135}," for",[125,710,711],{"class":135}," authorization\n",[125,713,714,717],{"class":127,"line":173},[125,715,716],{"class":131},"  Route",[125,718,720],{"class":719},"sxrX7"," [auth.github.redirect] not defined.\n",[10,722,723,724,727,728,731],{},"The error message is clear, we have to define the route ",[122,725,726],{},"auth.github.redirect",". Let's do this in our ",[122,729,730],{},"routes\u002Fweb.php"," file:",[114,733,735],{"className":149,"code":734,"filename":730,"language":151,"meta":120,"style":120},"\u003C?php\n\ndeclare(strict_types=1);\n\nuse App\\Http\\Controllers\\RegisteredViaGitHubController;\nuse Illuminate\\Support\\Facades\\Route;\n\n\u002F\u002F other routes...\n\nRoute::get('\u002Fauth\u002Fgithub\u002Fredirect', [RegisteredViaGitHubController::class, 'create'])\n    ->name('auth.github.redirect');\n",[122,736,737,741,745,749,753,758,763,767,772,776,781],{"__ignoreMap":120},[125,738,739],{"class":127,"line":128},[125,740,542],{},[125,742,743],{"class":127,"line":161},[125,744,548],{"emptyLinePlaceholder":547},[125,746,747],{"class":127,"line":167},[125,748,553],{},[125,750,751],{"class":127,"line":173},[125,752,548],{"emptyLinePlaceholder":547},[125,754,755],{"class":127,"line":179},[125,756,757],{},"use App\\Http\\Controllers\\RegisteredViaGitHubController;\n",[125,759,760],{"class":127,"line":185},[125,761,762],{},"use Illuminate\\Support\\Facades\\Route;\n",[125,764,765],{"class":127,"line":191},[125,766,548],{"emptyLinePlaceholder":547},[125,768,769],{"class":127,"line":197},[125,770,771],{},"\u002F\u002F other routes...\n",[125,773,774],{"class":127,"line":579},[125,775,548],{"emptyLinePlaceholder":547},[125,777,778],{"class":127,"line":584},[125,779,780],{},"Route::get('\u002Fauth\u002Fgithub\u002Fredirect', [RegisteredViaGitHubController::class, 'create'])\n",[125,782,783],{"class":127,"line":590},[125,784,785],{},"    ->name('auth.github.redirect');\n",[10,787,788,789,792],{},"But what is ",[122,790,791],{},"web.php","? It is where we define browser-facing routes and map URLs to controller methods. Reading this file gives us a useful overview of the application's entry points.",[85,794,797],{"color":795,"icon":796},"success","i-ph-lightbulb-duotone",[10,798,799,800,805,806,811],{},"I highly recommend you to watch ",[17,801,804],{"href":802,"rel":803},"https:\u002F\u002Fyoutu.be\u002FNjkxzc-aqts?si=qA5qWuVrm02VtJUX",[21],"Lessons From the Framework"," by ",[17,807,810],{"href":808,"rel":809},"https:\u002F\u002Fgithub.com\u002Flukeraymonddowning",[21],"Luke Dowing"," to understand how central this file is.",[10,813,814,815,818,819,821,822,824,825,827],{},"This route responds to ",[122,816,817],{},"\u002Fauth\u002Fgithub\u002Fredirect"," and calls the ",[122,820,475],{}," method on ",[122,823,447],{},". We also give it the name ",[122,826,726],{},", which lets us reference it from the test and elsewhere in the application.",[10,829,830],{},"Let's run the test again:",[114,832,834],{"className":116,"code":833,"filename":118,"language":119,"meta":120,"style":120},"➜ php artisan test tests\u002FHttp\u002FRegisteredViaGitHub\u002FCreateTest.php\n\n FAILED  Tests\\Http\\RegisteredViaGitHub\\CreateTest > redirects for authorization\n  Method redirect(\u003CAny Arguments>) from Mockery_1__demeter_c357969a11a830028afbe426ab0eea17_driver should be called\n exactly 1 times but called 0 times.\n",[122,835,836,848,852,879,917],{"__ignoreMap":120},[125,837,838,840,842,844,846],{"class":127,"line":128},[125,839,658],{"class":131},[125,841,661],{"class":135},[125,843,459],{"class":135},[125,845,643],{"class":135},[125,847,524],{"class":135},[125,849,850],{"class":127,"line":161},[125,851,548],{"emptyLinePlaceholder":547},[125,853,854,857,859,861,863,865,867,869,871,873,875,877],{"class":127,"line":167},[125,855,856],{"class":131}," FAILED",[125,858,679],{"class":135},[125,860,683],{"class":682},[125,862,686],{"class":135},[125,864,689],{"class":682},[125,866,692],{"class":135},[125,868,695],{"class":682},[125,870,698],{"class":135},[125,872,702],{"class":701},[125,874,705],{"class":135},[125,876,708],{"class":135},[125,878,711],{"class":135},[125,880,881,884,887,890,893,896,899,902,905,908,911,914],{"class":127,"line":173},[125,882,883],{"class":131},"  Method",[125,885,886],{"class":135}," redirect",[125,888,889],{"class":719},"(",[125,891,892],{"class":701},"\u003C",[125,894,895],{"class":719},"Any Arguments",[125,897,898],{"class":701},">",[125,900,901],{"class":719},") ",[125,903,904],{"class":135},"from",[125,906,907],{"class":135}," Mockery_1__demeter_c357969a11a830028afbe426ab0eea17_driver",[125,909,910],{"class":135}," should",[125,912,913],{"class":135}," be",[125,915,916],{"class":135}," called\n",[125,918,919,922,925,928,931,934,937],{"class":127,"line":179},[125,920,921],{"class":131}," exactly",[125,923,924],{"class":682}," 1",[125,926,927],{"class":135}," times",[125,929,930],{"class":135}," but",[125,932,933],{"class":135}," called",[125,935,936],{"class":682}," 0",[125,938,939],{"class":135}," times.\n",[10,941,942],{},"The new error message means that the route works, but the controller is not calling Socialite yet. Add that call to the controller:",[114,944,947],{"className":149,"code":945,"filename":946,"language":151,"meta":120,"style":120},"\u003C?php\n\nnamespace App\\Http\\Controllers;\n\nuse Laravel\\Socialite\\Facades\\Socialite;\n\nclass RegisteredViaGitHubController extends Controller\n{\n    public function create()\n    {\n        return Socialite::driver('github')->redirect();\n    }\n}\n","app\u002FHttp\u002FControllers\u002FRegisteredViaGitHubController.php",[122,948,949,953,957,962,966,970,974,979,984,989,994,999,1004],{"__ignoreMap":120},[125,950,951],{"class":127,"line":128},[125,952,542],{},[125,954,955],{"class":127,"line":161},[125,956,548],{"emptyLinePlaceholder":547},[125,958,959],{"class":127,"line":167},[125,960,961],{},"namespace App\\Http\\Controllers;\n",[125,963,964],{"class":127,"line":173},[125,965,548],{"emptyLinePlaceholder":547},[125,967,968],{"class":127,"line":179},[125,969,562],{},[125,971,972],{"class":127,"line":185},[125,973,548],{"emptyLinePlaceholder":547},[125,975,976],{"class":127,"line":191},[125,977,978],{},"class RegisteredViaGitHubController extends Controller\n",[125,980,981],{"class":127,"line":197},[125,982,983],{},"{\n",[125,985,986],{"class":127,"line":579},[125,987,988],{},"    public function create()\n",[125,990,991],{"class":127,"line":584},[125,992,993],{},"    {\n",[125,995,996],{"class":127,"line":590},[125,997,998],{},"        return Socialite::driver('github')->redirect();\n",[125,1000,1001],{"class":127,"line":595},[125,1002,1003],{},"    }\n",[125,1005,1006],{"class":127,"line":601},[125,1007,1008],{},"}\n",[10,1010,830],{},[114,1012,1014],{"className":116,"code":1013,"filename":118,"language":119,"meta":120,"style":120},"➜ php artisan test tests\u002FHttp\u002FRegisteredViaGitHub\u002FCreateTest.php\n\n   PASS  Tests\\Http\\RegisteredViaGitHub\\CreateTest\n  ✓ redirects for authorization\n",[122,1015,1016,1028,1032,1052],{"__ignoreMap":120},[125,1017,1018,1020,1022,1024,1026],{"class":127,"line":128},[125,1019,658],{"class":131},[125,1021,661],{"class":135},[125,1023,459],{"class":135},[125,1025,643],{"class":135},[125,1027,524],{"class":135},[125,1029,1030],{"class":127,"line":161},[125,1031,548],{"emptyLinePlaceholder":547},[125,1033,1034,1037,1039,1041,1043,1045,1047,1049],{"class":127,"line":167},[125,1035,1036],{"class":131},"   PASS",[125,1038,679],{"class":135},[125,1040,683],{"class":682},[125,1042,686],{"class":135},[125,1044,689],{"class":682},[125,1046,692],{"class":135},[125,1048,695],{"class":682},[125,1050,1051],{"class":135},"reateTest\n",[125,1053,1054,1057,1059,1061],{"class":127,"line":173},[125,1055,1056],{"class":131},"  ✓",[125,1058,705],{"class":135},[125,1060,708],{"class":135},[125,1062,711],{"class":135},[10,1064,1065,1066,1069,1070,1073],{},"And voilà! The test passes. Start the development server with ",[122,1067,1068],{},"composer dev"," and open ",[122,1071,1072],{},"http:\u002F\u002Flocalhost:8000\u002Fauth\u002Fgithub\u002Fredirect"," to verify the redirect manually. You should arrive at GitHub's authorization page. 🥳",[1075,1076,1077,1082],"figure",{},[1078,1079],"img",{"alt":1080,"src":1081},"Once redirected, you should arrive on the GitHub authorization page","\u002Fseries\u002Fempower-and-dynamize-our-vitepress-blog-with-a-laravel-api\u002Feffortless-social-authentication-with-laravel-socialite\u002Fgithub-authorization.png",[1083,1084,1080],"figcaption",{},[438,1086,1088],{"id":1087},"the-callback","The callback",[10,1090,1091],{},"Now that you understand the workflow, I will not repeat the instruction to run the test after every small change. Keep following the same loop for the rest of the project and in your own projects.",[10,1093,1094,1095,1098,1099,1102],{},"Let's create a new test file called ",[122,1096,1097],{},"StoreTest.php"," in the ",[122,1100,1101],{},"tests\u002FHttp\u002FRegisteredViaGitHub"," folder.",[114,1104,1106],{"className":116,"code":1105,"filename":118,"language":119,"meta":120,"style":120},"touch tests\u002FHttp\u002FRegisteredViaGitHub\u002FStoreTest.php\n",[122,1107,1108],{"__ignoreMap":120},[125,1109,1110,1112],{"class":127,"line":128},[125,1111,521],{"class":131},[125,1113,1114],{"class":135}," tests\u002FHttp\u002FRegisteredViaGitHub\u002FStoreTest.php\n",[10,1116,1117,1119,1120,1122,1123,1125],{},[122,1118,1097],{}," will test the ",[122,1121,481],{}," method of ",[122,1124,447],{},", which handles GitHub's callback. Socialite handles the OAuth exchange, so our application only needs to retrieve the provider user, save it, and authenticate that user.",[10,1127,1128],{},"First, let's write a test to ensure that a user can be logged in:",[114,1130,1133],{"className":149,"code":1131,"filename":1132,"language":151,"meta":120,"style":120},"\u003C?php\n\ndeclare(strict_types=1);\n\nuse Laravel\\Socialite\\Facades\\Socialite;\nuse Laravel\\Socialite\\Two\\User as SocialiteUser;\n\ntest('registers successfully', function () {\n    $fakeGitHubUser = fakeGitHubUser();\n\n    $this->get(route('auth.github.callback'));\n\n    $this->assertAuthenticated();\n\n    $this->assertDatabaseHas('users', [\n        'github_id' => $fakeGitHubUser->getId(),\n        'email' => $fakeGitHubUser->getEmail(),\n        'name' => $fakeGitHubUser->getName(),\n    ]);\n});\n\n\u002F**\n * Create a fake GitHub user and mock the Socialite driver.\n *\n * @param  array\u003Cstring, mixed>  $attributes\n *\u002F\nfunction fakeGitHubUser(array $attributes = []): SocialiteUser\n{\n    $fakeGitHubUser = (new SocialiteUser)->map(attributes: array_merge([\n        'id' => '12345',\n        'nickname' => 'testuser',\n        'email' => 'test@example.com',\n        'name' => 'Test User',\n        'avatar' => 'https:\u002F\u002Fexample.com\u002Favatar.jpg',\n    ], $attributes));\n\n    Socialite::shouldReceive('driver->user')->once()->andReturn($fakeGitHubUser);\n\n    return $fakeGitHubUser;\n}\n","tests\u002FHttp\u002FRegisteredViaGitHub\u002FStoreTest.php",[122,1134,1135,1139,1143,1147,1151,1155,1160,1164,1169,1174,1178,1183,1187,1192,1197,1203,1209,1215,1221,1227,1232,1237,1243,1249,1255,1261,1267,1273,1278,1284,1290,1296,1302,1308,1314,1320,1325,1331,1336,1342],{"__ignoreMap":120},[125,1136,1137],{"class":127,"line":128},[125,1138,542],{},[125,1140,1141],{"class":127,"line":161},[125,1142,548],{"emptyLinePlaceholder":547},[125,1144,1145],{"class":127,"line":167},[125,1146,553],{},[125,1148,1149],{"class":127,"line":173},[125,1150,548],{"emptyLinePlaceholder":547},[125,1152,1153],{"class":127,"line":179},[125,1154,562],{},[125,1156,1157],{"class":127,"line":185},[125,1158,1159],{},"use Laravel\\Socialite\\Two\\User as SocialiteUser;\n",[125,1161,1162],{"class":127,"line":191},[125,1163,548],{"emptyLinePlaceholder":547},[125,1165,1166],{"class":127,"line":197},[125,1167,1168],{},"test('registers successfully', function () {\n",[125,1170,1171],{"class":127,"line":579},[125,1172,1173],{},"    $fakeGitHubUser = fakeGitHubUser();\n",[125,1175,1176],{"class":127,"line":584},[125,1177,548],{"emptyLinePlaceholder":547},[125,1179,1180],{"class":127,"line":590},[125,1181,1182],{},"    $this->get(route('auth.github.callback'));\n",[125,1184,1185],{"class":127,"line":595},[125,1186,548],{"emptyLinePlaceholder":547},[125,1188,1189],{"class":127,"line":601},[125,1190,1191],{},"    $this->assertAuthenticated();\n",[125,1193,1195],{"class":127,"line":1194},14,[125,1196,548],{"emptyLinePlaceholder":547},[125,1198,1200],{"class":127,"line":1199},15,[125,1201,1202],{},"    $this->assertDatabaseHas('users', [\n",[125,1204,1206],{"class":127,"line":1205},16,[125,1207,1208],{},"        'github_id' => $fakeGitHubUser->getId(),\n",[125,1210,1212],{"class":127,"line":1211},17,[125,1213,1214],{},"        'email' => $fakeGitHubUser->getEmail(),\n",[125,1216,1218],{"class":127,"line":1217},18,[125,1219,1220],{},"        'name' => $fakeGitHubUser->getName(),\n",[125,1222,1224],{"class":127,"line":1223},19,[125,1225,1226],{},"    ]);\n",[125,1228,1230],{"class":127,"line":1229},20,[125,1231,604],{},[125,1233,1235],{"class":127,"line":1234},21,[125,1236,548],{"emptyLinePlaceholder":547},[125,1238,1240],{"class":127,"line":1239},22,[125,1241,1242],{},"\u002F**\n",[125,1244,1246],{"class":127,"line":1245},23,[125,1247,1248],{}," * Create a fake GitHub user and mock the Socialite driver.\n",[125,1250,1252],{"class":127,"line":1251},24,[125,1253,1254],{}," *\n",[125,1256,1258],{"class":127,"line":1257},25,[125,1259,1260],{}," * @param  array\u003Cstring, mixed>  $attributes\n",[125,1262,1264],{"class":127,"line":1263},26,[125,1265,1266],{}," *\u002F\n",[125,1268,1270],{"class":127,"line":1269},27,[125,1271,1272],{},"function fakeGitHubUser(array $attributes = []): SocialiteUser\n",[125,1274,1276],{"class":127,"line":1275},28,[125,1277,983],{},[125,1279,1281],{"class":127,"line":1280},29,[125,1282,1283],{},"    $fakeGitHubUser = (new SocialiteUser)->map(attributes: array_merge([\n",[125,1285,1287],{"class":127,"line":1286},30,[125,1288,1289],{},"        'id' => '12345',\n",[125,1291,1293],{"class":127,"line":1292},31,[125,1294,1295],{},"        'nickname' => 'testuser',\n",[125,1297,1299],{"class":127,"line":1298},32,[125,1300,1301],{},"        'email' => 'test@example.com',\n",[125,1303,1305],{"class":127,"line":1304},33,[125,1306,1307],{},"        'name' => 'Test User',\n",[125,1309,1311],{"class":127,"line":1310},34,[125,1312,1313],{},"        'avatar' => 'https:\u002F\u002Fexample.com\u002Favatar.jpg',\n",[125,1315,1317],{"class":127,"line":1316},35,[125,1318,1319],{},"    ], $attributes));\n",[125,1321,1323],{"class":127,"line":1322},36,[125,1324,548],{"emptyLinePlaceholder":547},[125,1326,1328],{"class":127,"line":1327},37,[125,1329,1330],{},"    Socialite::shouldReceive('driver->user')->once()->andReturn($fakeGitHubUser);\n",[125,1332,1334],{"class":127,"line":1333},38,[125,1335,548],{"emptyLinePlaceholder":547},[125,1337,1339],{"class":127,"line":1338},39,[125,1340,1341],{},"    return $fakeGitHubUser;\n",[125,1343,1345],{"class":127,"line":1344},40,[125,1346,1008],{},[10,1348,1349,1350,1353,1354,1357],{},"This test creates a fake GitHub user and mocks Socialite to return it when ",[122,1351,1352],{},"user"," is called. We avoid a real HTTP request to GitHub, and the ",[122,1355,1356],{},"fakeGitHubUser"," helper keeps that setup reusable and the test readable.",[10,1359,1360,1361,1364],{},"Then, we call the ",[122,1362,1363],{},"auth.github.callback"," route and assert that the user is authenticated and saved in the database.",[10,1366,1367],{},"Now, let's follow the error message from our test.",[57,1369,1370],{},[60,1371,1372,1373,1375,1376,731],{},"The route ",[122,1374,1363],{}," is not defined. Let's define it in our ",[122,1377,730],{},[114,1379,1381],{"className":149,"code":1380,"filename":730,"language":151,"meta":120,"style":120},"Route::get('\u002Fauth\u002Fgithub\u002Fcallback', [RegisteredViaGitHubController::class, 'store'])\n    ->name('auth.github.callback');\n",[122,1382,1383,1388],{"__ignoreMap":120},[125,1384,1385],{"class":127,"line":128},[125,1386,1387],{},"Route::get('\u002Fauth\u002Fgithub\u002Fcallback', [RegisteredViaGitHubController::class, 'store'])\n",[125,1389,1390],{"class":127,"line":161},[125,1391,1392],{},"    ->name('auth.github.callback');\n",[57,1394,1395],{"start":161},[60,1396,1397,1398,1400],{},"The user is not authenticated. Implement the ",[122,1399,481],{}," method in the controller:",[114,1402,1404],{"className":149,"code":1403,"filename":946,"language":151,"meta":120,"style":120},"\u003C?php\n\ndeclare(strict_types=1);\n\nnamespace App\\Http\\Controllers;\n\nuse Illuminate\\Support\\Facades\\Auth;\nuse Laravel\\Socialite\\Facades\\Socialite;\n\nfinal class RegisteredViaGitHubController extends Controller\n{\n    public function store()\n    {\n        $githubUser = Socialite::driver('github')->user();\n\n        Auth::login($githubUser);\n    }\n}\n",[122,1405,1406,1410,1414,1418,1422,1426,1430,1435,1439,1443,1448,1452,1457,1461,1466,1470,1475,1479],{"__ignoreMap":120},[125,1407,1408],{"class":127,"line":128},[125,1409,542],{},[125,1411,1412],{"class":127,"line":161},[125,1413,548],{"emptyLinePlaceholder":547},[125,1415,1416],{"class":127,"line":167},[125,1417,553],{},[125,1419,1420],{"class":127,"line":173},[125,1421,548],{"emptyLinePlaceholder":547},[125,1423,1424],{"class":127,"line":179},[125,1425,961],{},[125,1427,1428],{"class":127,"line":185},[125,1429,548],{"emptyLinePlaceholder":547},[125,1431,1432],{"class":127,"line":191},[125,1433,1434],{},"use Illuminate\\Support\\Facades\\Auth;\n",[125,1436,1437],{"class":127,"line":197},[125,1438,562],{},[125,1440,1441],{"class":127,"line":579},[125,1442,548],{"emptyLinePlaceholder":547},[125,1444,1445],{"class":127,"line":584},[125,1446,1447],{},"final class RegisteredViaGitHubController extends Controller\n",[125,1449,1450],{"class":127,"line":590},[125,1451,983],{},[125,1453,1454],{"class":127,"line":595},[125,1455,1456],{},"    public function store()\n",[125,1458,1459],{"class":127,"line":601},[125,1460,993],{},[125,1462,1463],{"class":127,"line":1194},[125,1464,1465],{},"        $githubUser = Socialite::driver('github')->user();\n",[125,1467,1468],{"class":127,"line":1199},[125,1469,548],{"emptyLinePlaceholder":547},[125,1471,1472],{"class":127,"line":1205},[125,1473,1474],{},"        Auth::login($githubUser);\n",[125,1476,1477],{"class":127,"line":1211},[125,1478,1003],{},[125,1480,1481],{"class":127,"line":1217},[125,1482,1008],{},[10,1484,1485,1488,1489,1493,1494,1497],{},[122,1486,1487],{},"Socialite::driver('github')->user()"," handles the OAuth flow and returns the provider ",[17,1490,1352],{"href":1491,"rel":1492},"https:\u002F\u002Fgithub.com\u002Flaravel\u002Fsocialite\u002Fblob\u002F5.x\u002Fsrc\u002FAbstractUser.php",[21],". We then use Laravel's ",[122,1495,1496],{},"Auth"," facade to log in that user.",[57,1499,1500],{"start":167},[60,1501,1502,1503,1506,1507,1514],{},"The user is still not authenticated. The ",[122,1504,1505],{},"login"," method expects an ",[17,1508,1511],{"href":1509,"rel":1510},"https:\u002F\u002Fgithub.com\u002Flaravel\u002Fframework\u002Fblob\u002F12.x\u002Fsrc\u002FIlluminate\u002FAuth\u002FAuthenticatable.php",[21],[122,1512,1513],{},"Authenticatable"," application user, but we are passing a Socialite user.",[10,1516,1517,1518,1520,1521,29],{},"We cannot pass the Socialite user directly to ",[122,1519,1505],{},". We must first create or retrieve an application user and then pass that model to ",[122,1522,1505],{},[114,1524,1526],{"className":149,"code":1525,"filename":946,"language":151,"meta":120,"style":120},"\u003C?php\n\ndeclare(strict_types=1);\n\nnamespace App\\Http\\Controllers;\n\nuse App\\Models\\User;\nuse Illuminate\\Support\\Facades\\Auth;\nuse Laravel\\Socialite\\Facades\\Socialite;\n\nfinal class RegisteredViaGitHubController extends Controller\n{\n    public function store()\n    {\n        $githubUser = Socialite::driver('github')->user();\n\n        $user = User::create([\n            'name' => $githubUser->getName(),\n            'email' => $githubUser->getEmail(),\n            'username' => $githubUser->getNickname(),\n            'github_id' => $githubUser->getId(),\n            'avatar' => $githubUser->getAvatar(),\n        ]);\n\n        Auth::login($user);\n    }\n}\n",[122,1527,1528,1532,1536,1540,1544,1548,1552,1557,1561,1565,1569,1573,1577,1581,1585,1589,1593,1598,1603,1608,1613,1618,1623,1628,1632,1637,1641],{"__ignoreMap":120},[125,1529,1530],{"class":127,"line":128},[125,1531,542],{},[125,1533,1534],{"class":127,"line":161},[125,1535,548],{"emptyLinePlaceholder":547},[125,1537,1538],{"class":127,"line":167},[125,1539,553],{},[125,1541,1542],{"class":127,"line":173},[125,1543,548],{"emptyLinePlaceholder":547},[125,1545,1546],{"class":127,"line":179},[125,1547,961],{},[125,1549,1550],{"class":127,"line":185},[125,1551,548],{"emptyLinePlaceholder":547},[125,1553,1554],{"class":127,"line":191},[125,1555,1556],{},"use App\\Models\\User;\n",[125,1558,1559],{"class":127,"line":197},[125,1560,1434],{},[125,1562,1563],{"class":127,"line":579},[125,1564,562],{},[125,1566,1567],{"class":127,"line":584},[125,1568,548],{"emptyLinePlaceholder":547},[125,1570,1571],{"class":127,"line":590},[125,1572,1447],{},[125,1574,1575],{"class":127,"line":595},[125,1576,983],{},[125,1578,1579],{"class":127,"line":601},[125,1580,1456],{},[125,1582,1583],{"class":127,"line":1194},[125,1584,993],{},[125,1586,1587],{"class":127,"line":1199},[125,1588,1465],{},[125,1590,1591],{"class":127,"line":1205},[125,1592,548],{"emptyLinePlaceholder":547},[125,1594,1595],{"class":127,"line":1211},[125,1596,1597],{},"        $user = User::create([\n",[125,1599,1600],{"class":127,"line":1217},[125,1601,1602],{},"            'name' => $githubUser->getName(),\n",[125,1604,1605],{"class":127,"line":1223},[125,1606,1607],{},"            'email' => $githubUser->getEmail(),\n",[125,1609,1610],{"class":127,"line":1229},[125,1611,1612],{},"            'username' => $githubUser->getNickname(),\n",[125,1614,1615],{"class":127,"line":1234},[125,1616,1617],{},"            'github_id' => $githubUser->getId(),\n",[125,1619,1620],{"class":127,"line":1239},[125,1621,1622],{},"            'avatar' => $githubUser->getAvatar(),\n",[125,1624,1625],{"class":127,"line":1245},[125,1626,1627],{},"        ]);\n",[125,1629,1630],{"class":127,"line":1251},[125,1631,548],{"emptyLinePlaceholder":547},[125,1633,1634],{"class":127,"line":1257},[125,1635,1636],{},"        Auth::login($user);\n",[125,1638,1639],{"class":127,"line":1263},[125,1640,1003],{},[125,1642,1643],{"class":127,"line":1269},[125,1644,1008],{},[57,1646,1647],{"start":173},[60,1648,1649,1650,146],{},"The user is still not authenticated. This time, check the log file. In Laravel, the log is stored in ",[122,1651,1652],{},"storage\u002Flogs\u002Flaravel.log",[114,1654,1658],{"className":1655,"code":1656,"filename":1652,"language":1657,"meta":120,"style":120},"language-log shiki shiki-themes github-light github-light github-dark","[2025-02-28 06:51:21] testing.ERROR: SQLSTATE[HY000]: General error: 1 no such table: users (Connection: sqlite, SQL: insert into \"users\" (\"name\", \"email\", \"updated_at\", \"created_at\") values (Test User, test@example.com, 2025-02-28 06:51:21, 2025-02-28 06:51:21))\n","log",[122,1659,1660],{"__ignoreMap":120},[125,1661,1662],{"class":127,"line":128},[125,1663,1656],{},[85,1665,1666],{"color":795,"icon":796},[10,1667,1668],{},"Do not hesitate to remove all the contents of the file before re-running the test. This way, you will only see the relevant logs.",[10,1670,1671,1672,1675],{},"This error message is clearer than the previous one: the ",[122,1673,1674],{},"users"," table does not exist, so the request fails before the user can be authenticated.",[10,1677,1678],{},"Before creating the table, inspect the database migrations:",[57,1680,1681,1690,1699],{},[60,1682,1683,1686,1687,1689],{},[122,1684,1685],{},"0001_01_01_000000_create_users_table",": This migration creates the ",[122,1688,1674],{}," table. The table exists in the schema, but the test database has not run the migration yet.",[60,1691,1692,1686,1695,1698],{},[122,1693,1694],{},"0001_01_01_000001_create_cache_table",[122,1696,1697],{},"cache"," table. We don't care about this one.",[60,1700,1701,1686,1704,1698],{},[122,1702,1703],{},"0001_01_01_000002_create_failed_jobs_table",[122,1705,1706],{},"failed_jobs",[10,1708,1709,1710,1713,1714,146],{},"The good news is that we can run migrations automatically before each test. This keeps the database in a clean state. Add the ",[122,1711,1712],{},"RefreshDatabase"," trait to ",[122,1715,1716],{},"Pest.php",[114,1718,1721],{"className":149,"code":1719,"filename":1720,"language":151,"meta":120,"style":120},"\u003C?php\n\ndeclare(strict_types=1);\n\npest()->extend(Tests\\TestCase::class)\n    ->use(Illuminate\\Foundation\\Testing\\RefreshDatabase::class)\n    ->in('Http');\n","tests\u002FPest.php",[122,1722,1723,1727,1731,1735,1739,1744,1749],{"__ignoreMap":120},[125,1724,1725],{"class":127,"line":128},[125,1726,542],{},[125,1728,1729],{"class":127,"line":161},[125,1730,548],{"emptyLinePlaceholder":547},[125,1732,1733],{"class":127,"line":167},[125,1734,553],{},[125,1736,1737],{"class":127,"line":173},[125,1738,548],{"emptyLinePlaceholder":547},[125,1740,1741],{"class":127,"line":179},[125,1742,1743],{},"pest()->extend(Tests\\TestCase::class)\n",[125,1745,1746],{"class":127,"line":185},[125,1747,1748],{},"    ->use(Illuminate\\Foundation\\Testing\\RefreshDatabase::class)\n",[125,1750,1751],{"class":127,"line":191},[125,1752,1753],{},"    ->in('Http');\n",[57,1755,1756],{"start":179},[60,1757,1758],{},"The user is still not authenticated. Check the log again:",[114,1760,1762],{"className":1655,"code":1761,"filename":1652,"language":1657,"meta":120,"style":120},"testing.ERROR: SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: users.password (Connection: sqlite, SQL: insert into \"users\" (\"name\", \"email\", \"updated_at\", \"created_at\") values (Test User, test@example.com, 2025-02-28 06:59:57, 2025-02-28 06:59:57))\n",[122,1763,1764],{"__ignoreMap":120},[125,1765,1766],{"class":127,"line":128},[125,1767,1761],{},[10,1769,1770,1771,1774],{},"This SQL error tells us that ",[122,1772,1773],{},"password"," is not nullable, but GitHub authentication does not provide a password. We need to adjust the migration for OAuth authentication.",[114,1776,1779],{"className":149,"code":1777,"filename":1778,"language":151,"meta":120,"style":120},"\u003C?php\n\ndeclare(strict_types=1);\n\nuse Illuminate\\Database\\Migrations\\Migration;\nuse Illuminate\\Database\\Schema\\Blueprint;\nuse Illuminate\\Support\\Facades\\Schema;\n\nreturn new class extends Migration\n{\n    \u002F**\n     * Run the migrations.\n     *\u002F\n    public function up(): void\n    {\n        Schema::create('users', function (Blueprint $table): void {\n            $table->id();\n            $table->string('name')->nullable(); \u002F\u002F [!code ++]\n            $table->string('username')->unique(); \u002F\u002F [!code ++]\n            $table->string('email')->unique();\n            $table->timestamp('email_verified_at')->nullable();\n            $table->string('password'); \u002F\u002F [!code --]\n            $table->rememberToken(); \u002F\u002F [!code --]\n            $table->string('github_id')->nullable()->unique(); \u002F\u002F [!code ++]\n            $table->string('avatar')->nullable(); \u002F\u002F [!code ++]\n            $table->timestamps();\n        });\n\n        \u002F\u002F other migrations...\n    }\n};\n\n","database\u002Fmigrations\u002F0001_01_01_000000_create_users_table.php",[122,1780,1781,1785,1789,1793,1797,1802,1807,1812,1816,1821,1825,1830,1835,1840,1845,1849,1854,1859,1864,1869,1874,1879,1884,1889,1894,1899,1904,1909,1913,1918,1922],{"__ignoreMap":120},[125,1782,1783],{"class":127,"line":128},[125,1784,542],{},[125,1786,1787],{"class":127,"line":161},[125,1788,548],{"emptyLinePlaceholder":547},[125,1790,1791],{"class":127,"line":167},[125,1792,553],{},[125,1794,1795],{"class":127,"line":173},[125,1796,548],{"emptyLinePlaceholder":547},[125,1798,1799],{"class":127,"line":179},[125,1800,1801],{},"use Illuminate\\Database\\Migrations\\Migration;\n",[125,1803,1804],{"class":127,"line":185},[125,1805,1806],{},"use Illuminate\\Database\\Schema\\Blueprint;\n",[125,1808,1809],{"class":127,"line":191},[125,1810,1811],{},"use Illuminate\\Support\\Facades\\Schema;\n",[125,1813,1814],{"class":127,"line":197},[125,1815,548],{"emptyLinePlaceholder":547},[125,1817,1818],{"class":127,"line":579},[125,1819,1820],{},"return new class extends Migration\n",[125,1822,1823],{"class":127,"line":584},[125,1824,983],{},[125,1826,1827],{"class":127,"line":590},[125,1828,1829],{},"    \u002F**\n",[125,1831,1832],{"class":127,"line":595},[125,1833,1834],{},"     * Run the migrations.\n",[125,1836,1837],{"class":127,"line":601},[125,1838,1839],{},"     *\u002F\n",[125,1841,1842],{"class":127,"line":1194},[125,1843,1844],{},"    public function up(): void\n",[125,1846,1847],{"class":127,"line":1199},[125,1848,993],{},[125,1850,1851],{"class":127,"line":1205},[125,1852,1853],{},"        Schema::create('users', function (Blueprint $table): void {\n",[125,1855,1856],{"class":127,"line":1211},[125,1857,1858],{},"            $table->id();\n",[125,1860,1861],{"class":127,"line":1217},[125,1862,1863],{},"            $table->string('name')->nullable(); \u002F\u002F [!code ++]\n",[125,1865,1866],{"class":127,"line":1223},[125,1867,1868],{},"            $table->string('username')->unique(); \u002F\u002F [!code ++]\n",[125,1870,1871],{"class":127,"line":1229},[125,1872,1873],{},"            $table->string('email')->unique();\n",[125,1875,1876],{"class":127,"line":1234},[125,1877,1878],{},"            $table->timestamp('email_verified_at')->nullable();\n",[125,1880,1881],{"class":127,"line":1239},[125,1882,1883],{},"            $table->string('password'); \u002F\u002F [!code --]\n",[125,1885,1886],{"class":127,"line":1245},[125,1887,1888],{},"            $table->rememberToken(); \u002F\u002F [!code --]\n",[125,1890,1891],{"class":127,"line":1251},[125,1892,1893],{},"            $table->string('github_id')->nullable()->unique(); \u002F\u002F [!code ++]\n",[125,1895,1896],{"class":127,"line":1257},[125,1897,1898],{},"            $table->string('avatar')->nullable(); \u002F\u002F [!code ++]\n",[125,1900,1901],{"class":127,"line":1263},[125,1902,1903],{},"            $table->timestamps();\n",[125,1905,1906],{"class":127,"line":1269},[125,1907,1908],{},"        });\n",[125,1910,1911],{"class":127,"line":1275},[125,1912,548],{"emptyLinePlaceholder":547},[125,1914,1915],{"class":127,"line":1280},[125,1916,1917],{},"        \u002F\u002F other migrations...\n",[125,1919,1920],{"class":127,"line":1286},[125,1921,1003],{},[125,1923,1924],{"class":127,"line":1292},[125,1925,1926],{},"};\n",[10,1928,1929],{},"In this migration, we add three columns:",[1931,1932,1933,1939,1945],"ul",{},[60,1934,1935,1938],{},[122,1936,1937],{},"username",": Stores the user's GitHub username.",[60,1940,1941,1944],{},[122,1942,1943],{},"github_id",": Stores the user's GitHub ID. It is unique so that two application users cannot share the same GitHub account, and nullable so that other authentication methods can be added later. Making a column nullable after the fact is more complicated in SQLite, so we add that constraint now.",[60,1946,1947,1950],{},[122,1948,1949],{},"avatar",": Stores the user's avatar URL.",[85,1952,1953],{"color":87,"icon":88},[10,1954,1955],{},"We directly edit the migration file instead of creating a new one because we haven't deployed the application yet. If we had, we would have created a new migration to alter the table.",[57,1957,1958],{"start":185},[60,1959,1960],{},"The user is still not authenticated. Debugging authentication in a fresh application can feel repetitive, but each error reveals another missing piece. Once you understand the process, it becomes much easier.",[10,1962,1963,1964,1967],{},"In our ",[122,1965,1966],{},"laravel.log"," file, we can see:",[114,1969,1971],{"className":1655,"code":1970,"filename":1652,"language":1657,"meta":120,"style":120},"[2025-02-28 07:09:29] testing.ERROR: SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: users.username (Connection: sqlite, SQL: insert into \"users\" (\"name\", \"email\", \"updated_at\", \"created_at\") values (Test User, test@example.com, 2025-02-28 07:09:29, 2025-02-28 07:09:29))\n",[122,1972,1973],{"__ignoreMap":120},[125,1974,1975],{"class":127,"line":128},[125,1976,1970],{},[10,1978,1979,1980,1982,1983,1986],{},"This SQL error is similar to the previous one, but this time ",[122,1981,1937],{}," is missing. That is surprising because both the migration and ",[122,1984,1985],{},"User::create"," contain the field.",[10,1988,1989,1990,1992,1993,1995,1996,1098,1998,2001],{},"The problem is Laravel's mass-assignment protection. By default, we must explicitly allow ",[122,1991,1937],{},", ",[122,1994,1943],{},", and ",[122,1997,1949],{},[122,1999,2000],{},"User"," model:",[114,2003,2007],{"className":2004,"code":2005,"filename":2006,"language":151,"meta":120,"style":120},"language-php shiki shiki-themes github-light github-light github-dark has-diff","\u003C?php\n\ndeclare(strict_types=1);\n\nnamespace App\\Models;\n\n\u002F\u002F use Illuminate\\Contracts\\Auth\\MustVerifyEmail;\nuse Illuminate\\Database\\Eloquent\\Factories\\HasFactory;\nuse Illuminate\\Foundation\\Auth\\User as Authenticatable;\nuse Illuminate\\Notifications\\Notifiable;\n\nclass User extends Authenticatable\n{\n    \u002F** @use HasFactory\u003C\\Database\\Factories\\UserFactory> *\u002F\n    use HasFactory, Notifiable;\n\n    \u002F**\n     * The attributes that are mass assignable.\n     *\n     * @var list\u003Cstring>\n     *\u002F\n    protected $fillable = [\n        'name',\n        'email',\n        'username', \u002F\u002F [!code ++]\n        'github_id', \u002F\u002F [!code ++]\n        'avatar', \u002F\u002F [!code ++]\n        'password', \u002F\u002F [!code --]\n    ];\n\n    \u002F\u002F ...\n}\n","app\u002FModels\u002FUser.php",[122,2008,2009,2013,2017,2021,2025,2030,2034,2039,2044,2049,2054,2058,2063,2067,2072,2077,2081,2085,2090,2095,2100,2104,2109,2114,2119,2127,2133,2139,2146,2151,2155,2160],{"__ignoreMap":120},[125,2010,2011],{"class":127,"line":128},[125,2012,542],{},[125,2014,2015],{"class":127,"line":161},[125,2016,548],{"emptyLinePlaceholder":547},[125,2018,2019],{"class":127,"line":167},[125,2020,553],{},[125,2022,2023],{"class":127,"line":173},[125,2024,548],{"emptyLinePlaceholder":547},[125,2026,2027],{"class":127,"line":179},[125,2028,2029],{},"namespace App\\Models;\n",[125,2031,2032],{"class":127,"line":185},[125,2033,548],{"emptyLinePlaceholder":547},[125,2035,2036],{"class":127,"line":191},[125,2037,2038],{},"\u002F\u002F use Illuminate\\Contracts\\Auth\\MustVerifyEmail;\n",[125,2040,2041],{"class":127,"line":197},[125,2042,2043],{},"use Illuminate\\Database\\Eloquent\\Factories\\HasFactory;\n",[125,2045,2046],{"class":127,"line":579},[125,2047,2048],{},"use Illuminate\\Foundation\\Auth\\User as Authenticatable;\n",[125,2050,2051],{"class":127,"line":584},[125,2052,2053],{},"use Illuminate\\Notifications\\Notifiable;\n",[125,2055,2056],{"class":127,"line":590},[125,2057,548],{"emptyLinePlaceholder":547},[125,2059,2060],{"class":127,"line":595},[125,2061,2062],{},"class User extends Authenticatable\n",[125,2064,2065],{"class":127,"line":601},[125,2066,983],{},[125,2068,2069],{"class":127,"line":1194},[125,2070,2071],{},"    \u002F** @use HasFactory\u003C\\Database\\Factories\\UserFactory> *\u002F\n",[125,2073,2074],{"class":127,"line":1199},[125,2075,2076],{},"    use HasFactory, Notifiable;\n",[125,2078,2079],{"class":127,"line":1205},[125,2080,548],{"emptyLinePlaceholder":547},[125,2082,2083],{"class":127,"line":1211},[125,2084,1829],{},[125,2086,2087],{"class":127,"line":1217},[125,2088,2089],{},"     * The attributes that are mass assignable.\n",[125,2091,2092],{"class":127,"line":1223},[125,2093,2094],{},"     *\n",[125,2096,2097],{"class":127,"line":1229},[125,2098,2099],{},"     * @var list\u003Cstring>\n",[125,2101,2102],{"class":127,"line":1234},[125,2103,1839],{},[125,2105,2106],{"class":127,"line":1239},[125,2107,2108],{},"    protected $fillable = [\n",[125,2110,2111],{"class":127,"line":1245},[125,2112,2113],{},"        'name',\n",[125,2115,2116],{"class":127,"line":1251},[125,2117,2118],{},"        'email',\n",[125,2120,2124],{"class":2121,"line":1257},[127,2122,2123],"diff","add",[125,2125,2126],{},"        'username',",[125,2128,2130],{"class":2129,"line":1263},[127,2122,2123],[125,2131,2132],{},"        'github_id',",[125,2134,2136],{"class":2135,"line":1269},[127,2122,2123],[125,2137,2138],{},"        'avatar',",[125,2140,2143],{"class":2141,"line":1275},[127,2122,2142],"remove",[125,2144,2145],{},"        'password',",[125,2147,2148],{"class":127,"line":1280},[125,2149,2150],{},"    ];\n",[125,2152,2153],{"class":127,"line":1286},[125,2154,548],{"emptyLinePlaceholder":547},[125,2156,2157],{"class":127,"line":1292},[125,2158,2159],{},"    \u002F\u002F ...\n",[125,2161,2162],{"class":127,"line":1298},[125,2163,1008],{},[10,2165,2166,2167,2170,2171,2174],{},"This approach works, but it means remembering to update ",[122,2168,2169],{},"fillable"," every time the model gains an assignable column. Mass-assignment protection is especially important when request data is passed directly to a model. For example, a user must never be able to set an ",[122,2172,2173],{},"is_admin"," field while registering.",[114,2176,2179],{"className":149,"code":2177,"filename":2178,"language":151,"meta":120,"style":120},"function (Request $request): void {\n    User::create($request->all()); \u002F\u002F Never write this code.\n}\n","Example",[122,2180,2181,2186,2191],{"__ignoreMap":120},[125,2182,2183],{"class":127,"line":128},[125,2184,2185],{},"function (Request $request): void {\n",[125,2187,2188],{"class":127,"line":161},[125,2189,2190],{},"    User::create($request->all()); \u002F\u002F Never write this code.\n",[125,2192,2193],{"class":127,"line":167},[125,2194,1008],{},[10,2196,2197,2198,2201],{},"Without this protection, a user could become an administrator simply by sending ",[122,2199,2200],{},"is_admin=true"," in the request. That would be a serious security issue.",[10,2203,2204,2205,2208,2209,146],{},"To remove the repetitive model configuration while keeping the input safe, we can disable guarded mode with ",[122,2206,2207],{},"Model::unguard()"," in ",[122,2210,2211],{},"app\u002FProviders\u002FAppServiceProvider.php",[114,2213,2215],{"className":149,"code":2214,"filename":2211,"language":151,"meta":120,"style":120},"\u003C?php\n\ndeclare(strict_types=1);\n\nnamespace App\\Providers;\n\nuse Illuminate\\Database\\Eloquent\\Model;\nuse Illuminate\\Support\\ServiceProvider;\n\nclass AppServiceProvider extends ServiceProvider\n{\n    public function boot(): void\n    {\n        Model::unguard();\n    }\n}\n",[122,2216,2217,2221,2225,2229,2233,2238,2242,2247,2252,2256,2261,2265,2270,2274,2279,2283],{"__ignoreMap":120},[125,2218,2219],{"class":127,"line":128},[125,2220,542],{},[125,2222,2223],{"class":127,"line":161},[125,2224,548],{"emptyLinePlaceholder":547},[125,2226,2227],{"class":127,"line":167},[125,2228,553],{},[125,2230,2231],{"class":127,"line":173},[125,2232,548],{"emptyLinePlaceholder":547},[125,2234,2235],{"class":127,"line":179},[125,2236,2237],{},"namespace App\\Providers;\n",[125,2239,2240],{"class":127,"line":185},[125,2241,548],{"emptyLinePlaceholder":547},[125,2243,2244],{"class":127,"line":191},[125,2245,2246],{},"use Illuminate\\Database\\Eloquent\\Model;\n",[125,2248,2249],{"class":127,"line":197},[125,2250,2251],{},"use Illuminate\\Support\\ServiceProvider;\n",[125,2253,2254],{"class":127,"line":579},[125,2255,548],{"emptyLinePlaceholder":547},[125,2257,2258],{"class":127,"line":584},[125,2259,2260],{},"class AppServiceProvider extends ServiceProvider\n",[125,2262,2263],{"class":127,"line":590},[125,2264,983],{},[125,2266,2267],{"class":127,"line":595},[125,2268,2269],{},"    public function boot(): void\n",[125,2271,2272],{"class":127,"line":601},[125,2273,993],{},[125,2275,2276],{"class":127,"line":1194},[125,2277,2278],{},"        Model::unguard();\n",[125,2280,2281],{"class":127,"line":1199},[125,2282,1003],{},[125,2284,2285],{"class":127,"line":1205},[125,2286,1008],{},[10,2288,2289,2290,2292,2293,2295],{},"From now on, all models will be unguarded, so we can remove ",[122,2291,2169],{}," from ",[122,2294,2000],{},". However, every user-controlled input must go through a form request. Validating the input before saving it ensures that users cannot set arbitrary fields or unexpected values.",[10,2297,2298],{},"In this OAuth flow, the data comes from a provider we trust, so we do not need a form request for it. We will use form requests later when we implement the comment system.",[10,2300,2301],{},"And voilà! The test passes, and users can now create an account through GitHub. 🥳",[10,2303,2304],{},"We are not finished yet. If a user logs in again, the application will try to create a duplicate record. We need to handle existing users.",[10,2306,2307],{},"First, let's write a test to ensure that a user can log in again:",[114,2309,2311],{"className":149,"code":2310,"filename":1132,"language":151,"meta":120,"style":120},"\u003C?php\n\nuse App\\Models\\User;\n\n\u002F\u002F other tests...\n\ntest('updates the user if they already exist', function () {\n    $fakeGitHubUser = fakeGitHubUser();\n\n    $user = User::factory()->create(['github_id' => $fakeGitHubUser->getId()]);\n\n    $this->get(route('auth.github.callback'));\n\n    $this->assertAuthenticated();\n\n    $this->assertDatabaseHas('users', [\n        'id' => $user->id,\n        'github_id' => $fakeGitHubUser->getId(),\n        'email' => $fakeGitHubUser->getEmail(),\n        'name' => $fakeGitHubUser->getName(),\n    ]);\n});\n\n",[122,2312,2313,2317,2321,2325,2329,2334,2338,2343,2347,2351,2356,2360,2364,2368,2372,2376,2380,2385,2389,2393,2397,2401],{"__ignoreMap":120},[125,2314,2315],{"class":127,"line":128},[125,2316,542],{},[125,2318,2319],{"class":127,"line":161},[125,2320,548],{"emptyLinePlaceholder":547},[125,2322,2323],{"class":127,"line":167},[125,2324,1556],{},[125,2326,2327],{"class":127,"line":173},[125,2328,548],{"emptyLinePlaceholder":547},[125,2330,2331],{"class":127,"line":179},[125,2332,2333],{},"\u002F\u002F other tests...\n",[125,2335,2336],{"class":127,"line":185},[125,2337,548],{"emptyLinePlaceholder":547},[125,2339,2340],{"class":127,"line":191},[125,2341,2342],{},"test('updates the user if they already exist', function () {\n",[125,2344,2345],{"class":127,"line":197},[125,2346,1173],{},[125,2348,2349],{"class":127,"line":579},[125,2350,548],{"emptyLinePlaceholder":547},[125,2352,2353],{"class":127,"line":584},[125,2354,2355],{},"    $user = User::factory()->create(['github_id' => $fakeGitHubUser->getId()]);\n",[125,2357,2358],{"class":127,"line":590},[125,2359,548],{"emptyLinePlaceholder":547},[125,2361,2362],{"class":127,"line":595},[125,2363,1182],{},[125,2365,2366],{"class":127,"line":601},[125,2367,548],{"emptyLinePlaceholder":547},[125,2369,2370],{"class":127,"line":1194},[125,2371,1191],{},[125,2373,2374],{"class":127,"line":1199},[125,2375,548],{"emptyLinePlaceholder":547},[125,2377,2378],{"class":127,"line":1205},[125,2379,1202],{},[125,2381,2382],{"class":127,"line":1211},[125,2383,2384],{},"        'id' => $user->id,\n",[125,2386,2387],{"class":127,"line":1217},[125,2388,1208],{},[125,2390,2391],{"class":127,"line":1223},[125,2392,1214],{},[125,2394,2395],{"class":127,"line":1229},[125,2396,1220],{},[125,2398,2399],{"class":127,"line":1234},[125,2400,1226],{},[125,2402,2403],{"class":127,"line":1239},[125,2404,604],{},[10,2406,2407,2408,2410],{},"This test creates a user with the same ",[122,2409,1943],{}," as the fake GitHub user. It then calls the callback route and asserts that the existing user is authenticated and updated.",[10,2412,2413],{},"Let's follow the error message:",[114,2415,2417],{"className":116,"code":2416,"filename":118,"language":119,"meta":120,"style":120},"SQLSTATE[HY000]: General error: 1 table users has no column named password (Connection: sqlite, SQL: insert into \"users\" (\"name\", \"email\", \"email_verified_at\", \"password\", \"remember_token\", \"github_id\", \"updated_at\", \"created_at\") values (Lawrence Herzog, roconnell@example.net, 2025-03-01 18:44:12, $2y$04$NXCZUWIcdKPiCdw3n5cTBOIpQo9\u002FDPc9rmgX5VRTQ3iTeWduC4x36, 0i9tgsiLpA, 12345, 2025-03-01 18:44:12, 2025-03-01 18:44:12))\n",[122,2418,2419],{"__ignoreMap":120},[125,2420,2421,2424,2427,2430,2432,2435,2438,2441,2444,2447,2450,2453,2456,2459,2462,2465,2468,2471,2474,2477,2479,2482,2485,2488,2491,2494,2497,2500,2503,2506,2509,2512,2515,2518,2521,2524,2527,2530,2532,2534,2536,2539],{"class":127,"line":128},[125,2422,2423],{"class":131},"SQLSTATE[HY000]:",[125,2425,2426],{"class":135}," General",[125,2428,2429],{"class":135}," error:",[125,2431,924],{"class":682},[125,2433,2434],{"class":135}," table",[125,2436,2437],{"class":135}," users",[125,2439,2440],{"class":135}," has",[125,2442,2443],{"class":135}," no",[125,2445,2446],{"class":135}," column",[125,2448,2449],{"class":135}," named",[125,2451,2452],{"class":135}," password",[125,2454,2455],{"class":719}," (Connection: ",[125,2457,2458],{"class":135},"sqlite,",[125,2460,2461],{"class":135}," SQL:",[125,2463,2464],{"class":135}," insert",[125,2466,2467],{"class":135}," into",[125,2469,2470],{"class":135}," \"users\"",[125,2472,2473],{"class":719}," (",[125,2475,2476],{"class":135},"\"name\"",[125,2478,1992],{"class":719},[125,2480,2481],{"class":135},"\"email\",",[125,2483,2484],{"class":135}," \"email_verified_at\",",[125,2486,2487],{"class":135}," \"password\",",[125,2489,2490],{"class":135}," \"remember_token\",",[125,2492,2493],{"class":135}," \"github_id\",",[125,2495,2496],{"class":135}," \"updated_at\",",[125,2498,2499],{"class":135}," \"created_at\"",[125,2501,2502],{"class":719},") values (",[125,2504,2505],{"class":131},"Lawrence",[125,2507,2508],{"class":135}," Herzog,",[125,2510,2511],{"class":135}," roconnell@example.net,",[125,2513,2514],{"class":135}," 2025-03-01",[125,2516,2517],{"class":135}," 18:44:12,",[125,2519,2520],{"class":719}," $2y$04$NXCZUWIcdKPiCdw3n5cTBOIpQo9",[125,2522,2523],{"class":135},"\u002FDPc9rmgX5VRTQ3iTeWduC4x36,",[125,2525,2526],{"class":135}," 0i9tgsiLpA,",[125,2528,2529],{"class":135}," 12345,",[125,2531,2514],{"class":135},[125,2533,2517],{"class":135},[125,2535,2514],{"class":135},[125,2537,2538],{"class":135}," 18:44:12",[125,2540,2541],{"class":719},"))\n",[10,2543,2544,2545,2547,2548,2551],{},"This SQL error mentions the ",[122,2546,1773],{}," column. That is strange because we removed it from the migration. The error comes from the user factory, which still contains Laravel's default password fields. Open ",[122,2549,2550],{},"database\u002Ffactories\u002FUserFactory.php"," and remove them:",[114,2553,2555],{"className":2004,"code":2554,"filename":2550,"language":151,"meta":120,"style":120},"\u003C?php\n\ndeclare(strict_types=1);\n\nnamespace Database\\Factories;\n\nuse Illuminate\\Database\\Eloquent\\Factories\\Factory;\nuse Illuminate\\Support\\Facades\\Hash; \u002F\u002F [!code --]\nuse Illuminate\\Support\\Str; \u002F\u002F [!code --]\n\n\u002F**\n * @extends \\Illuminate\\Database\\Eloquent\\Factories\\Factory\u003C\\App\\Models\\User>\n *\u002F\nclass UserFactory extends Factory\n{\n    \u002F** \u002F\u002F [!code --]\n     * The current password being used by the factory. \u002F\u002F [!code --]\n     *\u002F \u002F\u002F [!code --]\n    protected static ?string $password = null; \u002F\u002F [!code --]\n\n    \u002F**\n     * Define the model's default state.\n     *\n     * @return array\u003Cstring, mixed>\n     *\u002F\n    public function definition(): array\n    {\n        return [\n            'name' => fake()->name(),\n            'username' => fake()->unique()->userName(), \u002F\u002F [!code ++]\n            'email' => fake()->unique()->safeEmail(),\n            'github_id' => fake()->unique()->numberBetween(1, 1000000), \u002F\u002F [!code ++]\n            'avatar' => fake()->imageUrl(), \u002F\u002F [!code ++]\n            'email_verified_at' => now(),\n            'password' => self::$password ??= Hash::make('password'), \u002F\u002F [!code --]\n            'remember_token' => Str::random(10), \u002F\u002F [!code --]\n        ];\n    }\n \u002F\u002F [!code --]\n    \u002F** \u002F\u002F [!code --]\n     * Indicate that the model's email address should be unverified. \u002F\u002F [!code --]\n     *\u002F \u002F\u002F [!code --]\n    public function unverified(): static \u002F\u002F [!code --]\n    { \u002F\u002F [!code --]\n        return $this->state(fn (array $attributes): array => [ \u002F\u002F [!code --]\n            'email_verified_at' => null, \u002F\u002F [!code --]\n        ]);  \u002F\u002F [!code --]\n    } \u002F\u002F [!code --]\n}\n",[122,2556,2557,2561,2565,2569,2573,2578,2582,2587,2592,2597,2601,2605,2610,2614,2619,2623,2628,2634,2640,2645,2649,2653,2658,2662,2667,2671,2676,2680,2685,2690,2696,2701,2707,2713,2718,2724,2730,2735,2739,2744,2751,2757,2763,2769,2775,2782,2788,2794],{"__ignoreMap":120},[125,2558,2559],{"class":127,"line":128},[125,2560,542],{},[125,2562,2563],{"class":127,"line":161},[125,2564,548],{"emptyLinePlaceholder":547},[125,2566,2567],{"class":127,"line":167},[125,2568,553],{},[125,2570,2571],{"class":127,"line":173},[125,2572,548],{"emptyLinePlaceholder":547},[125,2574,2575],{"class":127,"line":179},[125,2576,2577],{},"namespace Database\\Factories;\n",[125,2579,2580],{"class":127,"line":185},[125,2581,548],{"emptyLinePlaceholder":547},[125,2583,2584],{"class":127,"line":191},[125,2585,2586],{},"use Illuminate\\Database\\Eloquent\\Factories\\Factory;\n",[125,2588,2589],{"class":127,"line":197},[125,2590,2591],{},"use Illuminate\\Support\\Facades\\Hash; \u002F\u002F [!code --]\n",[125,2593,2594],{"class":127,"line":579},[125,2595,2596],{},"use Illuminate\\Support\\Str; \u002F\u002F [!code --]\n",[125,2598,2599],{"class":127,"line":584},[125,2600,548],{"emptyLinePlaceholder":547},[125,2602,2603],{"class":127,"line":590},[125,2604,1242],{},[125,2606,2607],{"class":127,"line":595},[125,2608,2609],{}," * @extends \\Illuminate\\Database\\Eloquent\\Factories\\Factory\u003C\\App\\Models\\User>\n",[125,2611,2612],{"class":127,"line":601},[125,2613,1266],{},[125,2615,2616],{"class":127,"line":1194},[125,2617,2618],{},"class UserFactory extends Factory\n",[125,2620,2621],{"class":127,"line":1199},[125,2622,983],{},[125,2624,2625],{"class":127,"line":1205},[125,2626,2627],{},"    \u002F** \u002F\u002F [!code --]\n",[125,2629,2631],{"class":2630,"line":1211},[127,2122,2142],[125,2632,2633],{},"     * The current password being used by the factory.",[125,2635,2637],{"class":2636,"line":1217},[127,2122,2142],[125,2638,2639],{},"     *\u002F",[125,2641,2642],{"class":127,"line":1223},[125,2643,2644],{},"    protected static ?string $password = null; \u002F\u002F [!code --]\n",[125,2646,2647],{"class":127,"line":1229},[125,2648,548],{"emptyLinePlaceholder":547},[125,2650,2651],{"class":127,"line":1234},[125,2652,1829],{},[125,2654,2655],{"class":127,"line":1239},[125,2656,2657],{},"     * Define the model's default state.\n",[125,2659,2660],{"class":127,"line":1245},[125,2661,2094],{},[125,2663,2664],{"class":127,"line":1251},[125,2665,2666],{},"     * @return array\u003Cstring, mixed>\n",[125,2668,2669],{"class":127,"line":1257},[125,2670,1839],{},[125,2672,2673],{"class":127,"line":1263},[125,2674,2675],{},"    public function definition(): array\n",[125,2677,2678],{"class":127,"line":1269},[125,2679,993],{},[125,2681,2682],{"class":127,"line":1275},[125,2683,2684],{},"        return [\n",[125,2686,2687],{"class":127,"line":1280},[125,2688,2689],{},"            'name' => fake()->name(),\n",[125,2691,2693],{"class":2692,"line":1286},[127,2122,2123],[125,2694,2695],{},"            'username' => fake()->unique()->userName(),",[125,2697,2698],{"class":127,"line":1292},[125,2699,2700],{},"            'email' => fake()->unique()->safeEmail(),\n",[125,2702,2704],{"class":2703,"line":1298},[127,2122,2123],[125,2705,2706],{},"            'github_id' => fake()->unique()->numberBetween(1, 1000000),",[125,2708,2710],{"class":2709,"line":1304},[127,2122,2123],[125,2711,2712],{},"            'avatar' => fake()->imageUrl(),",[125,2714,2715],{"class":127,"line":1310},[125,2716,2717],{},"            'email_verified_at' => now(),\n",[125,2719,2721],{"class":2720,"line":1316},[127,2122,2142],[125,2722,2723],{},"            'password' => self::$password ??= Hash::make('password'),",[125,2725,2727],{"class":2726,"line":1322},[127,2122,2142],[125,2728,2729],{},"            'remember_token' => Str::random(10),",[125,2731,2732],{"class":127,"line":1327},[125,2733,2734],{},"        ];\n",[125,2736,2737],{"class":127,"line":1333},[125,2738,1003],{},[125,2740,2742],{"class":2741,"line":1344},[127,2122,2142],[125,2743,2627],{},[125,2745,2748],{"class":2746,"line":2747},[127,2122,2142],41,[125,2749,2750],{},"     * Indicate that the model's email address should be unverified.",[125,2752,2755],{"class":2753,"line":2754},[127,2122,2142],42,[125,2756,2639],{},[125,2758,2760],{"class":127,"line":2759},43,[125,2761,2762],{},"    public function unverified(): static \u002F\u002F [!code --]\n",[125,2764,2766],{"class":127,"line":2765},44,[125,2767,2768],{},"    { \u002F\u002F [!code --]\n",[125,2770,2772],{"class":127,"line":2771},45,[125,2773,2774],{},"        return $this->state(fn (array $attributes): array => [ \u002F\u002F [!code --]\n",[125,2776,2779],{"class":2777,"line":2778},[127,2122,2142],46,[125,2780,2781],{},"            'email_verified_at' => null,",[125,2783,2785],{"class":127,"line":2784},47,[125,2786,2787],{},"        ]);  \u002F\u002F [!code --]\n",[125,2789,2791],{"class":127,"line":2790},48,[125,2792,2793],{},"    } \u002F\u002F [!code --]\n",[125,2795,2797],{"class":127,"line":2796},49,[125,2798,1008],{},[10,2800,2801,2802,2805],{},"After this change, the error is ",[122,2803,2804],{},"The user is not authenticated",". We know what that means: there is another problem to fix.",[10,2807,2808,2809,2811],{},"By checking the ",[122,2810,1966],{}," file, we discover that the issue we wanted to find:",[114,2813,2815],{"className":1655,"code":2814,"filename":1652,"language":1657,"meta":120,"style":120},"[2025-03-01 18:51:14] testing.ERROR: SQLSTATE[23000]: Integrity constraint violation: 19 UNIQUE constraint failed: users.github_id (Connection: sqlite, SQL: insert into \"users\" (\"name\", \"email\", \"username\", \"github_id\", \"avatar\", \"updated_at\", \"created_at\") values (Test User, test@example.com, testuser, 12345, https:\u002F\u002Fexample.com\u002Favatar.jpg, 2025-03-01 18:51:14, 2025-03-01 18:51:14))\n",[122,2816,2817],{"__ignoreMap":120},[125,2818,2819],{"class":127,"line":128},[125,2820,2814],{},[10,2822,2823,2824,2826,2827,2834],{},"This SQL error means that a user with the same ",[122,2825,1943],{}," already exists. Instead of always creating a new user, we need to update the existing record when it is found and create one when it is not. Laravel's ",[17,2828,2831],{"href":2829,"rel":2830},"https:\u002F\u002Flaravel.com\u002Fdocs\u002F12.x\u002Feloquent#updates",[21],[122,2832,2833],{},"updateOrCreate"," method does exactly that.",[114,2836,2838],{"className":149,"code":2837,"filename":946,"language":151,"meta":120,"style":120},"\u003C?php\n\nclass RegisteredViaGitHubController extends Controller\n{\n    public function store()\n    {\n        $githubUser = Socialite::driver('github')->user();\n\n        $user = User::updateOrCreate(\n            [\n                'github_id' => $githubUser->getId(),\n            ],\n            [\n                'name' => $githubUser->getName(),\n                'email' => $githubUser->getEmail(),\n                'username' => $githubUser->getNickname(),\n                'avatar' => $githubUser->getAvatar(),\n            ]\n        );\n\n        Auth::login($user);\n    }\n}\n",[122,2839,2840,2844,2848,2852,2856,2860,2864,2868,2872,2877,2882,2887,2892,2896,2901,2906,2911,2916,2921,2926,2930,2934,2938],{"__ignoreMap":120},[125,2841,2842],{"class":127,"line":128},[125,2843,542],{},[125,2845,2846],{"class":127,"line":161},[125,2847,548],{"emptyLinePlaceholder":547},[125,2849,2850],{"class":127,"line":167},[125,2851,978],{},[125,2853,2854],{"class":127,"line":173},[125,2855,983],{},[125,2857,2858],{"class":127,"line":179},[125,2859,1456],{},[125,2861,2862],{"class":127,"line":185},[125,2863,993],{},[125,2865,2866],{"class":127,"line":191},[125,2867,1465],{},[125,2869,2870],{"class":127,"line":197},[125,2871,548],{"emptyLinePlaceholder":547},[125,2873,2874],{"class":127,"line":579},[125,2875,2876],{},"        $user = User::updateOrCreate(\n",[125,2878,2879],{"class":127,"line":584},[125,2880,2881],{},"            [\n",[125,2883,2884],{"class":127,"line":590},[125,2885,2886],{},"                'github_id' => $githubUser->getId(),\n",[125,2888,2889],{"class":127,"line":595},[125,2890,2891],{},"            ],\n",[125,2893,2894],{"class":127,"line":601},[125,2895,2881],{},[125,2897,2898],{"class":127,"line":1194},[125,2899,2900],{},"                'name' => $githubUser->getName(),\n",[125,2902,2903],{"class":127,"line":1199},[125,2904,2905],{},"                'email' => $githubUser->getEmail(),\n",[125,2907,2908],{"class":127,"line":1205},[125,2909,2910],{},"                'username' => $githubUser->getNickname(),\n",[125,2912,2913],{"class":127,"line":1211},[125,2914,2915],{},"                'avatar' => $githubUser->getAvatar(),\n",[125,2917,2918],{"class":127,"line":1217},[125,2919,2920],{},"            ]\n",[125,2922,2923],{"class":127,"line":1223},[125,2924,2925],{},"        );\n",[125,2927,2928],{"class":127,"line":1229},[125,2929,548],{"emptyLinePlaceholder":547},[125,2931,2932],{"class":127,"line":1234},[125,2933,1636],{},[125,2935,2936],{"class":127,"line":1239},[125,2937,1003],{},[125,2939,2940],{"class":127,"line":1245},[125,2941,1008],{},[10,2943,2944],{},"Finally, redirect the user to the frontend homepage after logging in.",[10,2946,2947],{},"The last test is to ensure that the user is redirected to the homepage after logging in:",[114,2949,2951],{"className":149,"code":2950,"filename":1132,"language":151,"meta":120,"style":120},"\u003C?php\n\n\u002F\u002F other tests...\n\ntest('redirects to the homepage', function () {\n    fakeGitHubUser();\n\n    $this->get(route('auth.github.callback'))\n        ->assertRedirect('\u002F');\n});\n",[122,2952,2953,2957,2961,2965,2969,2974,2979,2983,2988,2993],{"__ignoreMap":120},[125,2954,2955],{"class":127,"line":128},[125,2956,542],{},[125,2958,2959],{"class":127,"line":161},[125,2960,548],{"emptyLinePlaceholder":547},[125,2962,2963],{"class":127,"line":167},[125,2964,2333],{},[125,2966,2967],{"class":127,"line":173},[125,2968,548],{"emptyLinePlaceholder":547},[125,2970,2971],{"class":127,"line":179},[125,2972,2973],{},"test('redirects to the homepage', function () {\n",[125,2975,2976],{"class":127,"line":185},[125,2977,2978],{},"    fakeGitHubUser();\n",[125,2980,2981],{"class":127,"line":191},[125,2982,548],{"emptyLinePlaceholder":547},[125,2984,2985],{"class":127,"line":197},[125,2986,2987],{},"    $this->get(route('auth.github.callback'))\n",[125,2989,2990],{"class":127,"line":579},[125,2991,2992],{},"        ->assertRedirect('\u002F');\n",[125,2994,2995],{"class":127,"line":584},[125,2996,604],{},[10,2998,203,2999,3002],{},[122,3000,3001],{},"assertRedirect"," method verifies that the user is redirected to the homepage after logging in.",[10,3004,3005],{},"Then, the last step is to implement the redirection in our controller:",[114,3007,3009],{"className":149,"code":3008,"filename":946,"language":151,"meta":120,"style":120},"\u003C?php\nclass RegisteredViaGitHubController extends Controller\n{\n    public function store()\n    {\n        \u002F\u002F ...\n\n        Auth::login($user);\n\n        return redirect('\u002F');\n    }\n}\n",[122,3010,3011,3015,3019,3023,3027,3031,3036,3040,3044,3048,3053,3057],{"__ignoreMap":120},[125,3012,3013],{"class":127,"line":128},[125,3014,542],{},[125,3016,3017],{"class":127,"line":161},[125,3018,978],{},[125,3020,3021],{"class":127,"line":167},[125,3022,983],{},[125,3024,3025],{"class":127,"line":173},[125,3026,1456],{},[125,3028,3029],{"class":127,"line":179},[125,3030,993],{},[125,3032,3033],{"class":127,"line":185},[125,3034,3035],{},"        \u002F\u002F ...\n",[125,3037,3038],{"class":127,"line":191},[125,3039,548],{"emptyLinePlaceholder":547},[125,3041,3042],{"class":127,"line":197},[125,3043,1636],{},[125,3045,3046],{"class":127,"line":579},[125,3047,548],{"emptyLinePlaceholder":547},[125,3049,3050],{"class":127,"line":584},[125,3051,3052],{},"        return redirect('\u002F');\n",[125,3054,3055],{"class":127,"line":590},[125,3056,1003],{},[125,3058,3059],{"class":127,"line":595},[125,3060,1008],{},[10,3062,3063],{},"And now, the controller is complete!",[40,3065,3067],{"id":3066},"reaching-complete-coverage","Reaching complete coverage",[10,3069,3070],{},"Before finishing, make sure the test script runs correctly.",[85,3072,3073],{"color":795,"icon":796},[10,3074,3075],{},"Before running the test script, run the lint and refactor scripts first.",[10,3077,3078],{},"PHPStan is not happy with our code. It raises two errors:",[114,3080,3082],{"className":116,"code":3081,"filename":118,"language":119,"meta":120,"style":120}," ------ ---------------------------------------------------------------------------------------------------\n  Line   app\u002FHttp\u002FControllers\u002FRegisteredViaGitHubController.php\n ------ ---------------------------------------------------------------------------------------------------\n  :13    Method App\\Http\\Controllers\\RegisteredViaGitHubController::create() has no return type specified.\n         🪪  missingType.return\n  :18    Method App\\Http\\Controllers\\RegisteredViaGitHubController::store() has no return type specified.\n         🪪  missingType.return\n ------ ---------------------------------------------------------------------------------------------------\n",[122,3083,3084,3092,3100,3106,3148,3156,3190,3196],{"__ignoreMap":120},[125,3085,3086,3089],{"class":127,"line":128},[125,3087,3088],{"class":131}," ------",[125,3090,3091],{"class":682}," ---------------------------------------------------------------------------------------------------\n",[125,3093,3094,3097],{"class":127,"line":161},[125,3095,3096],{"class":131},"  Line",[125,3098,3099],{"class":135},"   app\u002FHttp\u002FControllers\u002FRegisteredViaGitHubController.php\n",[125,3101,3102,3104],{"class":127,"line":167},[125,3103,3088],{"class":131},[125,3105,3091],{"class":682},[125,3107,3108,3111,3114,3117,3119,3121,3123,3126,3128,3131,3134,3137,3139,3142,3145],{"class":127,"line":173},[125,3109,3110],{"class":131},"  :13",[125,3112,3113],{"class":135},"    Method",[125,3115,3116],{"class":135}," App",[125,3118,683],{"class":682},[125,3120,686],{"class":135},[125,3122,695],{"class":682},[125,3124,3125],{"class":135},"ontrollers",[125,3127,689],{"class":682},[125,3129,3130],{"class":135},"egisteredViaGitHubController::create",[125,3132,3133],{"class":719},"() ",[125,3135,3136],{"class":135},"has",[125,3138,2443],{"class":135},[125,3140,3141],{"class":135}," return",[125,3143,3144],{"class":135}," type",[125,3146,3147],{"class":135}," specified.\n",[125,3149,3150,3153],{"class":127,"line":179},[125,3151,3152],{"class":131},"         🪪",[125,3154,3155],{"class":135},"  missingType.return\n",[125,3157,3158,3161,3163,3165,3167,3169,3171,3173,3175,3178,3180,3182,3184,3186,3188],{"class":127,"line":185},[125,3159,3160],{"class":131},"  :18",[125,3162,3113],{"class":135},[125,3164,3116],{"class":135},[125,3166,683],{"class":682},[125,3168,686],{"class":135},[125,3170,695],{"class":682},[125,3172,3125],{"class":135},[125,3174,689],{"class":682},[125,3176,3177],{"class":135},"egisteredViaGitHubController::store",[125,3179,3133],{"class":719},[125,3181,3136],{"class":135},[125,3183,2443],{"class":135},[125,3185,3141],{"class":135},[125,3187,3144],{"class":135},[125,3189,3147],{"class":135},[125,3191,3192,3194],{"class":127,"line":191},[125,3193,3152],{"class":131},[125,3195,3155],{"class":135},[125,3197,3198,3200],{"class":127,"line":197},[125,3199,3088],{"class":131},[125,3201,3091],{"class":682},[10,3203,3204,3205,3208],{},"The methods are missing return types. Add ",[122,3206,3207],{},": RedirectResponse"," to both methods:",[114,3210,3212],{"className":149,"code":3211,"filename":946,"language":151,"meta":120,"style":120},"\u003C?php\n\nuse Symfony\\Component\\HttpFoundation\\RedirectResponse;\n\nclass RegisteredViaGitHubController extends Controller\n{\n    public function create(): RedirectResponse\n    {\n        \u002F\u002F ...\n    }\n\n    public function store(): RedirectResponse\n    {\n        \u002F\u002F ...\n    }\n}\n",[122,3213,3214,3218,3222,3227,3231,3235,3239,3244,3248,3252,3256,3260,3265,3269,3273,3277],{"__ignoreMap":120},[125,3215,3216],{"class":127,"line":128},[125,3217,542],{},[125,3219,3220],{"class":127,"line":161},[125,3221,548],{"emptyLinePlaceholder":547},[125,3223,3224],{"class":127,"line":167},[125,3225,3226],{},"use Symfony\\Component\\HttpFoundation\\RedirectResponse;\n",[125,3228,3229],{"class":127,"line":173},[125,3230,548],{"emptyLinePlaceholder":547},[125,3232,3233],{"class":127,"line":179},[125,3234,978],{},[125,3236,3237],{"class":127,"line":185},[125,3238,983],{},[125,3240,3241],{"class":127,"line":191},[125,3242,3243],{},"    public function create(): RedirectResponse\n",[125,3245,3246],{"class":127,"line":197},[125,3247,993],{},[125,3249,3250],{"class":127,"line":579},[125,3251,3035],{},[125,3253,3254],{"class":127,"line":584},[125,3255,1003],{},[125,3257,3258],{"class":127,"line":590},[125,3259,548],{"emptyLinePlaceholder":547},[125,3261,3262],{"class":127,"line":595},[125,3263,3264],{},"    public function store(): RedirectResponse\n",[125,3266,3267],{"class":127,"line":601},[125,3268,993],{},[125,3270,3271],{"class":127,"line":1194},[125,3272,3035],{},[125,3274,3275],{"class":127,"line":1199},[125,3276,1003],{},[125,3278,3279],{"class":127,"line":1205},[125,3280,1008],{},[10,3282,203,3283,3286],{},[122,3284,3285],{},"test:unit"," script still fails because this default route has no test:",[114,3288,3290],{"className":149,"code":3289,"filename":730,"language":151,"meta":120,"style":120},"Route::get('\u002F', function () {\n    return view('welcome');\n});\n",[122,3291,3292,3297,3302],{"__ignoreMap":120},[125,3293,3294],{"class":127,"line":128},[125,3295,3296],{},"Route::get('\u002F', function () {\n",[125,3298,3299],{"class":127,"line":161},[125,3300,3301],{},"    return view('welcome');\n",[125,3303,3304],{"class":127,"line":167},[125,3305,604],{},[10,3307,3308],{},"We can safely remove it because users should be redirected to the homepage of the frontend application, not the Laravel application.",[10,3310,3311],{},"Once removed, everything is green! 🟢",[10,3313,3314],{},[529,3315,3316],{},"I really really love this feeling.",[40,3318,3320],{"id":3319},"final-thoughts","Final thoughts",[10,3322,3323,3324,3326],{},"GitHub authentication is now in place. We have a local ",[122,3325,2000],{}," model, a complete OAuth callback flow, and tests that describe the behavior we expect. The test-first workflow may feel slower at first, but it gives us confidence as the implementation grows.",[10,3328,3329],{},"Socialite handles the provider-specific details so we can focus on our application's users and sessions. That gives the comment system a reliable owner for each comment.",[10,3331,3332],{},"Next, we will design and implement the Laravel API for listing, creating, editing, and deleting comments.",[3334,3335,3336],"style",{},"html pre.shiki code .snPdu, html code.shiki .snPdu{--shiki-light:#6F42C1;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sIIMD, html code.shiki .sIIMD{--shiki-light:#032F62;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sBjJW, html code.shiki .sBjJW{--shiki-light:#005CC5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .s8jYJ, html code.shiki .s8jYJ{--shiki-light:#D73A49;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sxrX7, html code.shiki .sxrX7{--shiki-light:#24292E;--shiki-default:#24292E;--shiki-dark:#E1E4E8}",{"title":120,"searchDepth":161,"depth":161,"links":3338},[3339,3340,3341,3345,3346],{"id":42,"depth":161,"text":43},{"id":108,"depth":161,"text":109},{"id":427,"depth":161,"text":428,"children":3342},[3343,3344],{"id":440,"depth":167,"text":441},{"id":1087,"depth":167,"text":1088},{"id":3066,"depth":161,"text":3067},{"id":3319,"depth":161,"text":3320},"3729f419-a8b6-486c-a845-ab5ba1a33172","2025-02-25","Add GitHub authentication to Laravel with Socialite, OAuth callbacks, database synchronization, secure sessions, and test-first development.","md",null,{},"\u002Fseries\u002Fempower-and-dynamize-our-vitepress-blog-with-a-laravel-api\u002Feffortless-social-authentication-with-laravel-socialite","---\ncontentId: 3729f419-a8b6-486c-a845-ab5ba1a33172\npart: 3\ntitle: Effortless Social Authentication with Laravel Socialite\ndescription: Add GitHub authentication to Laravel with Socialite, OAuth callbacks, database synchronization, secure sessions, and test-first development.\ndate: 2025-02-25\nseriesSlug: empower-and-dynamize-our-vitepress-blog-with-a-laravel-api\n---\n\nWith the project structure and quality tools in place, we can start building the API's first user-facing feature: authentication. Since comments should belong to real users, this is a natural next step.\n\nThe problem is that authentication is hard, really hard. You have to deal with passwords, email verification, password resets, two-factor authentication, and more. Fortunately, frameworks such as Laravel make this process easier with [built-in authentication](https:\u002F\u002Flaravel.com\u002Fdocs\u002F12.x\u002Fauthentication) features and first-party packages such as [Fortify](https:\u002F\u002Flaravel.com\u002Fdocs\u002F12.x\u002Ffortify).\n\nEven with these tools, we still have to build and manage many parts ourselves: registration, login, password resets, profile management, and the related edge cases. We also need a mail service to send emails.\n\nAll of this is feasible, but it takes time and effort. For a simple blog like ours, we can use GitHub for social authentication instead. This simplifies the implementation and lets users sign in with an account they already have.\n\nBefore we dive into the implementation, let's take a moment to understand what social authentication is and why it is useful.\n\n## Social authentication in a nutshell\n\nTraditionally, when a user wants to create an account on a website, they have to fill out a registration form with their email address and password. This process has worked well for years, but it has its drawbacks. On our side, we have to manage all the data, and on the user side, they have to remember yet another password.\n\nWith social authentication, users can log in using their existing accounts on platforms like GitHub, Google, Apple, or Facebook. This means they don't have to create a new account or remember another password. Instead, they can use their existing credentials to log in quickly and easily, which reduces friction and improves the user experience.\n\nFor the more technical among you, here's a schema of how it works:\n\nThe flow is straightforward:\n\n1. The user clicks on the \"Log in with GitHub\" button on your website.\n2. The user is redirected to GitHub's authorization page, where they can grant permission for your application to access their GitHub account.\n3. If the user grants permission, GitHub redirects them back to your website with an authorization code.\n4. Your website exchanges the authorization code for an access token, which can be used to access the user's GitHub account.\n5. Your website can now use the access token to fetch the user's profile information from GitHub.\n6. Your website creates a new user account, or updates an existing one, and logs in the user based on the information received from GitHub.\n7. Your website redirects the user to the appropriate page after login.\n8. The user is now logged in and can interact with your website.\n\n::callout{icon=\"i-ph-info-duotone\" color=\"info\"}\nYou have to note that the user is logged in via our application, not via GitHub. This is super important to avoid sending the access token to the client or to manage multiple providers, or a provider in addition to simple email\u002Fpassword authentication.\n::\n\nSimple, right? Laravel has a first-party package called [Socialite](https:\u002F\u002Flaravel.com\u002Fdocs\u002F12.x\u002Fsocialite) that makes this process even easier. It handles the provider-specific OAuth details so that our application can focus on its own users.\n\n::callout{icon=\"i-ph-info-duotone\" color=\"info\"}\nKeep in mind that social authentication is not a miracle solution for every project. Even if it can be appealing at first, it can also come with its own set of challenges.\n::\n\n## Adding Socialite to the project\n\nFirst, let's install the package:\n\n```bash [terminal]\ncomposer require laravel\u002Fsocialite\n```\n\nNext, configure it in `config\u002Fservices.php`:\n\n```php [config\u002Fservices.php]\nreturn [\n  \u002F\u002F ...\n  'github' => [\n      'client_id' => env('GITHUB_CLIENT_ID'),\n      'client_secret' => env('GITHUB_CLIENT_SECRET'),\n      'redirect' => env('GITHUB_REDIRECT'),\n  ],\n];\n```\n\nThe `env` function reads these values from `.env`. Add the GitHub credentials and callback URL:\n\n```ini [.env]\nGITHUB_CLIENT_ID=your-client-id\nGITHUB_CLIENT_SECRET=your-client-secret\nGITHUB_REDIRECT=http:\u002F\u002Flocalhost:8000\u002Fauth\u002Fgithub\u002Fcallback\n```\n\nThe user is logged in through our application, not through GitHub. This distinction matters because we do not send the provider's access token to the client and can support multiple authentication providers later.\n\nAt this point, two questions usually arise:\n\n1. How do we get the `GITHUB_CLIENT_ID` and `GITHUB_CLIENT_SECRET`?\n2. Why do we need a `GITHUB_REDIRECT` URL and how do we get it?\n\nGitHub provides `GITHUB_CLIENT_ID` and `GITHUB_CLIENT_SECRET` when you create an OAuth application. Open your [GitHub settings](https:\u002F\u002Fgithub.com\u002Fsettings\u002Fprofile), go to `Developer settings`, select `OAuth Apps`, and click `New OAuth App`.\n\n::callout{icon=\"i-ph-info-duotone\" color=\"info\"}\nAn OAuth application is an application that uses the OAuth protocol to authenticate users. In our case, we are using GitHub as the OAuth provider. Having an OAuth application allows us to use GitHub's authentication system to log in users on our website.\n::\n\nFill in the form. GitHub is quite permissive about the values you provide during local development.\n\n1. The `Application name` can be anything you want. I recommend using the name of your website because it is shown to users when they log in, at least the first time.\n2. The `Homepage URL` is the URL of your website. You can use `http:\u002F\u002Flocalhost:8000` for local development.\n3. The `Authorization callback URL` is where GitHub redirects users after they authorize the application. It must match the value in `.env`. For local development, use `http:\u002F\u002Flocalhost:8000\u002Fauth\u002Fgithub\u002Fcallback`.\n\nClick `Register application` to create the OAuth application. On its page, you will find the `Client ID`, which looks like this:\n\n```txt [Client ID]\na1b2c3d4e5f6g7h8i9j0\n```\n\nA new `Client secret` must be generated. Click on `Generate a new client secret` and copy the value. It looks like this:\n\n```txt [Client Secret]\nd13f84af84688a69a71bc8b3808f8836e4c23774\n```\n\nFinally, add these values to `.env`:\n\n```ini [.env]\nGITHUB_CLIENT_ID=a1b2c3d4e5f6g7h8i9j0\nGITHUB_CLIENT_SECRET=d13f84af84688a69a71bc8b3808f8836e4c23774\nGITHUB_REDIRECT=http:\u002F\u002Flocalhost:8000\u002Fauth\u002Fgithub\u002Fcallback\n```\n\nDon't forget to also add the environment variables in your `.env.example` file.\n\n```ini [.env.example]\nGITHUB_CLIENT_ID=\nGITHUB_CLIENT_SECRET=\nGITHUB_REDIRECT=http:\u002F\u002Flocalhost:8000\u002Fauth\u002Fgithub\u002Fcallback\n```\n\nDo not commit the client ID or secret. Leave those values empty in `.env.example`, but keep the redirect URL so that anyone cloning the repository knows how to configure a local OAuth application.\n\nNow that we have everything set up, let's write the code to handle the authentication.\n\n## Handling the authentication\n\nFor the rest of the project, we will use a test-first approach: write a test, follow its failure, and add only the code needed to make it pass. This helps us define the behavior before implementation and avoids surprises later. It can feel intimidating at first, but Laravel makes the workflow approachable with its built-in testing tools.\n\n::callout{icon=\"i-ph-info-duotone\" color=\"info\"}\nThis may be test-driven development (TDD), but the label is less important here than the workflow: write the test first, then implement only what is necessary to make it pass.\n::\n\n### The redirect\n\nLet's create a new controller called `RegisteredViaGitHubController`:\n\n```bash [terminal]\nphp artisan make:controller RegisteredViaGitHubController\n```\n\nThis controller will contain two methods:\n\n1. `create`: This method will handle the redirection to GitHub.\n2. `store`: This method will handle the callback from GitHub.\n\nI tend to use traditional method names for readability and consistency. Keeping these names in mind is useful because the tests will be organized around them.\n\nIn the folder `tests\u002FHttp`, we can safely remove the `ExampleTest.php` file, and then create a new folder called `RegisteredViaGitHub` and a new test file called `CreateTest.php`.\n\n```bash [terminal]\nmkdir tests\u002FHttp\u002FRegisteredViaGitHub\ntouch tests\u002FHttp\u002FRegisteredViaGitHub\u002FCreateTest.php\n```\n\nWithin this file, let's write our first test. _Really exciting!_\n\n```php [tests\u002FHttp\u002FRegisteredViaGitHub\u002FCreateTest.php]\n\u003C?php\n\ndeclare(strict_types=1);\n\nuse Laravel\\Socialite\\Facades\\Socialite;\n\ntest('redirects for authorization', function () {\n    Socialite::shouldReceive('driver->redirect')->once();\n\n    $this->get(route('auth.github.redirect'));\n\n    expect(true)->toBeTrue();\n});\n```\n\n::callout{icon=\"i-ph-info-duotone\" color=\"info\"}\n`expect(true)->toBeTrue()` gives the test an assertion without adding behavior that we need to verify. If `Socialite::shouldReceive('driver->redirect')->once()` is not satisfied, the mock will fail the test anyway.\n::\n\nThis is our first test, but not our simplest one. It mocks the `Socialite` facade and verifies that `redirect` is called once. We use a mock because the test should not send us to GitHub; it only needs to verify that our code asks Socialite to perform the redirect.\n\nLet's run the test:\n\n```bash [terminal]\nphp artisan test tests\u002FHttp\u002FRegisteredViaGitHub\u002FCreateTest.php\n```\n\nIt fails, as expected. In a test-first workflow, the failure tells us what to implement next. We follow the error message and add only the code needed to make the test pass.\n\n```bash [terminal]\n➜ php artisan test tests\u002FHttp\u002FRegisteredViaGitHub\u002FCreateTest.php\n\nFAILED  Tests\\Http\\RegisteredViaGitHub\\CreateTest > redirects for authorization\n  Route [auth.github.redirect] not defined.\n```\n\nThe error message is clear, we have to define the route `auth.github.redirect`. Let's do this in our `routes\u002Fweb.php` file:\n\n```php [routes\u002Fweb.php]\n\u003C?php\n\ndeclare(strict_types=1);\n\nuse App\\Http\\Controllers\\RegisteredViaGitHubController;\nuse Illuminate\\Support\\Facades\\Route;\n\n\u002F\u002F other routes...\n\nRoute::get('\u002Fauth\u002Fgithub\u002Fredirect', [RegisteredViaGitHubController::class, 'create'])\n    ->name('auth.github.redirect');\n```\n\nBut what is `web.php`? It is where we define browser-facing routes and map URLs to controller methods. Reading this file gives us a useful overview of the application's entry points.\n\n::callout{icon=\"i-ph-lightbulb-duotone\" color=\"success\"}\nI highly recommend you to watch [Lessons From the Framework](https:\u002F\u002Fyoutu.be\u002FNjkxzc-aqts?si=qA5qWuVrm02VtJUX) by [Luke Dowing](https:\u002F\u002Fgithub.com\u002Flukeraymonddowning) to understand how central this file is.\n::\n\nThis route responds to `\u002Fauth\u002Fgithub\u002Fredirect` and calls the `create` method on `RegisteredViaGitHubController`. We also give it the name `auth.github.redirect`, which lets us reference it from the test and elsewhere in the application.\n\nLet's run the test again:\n\n```bash [terminal]\n➜ php artisan test tests\u002FHttp\u002FRegisteredViaGitHub\u002FCreateTest.php\n\n FAILED  Tests\\Http\\RegisteredViaGitHub\\CreateTest > redirects for authorization\n  Method redirect(\u003CAny Arguments>) from Mockery_1__demeter_c357969a11a830028afbe426ab0eea17_driver should be called\n exactly 1 times but called 0 times.\n```\n\nThe new error message means that the route works, but the controller is not calling Socialite yet. Add that call to the controller:\n\n```php [app\u002FHttp\u002FControllers\u002FRegisteredViaGitHubController.php]\n\u003C?php\n\nnamespace App\\Http\\Controllers;\n\nuse Laravel\\Socialite\\Facades\\Socialite;\n\nclass RegisteredViaGitHubController extends Controller\n{\n    public function create()\n    {\n        return Socialite::driver('github')->redirect();\n    }\n}\n```\n\nLet's run the test again:\n\n```bash [terminal]\n➜ php artisan test tests\u002FHttp\u002FRegisteredViaGitHub\u002FCreateTest.php\n\n   PASS  Tests\\Http\\RegisteredViaGitHub\\CreateTest\n  ✓ redirects for authorization\n```\n\nAnd voilà! The test passes. Start the development server with `composer dev` and open `http:\u002F\u002Flocalhost:8000\u002Fauth\u002Fgithub\u002Fredirect` to verify the redirect manually. You should arrive at GitHub's authorization page. 🥳\n\n![Once redirected, you should arrive on the GitHub authorization page](\u002Fseries\u002Fempower-and-dynamize-our-vitepress-blog-with-a-laravel-api\u002Feffortless-social-authentication-with-laravel-socialite\u002Fgithub-authorization.png)\n\n### The callback\n\nNow that you understand the workflow, I will not repeat the instruction to run the test after every small change. Keep following the same loop for the rest of the project and in your own projects.\n\nLet's create a new test file called `StoreTest.php` in the `tests\u002FHttp\u002FRegisteredViaGitHub` folder.\n\n```bash [terminal]\ntouch tests\u002FHttp\u002FRegisteredViaGitHub\u002FStoreTest.php\n```\n\n`StoreTest.php` will test the `store` method of `RegisteredViaGitHubController`, which handles GitHub's callback. Socialite handles the OAuth exchange, so our application only needs to retrieve the provider user, save it, and authenticate that user.\n\nFirst, let's write a test to ensure that a user can be logged in:\n\n```php [tests\u002FHttp\u002FRegisteredViaGitHub\u002FStoreTest.php]\n\u003C?php\n\ndeclare(strict_types=1);\n\nuse Laravel\\Socialite\\Facades\\Socialite;\nuse Laravel\\Socialite\\Two\\User as SocialiteUser;\n\ntest('registers successfully', function () {\n    $fakeGitHubUser = fakeGitHubUser();\n\n    $this->get(route('auth.github.callback'));\n\n    $this->assertAuthenticated();\n\n    $this->assertDatabaseHas('users', [\n        'github_id' => $fakeGitHubUser->getId(),\n        'email' => $fakeGitHubUser->getEmail(),\n        'name' => $fakeGitHubUser->getName(),\n    ]);\n});\n\n\u002F**\n * Create a fake GitHub user and mock the Socialite driver.\n *\n * @param  array\u003Cstring, mixed>  $attributes\n *\u002F\nfunction fakeGitHubUser(array $attributes = []): SocialiteUser\n{\n    $fakeGitHubUser = (new SocialiteUser)->map(attributes: array_merge([\n        'id' => '12345',\n        'nickname' => 'testuser',\n        'email' => 'test@example.com',\n        'name' => 'Test User',\n        'avatar' => 'https:\u002F\u002Fexample.com\u002Favatar.jpg',\n    ], $attributes));\n\n    Socialite::shouldReceive('driver->user')->once()->andReturn($fakeGitHubUser);\n\n    return $fakeGitHubUser;\n}\n```\n\nThis test creates a fake GitHub user and mocks Socialite to return it when `user` is called. We avoid a real HTTP request to GitHub, and the `fakeGitHubUser` helper keeps that setup reusable and the test readable.\n\nThen, we call the `auth.github.callback` route and assert that the user is authenticated and saved in the database.\n\nNow, let's follow the error message from our test.\n\n1. The route `auth.github.callback` is not defined. Let's define it in our `routes\u002Fweb.php` file:\n\n```php [routes\u002Fweb.php]\nRoute::get('\u002Fauth\u002Fgithub\u002Fcallback', [RegisteredViaGitHubController::class, 'store'])\n    ->name('auth.github.callback');\n```\n\n2. The user is not authenticated. Implement the `store` method in the controller:\n\n```php [app\u002FHttp\u002FControllers\u002FRegisteredViaGitHubController.php]\n\u003C?php\n\ndeclare(strict_types=1);\n\nnamespace App\\Http\\Controllers;\n\nuse Illuminate\\Support\\Facades\\Auth;\nuse Laravel\\Socialite\\Facades\\Socialite;\n\nfinal class RegisteredViaGitHubController extends Controller\n{\n    public function store()\n    {\n        $githubUser = Socialite::driver('github')->user();\n\n        Auth::login($githubUser);\n    }\n}\n```\n\n`Socialite::driver('github')->user()` handles the OAuth flow and returns the provider [user](https:\u002F\u002Fgithub.com\u002Flaravel\u002Fsocialite\u002Fblob\u002F5.x\u002Fsrc\u002FAbstractUser.php). We then use Laravel's `Auth` facade to log in that user.\n\n3. The user is still not authenticated. The `login` method expects an [`Authenticatable`](https:\u002F\u002Fgithub.com\u002Flaravel\u002Fframework\u002Fblob\u002F12.x\u002Fsrc\u002FIlluminate\u002FAuth\u002FAuthenticatable.php) application user, but we are passing a Socialite user.\n\nWe cannot pass the Socialite user directly to `login`. We must first create or retrieve an application user and then pass that model to `login`.\n\n```php [app\u002FHttp\u002FControllers\u002FRegisteredViaGitHubController.php]\n\u003C?php\n\ndeclare(strict_types=1);\n\nnamespace App\\Http\\Controllers;\n\nuse App\\Models\\User;\nuse Illuminate\\Support\\Facades\\Auth;\nuse Laravel\\Socialite\\Facades\\Socialite;\n\nfinal class RegisteredViaGitHubController extends Controller\n{\n    public function store()\n    {\n        $githubUser = Socialite::driver('github')->user();\n\n        $user = User::create([\n            'name' => $githubUser->getName(),\n            'email' => $githubUser->getEmail(),\n            'username' => $githubUser->getNickname(),\n            'github_id' => $githubUser->getId(),\n            'avatar' => $githubUser->getAvatar(),\n        ]);\n\n        Auth::login($user);\n    }\n}\n```\n\n4. The user is still not authenticated. This time, check the log file. In Laravel, the log is stored in `storage\u002Flogs\u002Flaravel.log`:\n\n```log [storage\u002Flogs\u002Flaravel.log]\n[2025-02-28 06:51:21] testing.ERROR: SQLSTATE[HY000]: General error: 1 no such table: users (Connection: sqlite, SQL: insert into \"users\" (\"name\", \"email\", \"updated_at\", \"created_at\") values (Test User, test@example.com, 2025-02-28 06:51:21, 2025-02-28 06:51:21))\n```\n\n::callout{icon=\"i-ph-lightbulb-duotone\" color=\"success\"}\nDo not hesitate to remove all the contents of the file before re-running the test. This way, you will only see the relevant logs.\n::\n\nThis error message is clearer than the previous one: the `users` table does not exist, so the request fails before the user can be authenticated.\n\nBefore creating the table, inspect the database migrations:\n\n1. `0001_01_01_000000_create_users_table`: This migration creates the `users` table. The table exists in the schema, but the test database has not run the migration yet.\n2. `0001_01_01_000001_create_cache_table`: This migration creates the `cache` table. We don't care about this one.\n3. `0001_01_01_000002_create_failed_jobs_table`: This migration creates the `failed_jobs` table. We don't care about this one.\n\nThe good news is that we can run migrations automatically before each test. This keeps the database in a clean state. Add the `RefreshDatabase` trait to `Pest.php`:\n\n```php [tests\u002FPest.php]\n\u003C?php\n\ndeclare(strict_types=1);\n\npest()->extend(Tests\\TestCase::class)\n    ->use(Illuminate\\Foundation\\Testing\\RefreshDatabase::class)\n    ->in('Http');\n```\n\n5. The user is still not authenticated. Check the log again:\n\n```log [storage\u002Flogs\u002Flaravel.log]\ntesting.ERROR: SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: users.password (Connection: sqlite, SQL: insert into \"users\" (\"name\", \"email\", \"updated_at\", \"created_at\") values (Test User, test@example.com, 2025-02-28 06:59:57, 2025-02-28 06:59:57))\n```\n\nThis SQL error tells us that `password` is not nullable, but GitHub authentication does not provide a password. We need to adjust the migration for OAuth authentication.\n\n```php [database\u002Fmigrations\u002F0001_01_01_000000_create_users_table.php]\n\u003C?php\n\ndeclare(strict_types=1);\n\nuse Illuminate\\Database\\Migrations\\Migration;\nuse Illuminate\\Database\\Schema\\Blueprint;\nuse Illuminate\\Support\\Facades\\Schema;\n\nreturn new class extends Migration\n{\n    \u002F**\n     * Run the migrations.\n     *\u002F\n    public function up(): void\n    {\n        Schema::create('users', function (Blueprint $table): void {\n            $table->id();\n            $table->string('name')->nullable(); \u002F\u002F [!code ++]\n            $table->string('username')->unique(); \u002F\u002F [!code ++]\n            $table->string('email')->unique();\n            $table->timestamp('email_verified_at')->nullable();\n            $table->string('password'); \u002F\u002F [!code --]\n            $table->rememberToken(); \u002F\u002F [!code --]\n            $table->string('github_id')->nullable()->unique(); \u002F\u002F [!code ++]\n            $table->string('avatar')->nullable(); \u002F\u002F [!code ++]\n            $table->timestamps();\n        });\n\n        \u002F\u002F other migrations...\n    }\n};\n\n```\n\nIn this migration, we add three columns:\n\n- `username`: Stores the user's GitHub username.\n- `github_id`: Stores the user's GitHub ID. It is unique so that two application users cannot share the same GitHub account, and nullable so that other authentication methods can be added later. Making a column nullable after the fact is more complicated in SQLite, so we add that constraint now.\n- `avatar`: Stores the user's avatar URL.\n\n::callout{icon=\"i-ph-info-duotone\" color=\"info\"}\nWe directly edit the migration file instead of creating a new one because we haven't deployed the application yet. If we had, we would have created a new migration to alter the table.\n::\n\n6. The user is still not authenticated. Debugging authentication in a fresh application can feel repetitive, but each error reveals another missing piece. Once you understand the process, it becomes much easier.\n\nIn our `laravel.log` file, we can see:\n\n```log [storage\u002Flogs\u002Flaravel.log]\n[2025-02-28 07:09:29] testing.ERROR: SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: users.username (Connection: sqlite, SQL: insert into \"users\" (\"name\", \"email\", \"updated_at\", \"created_at\") values (Test User, test@example.com, 2025-02-28 07:09:29, 2025-02-28 07:09:29))\n```\n\nThis SQL error is similar to the previous one, but this time `username` is missing. That is surprising because both the migration and `User::create` contain the field.\n\nThe problem is Laravel's mass-assignment protection. By default, we must explicitly allow `username`, `github_id`, and `avatar` in the `User` model:\n\n```php [app\u002FModels\u002FUser.php]\n\u003C?php\n\ndeclare(strict_types=1);\n\nnamespace App\\Models;\n\n\u002F\u002F use Illuminate\\Contracts\\Auth\\MustVerifyEmail;\nuse Illuminate\\Database\\Eloquent\\Factories\\HasFactory;\nuse Illuminate\\Foundation\\Auth\\User as Authenticatable;\nuse Illuminate\\Notifications\\Notifiable;\n\nclass User extends Authenticatable\n{\n    \u002F** @use HasFactory\u003C\\Database\\Factories\\UserFactory> *\u002F\n    use HasFactory, Notifiable;\n\n    \u002F**\n     * The attributes that are mass assignable.\n     *\n     * @var list\u003Cstring>\n     *\u002F\n    protected $fillable = [\n        'name',\n        'email',\n        'username', \u002F\u002F [!code ++]\n        'github_id', \u002F\u002F [!code ++]\n        'avatar', \u002F\u002F [!code ++]\n        'password', \u002F\u002F [!code --]\n    ];\n\n    \u002F\u002F ...\n}\n```\n\nThis approach works, but it means remembering to update `fillable` every time the model gains an assignable column. Mass-assignment protection is especially important when request data is passed directly to a model. For example, a user must never be able to set an `is_admin` field while registering.\n\n```php [Example]\nfunction (Request $request): void {\n    User::create($request->all()); \u002F\u002F Never write this code.\n}\n```\n\nWithout this protection, a user could become an administrator simply by sending `is_admin=true` in the request. That would be a serious security issue.\n\nTo remove the repetitive model configuration while keeping the input safe, we can disable guarded mode with `Model::unguard()` in `app\u002FProviders\u002FAppServiceProvider.php`:\n\n```php [app\u002FProviders\u002FAppServiceProvider.php]\n\u003C?php\n\ndeclare(strict_types=1);\n\nnamespace App\\Providers;\n\nuse Illuminate\\Database\\Eloquent\\Model;\nuse Illuminate\\Support\\ServiceProvider;\n\nclass AppServiceProvider extends ServiceProvider\n{\n    public function boot(): void\n    {\n        Model::unguard();\n    }\n}\n```\n\nFrom now on, all models will be unguarded, so we can remove `fillable` from `User`. However, every user-controlled input must go through a form request. Validating the input before saving it ensures that users cannot set arbitrary fields or unexpected values.\n\nIn this OAuth flow, the data comes from a provider we trust, so we do not need a form request for it. We will use form requests later when we implement the comment system.\n\nAnd voilà! The test passes, and users can now create an account through GitHub. 🥳\n\nWe are not finished yet. If a user logs in again, the application will try to create a duplicate record. We need to handle existing users.\n\nFirst, let's write a test to ensure that a user can log in again:\n\n```php [tests\u002FHttp\u002FRegisteredViaGitHub\u002FStoreTest.php]\n\u003C?php\n\nuse App\\Models\\User;\n\n\u002F\u002F other tests...\n\ntest('updates the user if they already exist', function () {\n    $fakeGitHubUser = fakeGitHubUser();\n\n    $user = User::factory()->create(['github_id' => $fakeGitHubUser->getId()]);\n\n    $this->get(route('auth.github.callback'));\n\n    $this->assertAuthenticated();\n\n    $this->assertDatabaseHas('users', [\n        'id' => $user->id,\n        'github_id' => $fakeGitHubUser->getId(),\n        'email' => $fakeGitHubUser->getEmail(),\n        'name' => $fakeGitHubUser->getName(),\n    ]);\n});\n\n```\n\nThis test creates a user with the same `github_id` as the fake GitHub user. It then calls the callback route and asserts that the existing user is authenticated and updated.\n\nLet's follow the error message:\n\n```bash [terminal]\nSQLSTATE[HY000]: General error: 1 table users has no column named password (Connection: sqlite, SQL: insert into \"users\" (\"name\", \"email\", \"email_verified_at\", \"password\", \"remember_token\", \"github_id\", \"updated_at\", \"created_at\") values (Lawrence Herzog, roconnell@example.net, 2025-03-01 18:44:12, $2y$04$NXCZUWIcdKPiCdw3n5cTBOIpQo9\u002FDPc9rmgX5VRTQ3iTeWduC4x36, 0i9tgsiLpA, 12345, 2025-03-01 18:44:12, 2025-03-01 18:44:12))\n```\n\nThis SQL error mentions the `password` column. That is strange because we removed it from the migration. The error comes from the user factory, which still contains Laravel's default password fields. Open `database\u002Ffactories\u002FUserFactory.php` and remove them:\n\n```php [database\u002Ffactories\u002FUserFactory.php]\n\u003C?php\n\ndeclare(strict_types=1);\n\nnamespace Database\\Factories;\n\nuse Illuminate\\Database\\Eloquent\\Factories\\Factory;\nuse Illuminate\\Support\\Facades\\Hash; \u002F\u002F [!code --]\nuse Illuminate\\Support\\Str; \u002F\u002F [!code --]\n\n\u002F**\n * @extends \\Illuminate\\Database\\Eloquent\\Factories\\Factory\u003C\\App\\Models\\User>\n *\u002F\nclass UserFactory extends Factory\n{\n    \u002F** \u002F\u002F [!code --]\n     * The current password being used by the factory. \u002F\u002F [!code --]\n     *\u002F \u002F\u002F [!code --]\n    protected static ?string $password = null; \u002F\u002F [!code --]\n\n    \u002F**\n     * Define the model's default state.\n     *\n     * @return array\u003Cstring, mixed>\n     *\u002F\n    public function definition(): array\n    {\n        return [\n            'name' => fake()->name(),\n            'username' => fake()->unique()->userName(), \u002F\u002F [!code ++]\n            'email' => fake()->unique()->safeEmail(),\n            'github_id' => fake()->unique()->numberBetween(1, 1000000), \u002F\u002F [!code ++]\n            'avatar' => fake()->imageUrl(), \u002F\u002F [!code ++]\n            'email_verified_at' => now(),\n            'password' => self::$password ??= Hash::make('password'), \u002F\u002F [!code --]\n            'remember_token' => Str::random(10), \u002F\u002F [!code --]\n        ];\n    }\n \u002F\u002F [!code --]\n    \u002F** \u002F\u002F [!code --]\n     * Indicate that the model's email address should be unverified. \u002F\u002F [!code --]\n     *\u002F \u002F\u002F [!code --]\n    public function unverified(): static \u002F\u002F [!code --]\n    { \u002F\u002F [!code --]\n        return $this->state(fn (array $attributes): array => [ \u002F\u002F [!code --]\n            'email_verified_at' => null, \u002F\u002F [!code --]\n        ]);  \u002F\u002F [!code --]\n    } \u002F\u002F [!code --]\n}\n```\n\nAfter this change, the error is `The user is not authenticated`. We know what that means: there is another problem to fix.\n\nBy checking the `laravel.log` file, we discover that the issue we wanted to find:\n\n```log [storage\u002Flogs\u002Flaravel.log]\n[2025-03-01 18:51:14] testing.ERROR: SQLSTATE[23000]: Integrity constraint violation: 19 UNIQUE constraint failed: users.github_id (Connection: sqlite, SQL: insert into \"users\" (\"name\", \"email\", \"username\", \"github_id\", \"avatar\", \"updated_at\", \"created_at\") values (Test User, test@example.com, testuser, 12345, https:\u002F\u002Fexample.com\u002Favatar.jpg, 2025-03-01 18:51:14, 2025-03-01 18:51:14))\n```\n\nThis SQL error means that a user with the same `github_id` already exists. Instead of always creating a new user, we need to update the existing record when it is found and create one when it is not. Laravel's [`updateOrCreate`](https:\u002F\u002Flaravel.com\u002Fdocs\u002F12.x\u002Feloquent#updates) method does exactly that.\n\n```php [app\u002FHttp\u002FControllers\u002FRegisteredViaGitHubController.php]\n\u003C?php\n\nclass RegisteredViaGitHubController extends Controller\n{\n    public function store()\n    {\n        $githubUser = Socialite::driver('github')->user();\n\n        $user = User::updateOrCreate(\n            [\n                'github_id' => $githubUser->getId(),\n            ],\n            [\n                'name' => $githubUser->getName(),\n                'email' => $githubUser->getEmail(),\n                'username' => $githubUser->getNickname(),\n                'avatar' => $githubUser->getAvatar(),\n            ]\n        );\n\n        Auth::login($user);\n    }\n}\n```\n\nFinally, redirect the user to the frontend homepage after logging in.\n\nThe last test is to ensure that the user is redirected to the homepage after logging in:\n\n```php [tests\u002FHttp\u002FRegisteredViaGitHub\u002FStoreTest.php]\n\u003C?php\n\n\u002F\u002F other tests...\n\ntest('redirects to the homepage', function () {\n    fakeGitHubUser();\n\n    $this->get(route('auth.github.callback'))\n        ->assertRedirect('\u002F');\n});\n```\n\nThe `assertRedirect` method verifies that the user is redirected to the homepage after logging in.\n\nThen, the last step is to implement the redirection in our controller:\n\n```php [app\u002FHttp\u002FControllers\u002FRegisteredViaGitHubController.php]\n\u003C?php\nclass RegisteredViaGitHubController extends Controller\n{\n    public function store()\n    {\n        \u002F\u002F ...\n\n        Auth::login($user);\n\n        return redirect('\u002F');\n    }\n}\n```\n\nAnd now, the controller is complete!\n\n## Reaching complete coverage\n\nBefore finishing, make sure the test script runs correctly.\n\n::callout{icon=\"i-ph-lightbulb-duotone\" color=\"success\"}\nBefore running the test script, run the lint and refactor scripts first.\n::\n\nPHPStan is not happy with our code. It raises two errors:\n\n```bash [terminal]\n ------ ---------------------------------------------------------------------------------------------------\n  Line   app\u002FHttp\u002FControllers\u002FRegisteredViaGitHubController.php\n ------ ---------------------------------------------------------------------------------------------------\n  :13    Method App\\Http\\Controllers\\RegisteredViaGitHubController::create() has no return type specified.\n         🪪  missingType.return\n  :18    Method App\\Http\\Controllers\\RegisteredViaGitHubController::store() has no return type specified.\n         🪪  missingType.return\n ------ ---------------------------------------------------------------------------------------------------\n```\n\nThe methods are missing return types. Add `: RedirectResponse` to both methods:\n\n```php [app\u002FHttp\u002FControllers\u002FRegisteredViaGitHubController.php]\n\u003C?php\n\nuse Symfony\\Component\\HttpFoundation\\RedirectResponse;\n\nclass RegisteredViaGitHubController extends Controller\n{\n    public function create(): RedirectResponse\n    {\n        \u002F\u002F ...\n    }\n\n    public function store(): RedirectResponse\n    {\n        \u002F\u002F ...\n    }\n}\n```\n\nThe `test:unit` script still fails because this default route has no test:\n\n```php [routes\u002Fweb.php]\nRoute::get('\u002F', function () {\n    return view('welcome');\n});\n```\n\nWe can safely remove it because users should be redirected to the homepage of the frontend application, not the Laravel application.\n\nOnce removed, everything is green! 🟢\n\n_I really really love this feeling._\n\n## Final thoughts\n\nGitHub authentication is now in place. We have a local `User` model, a complete OAuth callback flow, and tests that describe the behavior we expect. The test-first workflow may feel slower at first, but it gives us confidence as the implementation grows.\n\nSocialite handles the provider-specific details so we can focus on our application's users and sessions. That gives the comment system a reliable owner for each comment.\n\nNext, we will design and implement the Laravel API for listing, creating, editing, and deleting comments.\n",{"title":5,"description":3349},"empower-and-dynamize-our-vitepress-blog-with-a-laravel-api",{"loc":3353,"images":3358},[3359],{"loc":1081},"series\u002Fempower-and-dynamize-our-vitepress-blog-with-a-laravel-api\u002Feffortless-social-authentication-with-laravel-socialite","O4u26kxeAO88BuzK3JRmLLsEzUxcwCoPyQ3kvY3cFeo",{"overview":3363,"chapters":3537},{"id":3364,"title":3365,"body":3366,"contentId":3521,"date":3522,"description":3523,"extension":3350,"github":3351,"meetTheInstructor":3524,"meta":3525,"navigation":547,"part":3351,"path":3526,"rawbody":3527,"robots":3351,"schemaOrg":3351,"seo":3528,"seriesSlug":3356,"sitemap":3529,"stem":3530,"subtitle":3531,"topics":3532,"__hash__":3536},"series\u002Fseries\u002Fempower-and-dynamize-our-vitepress-blog-with-a-laravel-api.md","Empower and Enhance Your VitePress Blog with a Laravel API",{"type":7,"value":3367,"toc":3518},[3368,3371,3374,3377,3381,3390,3393,3402,3405,3408,3489,3492,3495,3498,3508,3512],[10,3369,3370],{},"Hello! 👋",[10,3372,3373],{},"After building our VitePress blog, we are ready to make it more dynamic with a Laravel API.",[10,3375,3376],{},"In the previous series, we explored VitePress from the initial project setup to deployment on Cloudflare Pages. We built a functional, automated blog with SEO metadata, Open Graph images, and a GitHub projects page. The blog handled everything at build time, so publishing content was the only task left.",[3378,3379],"callout-read-more",{"id":3380},"43c95b33-041c-4108-ab98-0d21ef60ed7f",[10,3382,3383,3384,3389],{},"This series introduces back-end development by creating a ",[17,3385,3388],{"href":3386,"rel":3387},"https:\u002F\u002Flaravel.com",[21],"Laravel"," API that gives our blog a few interactive features.",[10,3391,3392],{},"Until now, our blog has been mostly static. Content was generated at build time, with a small amount of data loaded dynamically, and the resulting HTML files were distributed through a CDN. By adding a Laravel API, we will let readers interact with the blog through features such as post reactions and comments.",[10,3394,3395,3396,3401],{},"Although the focus is on the back end, we will also integrate the API into our VitePress blog. We will use Vue.js and ",[17,3397,3400],{"href":3398,"rel":3399},"https:\u002F\u002Fpinia-colada.esm.dev\u002F",[21],"Pinia Colada"," as our data-fetching framework. VitePress is a Vue.js application, so we can extend it with the components we need.",[10,3403,3404],{},"The examples are designed to be easy to follow. Unnecessary complexity and over-engineering are neither desirable nor necessary here. We will focus on simplicity, clarity, and efficiency.",[10,3406,3407],{},"Throughout the series, we will build a Laravel API and integrate it with our VitePress blog. We will cover:",[1931,3409,3410,3417,3444,3455,3461,3471,3477,3483],{},[60,3411,3412,3416],{},[3413,3414,3415],"strong",{},"Setting up the Laravel project",": Create a new Laravel project and become familiar with its structure.",[60,3418,3419,3422,3423,1992,3428,1992,3433,1995,3438,3443],{},[3413,3420,3421],{},"Improving the development workflow",": Configure ",[17,3424,3427],{"href":3425,"rel":3426},"https:\u002F\u002Flaravel.com\u002Fdocs\u002F11.x\u002Fpint",[21],"Pint",[17,3429,3432],{"href":3430,"rel":3431},"https:\u002F\u002Fgithub.com\u002Frectorphp\u002Frector",[21],"Rector",[17,3434,3437],{"href":3435,"rel":3436},"https:\u002F\u002Fgithub.com\u002Flarastan\u002Flarastan",[21],"Larastan",[17,3439,3442],{"href":3440,"rel":3441},"https:\u002F\u002Fpestphp.com\u002F",[21],"Pest"," for formatting, refactoring, static analysis, and testing.",[60,3445,3446,3449,3450,29],{},[3413,3447,3448],{},"Adding social authentication",": Let users sign in with GitHub through ",[17,3451,3454],{"href":3452,"rel":3453},"https:\u002F\u002Flaravel.com\u002Fdocs\u002F11.x\u002Fsocialite",[21],"Laravel Socialite",[60,3456,3457,3460],{},[3413,3458,3459],{},"Connecting the applications",": Configure Sanctum, CORS, CSRF protection, and the first requests between the VitePress frontend and the Laravel API.",[60,3462,3463,3466,3467,3470],{},[3413,3464,3465],{},"Managing authentication state",": Use ",[17,3468,3400],{"href":3398,"rel":3469},[21]," to retrieve the current user and log out with an optimistic update.",[60,3472,3473,3476],{},[3413,3474,3475],{},"Building the comment API",": Create the models, relationships, endpoints, validation, authorization, and tests for comments.",[60,3478,3479,3482],{},[3413,3480,3481],{},"Keeping the backend in sync",": Generate post metadata from VitePress and synchronize it with the Laravel database.",[60,3484,3485,3488],{},[3413,3486,3487],{},"Building the comment interface",": Fetch, create, edit, and delete comments from the VitePress frontend.",[10,3490,3491],{},"By the end of the series, you will have a blog backed by a Laravel API. Readers will be able to sign in with GitHub and leave comments, and the architecture will be ready for additional interactive features.",[10,3493,3494],{},"Testing is part of the development process and will be included at each stage, so it does not need a dedicated article.",[10,3496,3497],{},"If you have questions, feedback, or suggestions, feel free to share them in the comments. Your input helps improve the series.",[85,3499,3500],{"color":87,"icon":88},[10,3501,3502,3503,3507],{},"This series assumes prior knowledge of TypeScript, Vue.js, and Laravel. If you are unfamiliar with any of these technologies, review the ",[17,3504,3506],{"href":3505},"\u002Fseries\u002Fcreate-a-blog-with-vitepress-and-vue-js-from-scratch","Create a Blog with VitePress and Vue.js from Scratch"," first.",[40,3509,3511],{"id":3510},"chapters","Chapters",[3513,3514],"series-chapters-list",{"className":3515},[3516,3517],"mx-auto","max-w-5xl",{"title":120,"searchDepth":161,"depth":161,"links":3519},[3520],{"id":3510,"depth":161,"text":3511},"676d45dd-eb18-42ec-803e-b9d6b7f04b42","2024-11-19","Transform your VitePress blog into a dynamic platform with a Laravel API. Leverage backend API capabilities to create advanced features.","I create this series because I love \u003Cstrong>VitePress\u003C\u002Fstrong> and \u003Cstrong>Laravel\u003C\u002Fstrong>. I truly believe that combining the two results in a simple yet \u003Cstrong>powerful solution\u003C\u002Fstrong> for creating a blog. I want to empower developers to enhance their VitePress blogs with a Laravel API to create advanced features.",{},"\u002Fseries\u002Fempower-and-dynamize-our-vitepress-blog-with-a-laravel-api","---\ncontentId: 676d45dd-eb18-42ec-803e-b9d6b7f04b42\ntitle: Empower and Enhance Your VitePress Blog with a Laravel API\nsubtitle: Transform your VitePress blog into a dynamic platform with an API\ndescription: Transform your VitePress blog into a dynamic platform with a Laravel API. Leverage backend API capabilities to create advanced features.\ndate: 2024-11-19\ntopics:\n  - laravel\n  - vitepress\n  - api\nmeetTheInstructor: I create this series because I love \u003Cstrong>VitePress\u003C\u002Fstrong> and \u003Cstrong>Laravel\u003C\u002Fstrong>. I truly believe that combining the two results in a simple yet \u003Cstrong>powerful solution\u003C\u002Fstrong> for creating a blog. I want to empower developers to enhance their VitePress blogs with a Laravel API to create advanced features.\nseriesSlug: empower-and-dynamize-our-vitepress-blog-with-a-laravel-api\n---\n\nHello! 👋\n\nAfter building our VitePress blog, we are ready to make it more dynamic with a Laravel API.\n\nIn the previous series, we explored VitePress from the initial project setup to deployment on Cloudflare Pages. We built a functional, automated blog with SEO metadata, Open Graph images, and a GitHub projects page. The blog handled everything at build time, so publishing content was the only task left.\n\n\u003CCalloutReadMore id=\"43c95b33-041c-4108-ab98-0d21ef60ed7f\" \u002F>\n\nThis series introduces back-end development by creating a [Laravel](https:\u002F\u002Flaravel.com) API that gives our blog a few interactive features.\n\nUntil now, our blog has been mostly static. Content was generated at build time, with a small amount of data loaded dynamically, and the resulting HTML files were distributed through a CDN. By adding a Laravel API, we will let readers interact with the blog through features such as post reactions and comments.\n\nAlthough the focus is on the back end, we will also integrate the API into our VitePress blog. We will use Vue.js and [Pinia Colada](https:\u002F\u002Fpinia-colada.esm.dev\u002F) as our data-fetching framework. VitePress is a Vue.js application, so we can extend it with the components we need.\n\nThe examples are designed to be easy to follow. Unnecessary complexity and over-engineering are neither desirable nor necessary here. We will focus on simplicity, clarity, and efficiency.\n\nThroughout the series, we will build a Laravel API and integrate it with our VitePress blog. We will cover:\n\n- **Setting up the Laravel project**: Create a new Laravel project and become familiar with its structure.\n- **Improving the development workflow**: Configure [Pint](https:\u002F\u002Flaravel.com\u002Fdocs\u002F11.x\u002Fpint), [Rector](https:\u002F\u002Fgithub.com\u002Frectorphp\u002Frector), [Larastan](https:\u002F\u002Fgithub.com\u002Flarastan\u002Flarastan), and [Pest](https:\u002F\u002Fpestphp.com\u002F) for formatting, refactoring, static analysis, and testing.\n- **Adding social authentication**: Let users sign in with GitHub through [Laravel Socialite](https:\u002F\u002Flaravel.com\u002Fdocs\u002F11.x\u002Fsocialite).\n- **Connecting the applications**: Configure Sanctum, CORS, CSRF protection, and the first requests between the VitePress frontend and the Laravel API.\n- **Managing authentication state**: Use [Pinia Colada](https:\u002F\u002Fpinia-colada.esm.dev\u002F) to retrieve the current user and log out with an optimistic update.\n- **Building the comment API**: Create the models, relationships, endpoints, validation, authorization, and tests for comments.\n- **Keeping the backend in sync**: Generate post metadata from VitePress and synchronize it with the Laravel database.\n- **Building the comment interface**: Fetch, create, edit, and delete comments from the VitePress frontend.\n\nBy the end of the series, you will have a blog backed by a Laravel API. Readers will be able to sign in with GitHub and leave comments, and the architecture will be ready for additional interactive features.\n\nTesting is part of the development process and will be included at each stage, so it does not need a dedicated article.\n\nIf you have questions, feedback, or suggestions, feel free to share them in the comments. Your input helps improve the series.\n\n::callout{icon=\"i-ph-info-duotone\" color=\"info\"}\nThis series assumes prior knowledge of TypeScript, Vue.js, and Laravel. If you are unfamiliar with any of these technologies, review the [Create a Blog with VitePress and Vue.js from Scratch](\u002Fseries\u002Fcreate-a-blog-with-vitepress-and-vue-js-from-scratch) first.\n::\n\n## Chapters\n\n::series-chapters-list\n---\nclass: mx-auto max-w-5xl\n---\n::\n",{"title":3365,"description":3523},{"loc":3526},"series\u002Fempower-and-dynamize-our-vitepress-blog-with-a-laravel-api","Transform your VitePress blog into a dynamic platform with an API",[3533,3534,3535],"laravel","vitepress","api","JLBtX9BKfN72BkhcYisEXraNGTvZSnT5YHM05A_uemk",[3538,3543,3548,3549,3554,3559,3564,3569],{"contentId":3539,"title":3540,"description":3541,"path":3542,"part":128,"github":3351,"topics":3351},"06469e25-715e-4a92-87b9-0db6db845a50","Setting Up Your Laravel Project Structure and Essentials","A comprehensive guide to setting up your Laravel project and understanding its structure for a better development experience.","\u002Fseries\u002Fempower-and-dynamize-our-vitepress-blog-with-a-laravel-api\u002Fsetting-up-your-laravel-project-structure-and-essentials",{"contentId":3544,"title":3545,"description":3546,"path":3547,"part":161,"github":3351,"topics":3351},"d566d5f4-3c4a-44e5-89cb-b3c547673ffa","Pint, Rector, Larastan, and Pest: Essentials for Success","Explore the four indispensable tools for maintaining code quality and consistency in Laravel projects: Pint, Rector, Larastan, and Pest.","\u002Fseries\u002Fempower-and-dynamize-our-vitepress-blog-with-a-laravel-api\u002Fpint-larastan-rector-and-pest-essential-for-success",{"contentId":3347,"title":5,"description":3349,"path":3353,"part":167,"github":3351,"topics":3351},{"contentId":3550,"title":3551,"description":3552,"path":3553,"part":173,"github":3351,"topics":3351},"b07ec3b5-ca56-4d14-ae2f-8c0702218293","Building a Simple Comment System for User Engagement","Build a Laravel comment API with models, migrations, resources, policies, form requests, controllers, routes, and comprehensive tests.","\u002Fseries\u002Fempower-and-dynamize-our-vitepress-blog-with-a-laravel-api\u002Fbuilding-a-simple-comment-system-for-user-engagement",{"contentId":3555,"title":3556,"description":3557,"path":3558,"part":179,"github":3351,"topics":3351},"53a607df-d10e-4423-966c-500d2885e6e0","Automating Your Backend to Stay in Sync Effortlessly","Generate frontend metadata with VitePress, synchronize posts with Laravel, and keep the backend database aligned automatically.","\u002Fseries\u002Fempower-and-dynamize-our-vitepress-blog-with-a-laravel-api\u002Fautomating-your-backend-to-stay-in-sync-effortlessly",{"contentId":3560,"title":3561,"description":3562,"path":3563,"part":185,"github":3351,"topics":3351},"e6040e0a-4997-40d6-bde8-332cbca0f307","Bridging the Gap: Connecting Your Frontend and Backend","Connect a VitePress frontend to a Laravel API with Sanctum, CORS, CSRF protection, shared credentials, and a reusable API client.","\u002Fseries\u002Fempower-and-dynamize-our-vitepress-blog-with-a-laravel-api\u002Fbridging-the-gap-connecting-your-frontend-and-backend",{"contentId":3565,"title":3566,"description":3567,"path":3568,"part":191,"github":3351,"topics":3351},"d507f8af-7c76-4660-859e-23af11c0728e","Streamlining User Authentication from Frontend to Backend","Connect frontend authentication to Laravel with a current-user query, GitHub logout flow, Pinia Colada caching, and optimistic state updates.","\u002Fseries\u002Fempower-and-dynamize-our-vitepress-blog-with-a-laravel-api\u002Fstreamlining-user-authentication-from-frontend-to-backend",{"contentId":3570,"title":3571,"description":3572,"path":3573,"part":197,"github":3351,"topics":3351},"1b2a6cc0-c10f-421a-9344-60cb1583a136","Simplifying Comment Querying and Editing with Pinia Colada","Build the VitePress comment interface with Pinia Colada queries, mutations, optimistic updates, reusable Vue components, and permissions.","\u002Fseries\u002Fempower-and-dynamize-our-vitepress-blog-with-a-laravel-api\u002Fsimplifying-comment-querying-and-editing-with-pinia-colada",1795016693537]