Luke Bouch
Archive Photos Replies Main Site Also on Micro.blog
  • Migrating from Laravel's Token Guard to Sanctum

    In an application I was working on today, I had to migrate from Laravel token authentication to Sanctum. The process went relatively smoothly but just in case someone else has trouble, I thought I would layout the migration process.

    Installing Sanctum

    I’m going to direct you to the official documentation for instructions on installing Sanctum. It’s very straight forward and should be easy for anyone follow.

    Migrating Api Keys

    In this application, we had a ApiUser model to distinguish between regular users and other applications that talk to our api. While Sanctum uses a second table to store the access tokens (personal_access_tokens), with the Laravel token authentication guard, we were storing the token directly on the ApiUser model itself. We did not want to invalidate all of the api keys we already had in the database so migrating them all over to Sanctum’s personal_access_tokens table was essential.

    Following the documentation, I added the HasApiTokens trait to the model I was going to be associating the tokens with. This provides all of the necessary methods and relationships in order to both create tokens for the model and to authenticate the incoming request against the model.

    To migrate the tokens from the old table to the new one was a little bit tricky and I had to do some digging in the source code of Sanctum in order to come up with a solution. You see, we were storing our tokens in plain text in the database, but Sanctum hashes the token before storing them. This meant it was not just as simple as coping the tokens from one table to the next.

    I first tried using the createToken() method the HasApiTokens trait provides. The problem with this is was, it generates the token for you and does not allow you to actually specify you own string to use as the token (which is what I needed to do in order to migrate the old tokens over).

    I was able to dive into the source code for createToken() and realized all it was doing was creating a PersonalAccessToken with a randomly generated string and saving it to the model. I copied and pasted this function and with a little modification, was able to create this migration that would work.

    return new class extends Migration
    {
        public function up()
        {
            ApiUser::all()->each(function (ApiUser $apiUser) {
                $plainTextToken = $apiUser->api_token;
    
                $apiUser->tokens()->create([
                    ‘name’ => ‘Migrated Token’,
                    ‘token’ => hash(‘sha256’, $plainTextToken),
                    ‘abilities’ => [‘*’],
                    ‘expires_at’ => null,
                ]);
            });
        }
    };
    

    Changing the Authentication Middleware

    I had to then instruct Laravel to use Sanctum as opposed to the token guard for authentication to our api. I did a search for ->middleware([‘auth:api’]) and replaced all instances of auth:api with auth:sanctum.

    Updating Tests

    With the all of the tokens successfully migrated over to the personal_access_tokens table and our api.php routes file updated to use the new middleware, the last thing to do was to update all of our tests that were manually passing a bearer token in the authorization header with every request to using this snippet that I found and modified from the documentation: Sanctum::actingAs(ApiUser::factory()->create(), [‘*’]);.

    Conclusion

    In conclusion, migrating to Laravel Sanctum is not as hard as I expect it would be. It’s fairly straight forward with the hardest part being the migration of old api tokens.

    I hope this article will be helpful to you as a guide for the process of migrating to Sanctum.

    → 12:01 PM, Oct 13
  • Overlanding Expo East 2022

    This weekend, I went camping with some friends and my dad and brother. We went to Overland Expo and here are some photos I got from it.

    → 9:05 PM, Oct 9
  • Replacing the Heater Core on my 2001 Jeep Cherokee XJ

    I have had the sneaky suspicion that my heater core on my Jeep has been leaking for a little while now and have been waiting for a good time to replace it. With a camping trip coming up, I finally decided to tackle it. I had read online that it would take roughly 5-10 hours to complete so on Saturday evening, I began the process of completely taking the dash apart.

    It took me Saturday night, most of the day Sunday, and the past several evenings after work to get the job done. And boy was it a big job! I would say I have ~15hrs into it but that also includes flushing out the entire coolant system.

    I’m super glad to have finally have heat that does not result in all the windshield fogging up.

    Oh… and it rain all weekend while I was working outside.

    On a side note, I went ahead and decided to replace the thermostat because I wanted to remove the housing to properly flush out the engine. These Jeep’s take a 180 degree thermostat but it is not uncommon for people to unintentionally, or without knowing better, replace them with a 195 degree and that was the case with my Jeep. Not to mention, some how a o-ring was suck in the thermostat.

    With a freshly flushed coolant system and a new thermostat, the Jeep is staying nice and cool.

    → 9:09 PM, Oct 5
  • Trip to Illinois: Day 03-07

    I was planning on publishing a post for each day of my trip but I have given up on that. Overall, the trip was amazing and I really enjoyed getting to know the people at Wilber.

    → 8:26 PM, Sep 20
  • Trip to Illinois: Day 02

    → 10:51 PM, Sep 16
  • Trip To Illinois: Day 01

    → 7:39 PM, Sep 12
  • L.O.Y.O.

    L.O.Y.O. - Live only you once.

    → 1:47 PM, Aug 17
  • Purchasing a 2001 Jeep Cherokee

    I have been wanting to sell my truck and purchase a Jeep for a while. The lack of four wheel drive has been driving me crazy every time it snows. Not that I have anywhere I need to be; I just want to get out and drive in the snow. My brother purchased a Cherokee XJ last week and it was the motivation I needed to finally decide to make the switch.

    I found a 2000 Jeep Cherokee on Facebook marketplace on Saturday that looked like it was in good condition but was overheating. I thought it was something I could fix myself so my dad, a friend, and I made the hour and a half drive to go look at it.

    When I got there, I quickly realized the Jeep needed way more work than I was willing to put into it. There were several issues in addition to the overheating issue.

    On the way home, I thought I might as well look to see if there was any other Jeeps listed in the area. I found one that was listed back at home, but after contacting the guy, I learned that it was at his girlfriend’s house, which was on the way home. We went to look at it, knowing that it was overpriced.

    When we got there, the starter had gone bad on it and so I was not able to hear it run. After looking at it carefully, my dad and I were able to negotiate them down from $5,500 to $3,500! I tried not to show my excitement and eagerly paid her. That night, at 9pm, I started working on replacing the battery and starter. I got it fixed in just over an hour.

    Over the past few days, I have been doing some light off roading and having a blast! Here are a few photos.

    → 9:16 PM, Aug 9
  • Father-Son Camping Trip 2022

    I got back yesterday from our 17th annual Father-Son camping trip. We all had a lot of fun!

    → 2:08 PM, Jul 17
  • First Day at My New Job

    The power went out this morning so I had to go to Panera to work. It was my first day at my new job. It was a great day!

    → 9:14 PM, May 16
  • First Web Development Job

    I want to share what The LORD has done in my life over the last few days.

    For the past few months, I have been struggling to know where to go with my career. I taught myself Laravel and the TALL stack and I was doing some freelance work. My mom kept suggesting that I get a job working for someone who could mentor me.

    We were discussing my options (going to school, freelancing, or applying for a junior web developer job) on Thursday night after I got back from my job at the church. These conversations were a regular occurrence. We prayed about it that night.

    On Friday around 1 pm, I got a text from my cousin. It was a screenshot of an email he had gotten from Jacob Bennett. It read

    Hey,

    Was looking to talk to your brother Luke but haven’t been able to find a way to get in touch with him. It’s regarding a possible position to work in Laravel at our company https://wilbergroup.com in Illinois, remote position of course. I’ve been watching some of the things he’s been working on, https://sublimeblogs.com/ for example and think he would make a great junior developer! Haven’t had any luck finding him but stumbled across your email and thought maybe you could either forward this to him or send me his email so I can contact him directly.

    Thanks for any help!

    Jake Bennett

    I immediately called my cousin and asked him to forward me the email. I email Jake and told him I was interested and gave him my phone number. He called me a little while later. He asked me a few questions and I walked him through Sublime Blogs.

    Jake said he had been trying to get in contact with me for the past 10 days but he could not find any contact information. (That is something I will need to add to my website.)

    I had an interview with him and his supervisor on Monday. It was a hour and a half and it went very well. They asked me a lot of personal questions and not very many technical ones.

    Later that day, Jake texted me and we setup a time that afternoon for the technical interview. He sent me some of the PHP classes he had written and asked me to read through it and explain what the code did.

    That evening I had an offer in my inbox. This is what my parents and I had been praying about and so I immediately accepted, with the understanding I would need to wrap up other projects.

    It is amazing how God works! The best part about this job is that my supervisors are Christians. I get to work under like-minded people. I’m very excited to start part-time on the 16th and then move to full-time next month.

    It so encouraging when God answers your prayers so clearly!

    → 9:18 AM, May 11
  • Modern Times Summed Up

    “🤔 Modern times summed up in 25 seconds from the Arther PBS show. https://youtu.be/iHrZRJR4igQ” - Zorn

    → 9:35 AM, Apr 27
  • I got tired of stuggling learning VueJS and Nuxt, so I decided to build out my new blog in Jigsaw. It is almost done and I will be moving away from Micro.blog in favor of my new headless cms I’m working on (Sublime blogs).

    I have nothing against Micro.blog but I’m a control freek and I want complete control over the frontend using my static site generator of choice. I’m also trying to cut down on costs.

    → 7:19 PM, Apr 25
  • Today I started working on the marketing site/landing page for Sublime Blogs. I used tailwindui for the navigation and hero section and it turned out pretty good in my opinion. The marketing may need some work but it is a start.

    → 9:20 PM, Apr 24
  • I just spent about four hours working on Sublime Blogs and using the API to build out my blog with NuxtJS. I have been primarily focusing on building out the API. It is coming together well so far.

    → 3:08 PM, Apr 22
  • After purchasing Tailwindui, I went to town redesigning Sublime Blogs. I’m very happy with how things are coming together!

    → 7:11 PM, Apr 20
  • I’m about 2 months out from being able to ship Sublime Blogs. I’m excited 😃

    I still need to work on pricing though 🤔 How does $2/month per site sound? Maybe that includes a certain amount of storage for photos?

    → 8:34 AM, Apr 20
  • I started researching Agile and Scrum project management. I’m hoping to start implementing it with some of my client and personal projects.

    → 9:14 PM, Apr 18
  • My brother just got his motorcycle license so my dad, brother, and I went for a ride. That was a lot of fun!

    → 9:07 PM, Apr 17
  • I sold my Honda Nighthawk today. Now I’m down to one bike and a truck.

    → 9:18 PM, Apr 16
  • I want to be good at designing user interfaces, but sadly no matter how hard I try, it never ends up looking the way I want it to. So… I purchased Tailwindui. We will see if it helps.

    Now to redesign Sublime Blogs.

    → 10:20 PM, Apr 15
  • I have a strong appreciation for great UI. I find that the apps I enjoy using are also the ones that stick (like Things 3, Notion, etc.) If the UI is mediocre, I probably won’t adopt it into my workflow.

    I guess that just sets the bar a little higher more my own apps 😬

    → 4:34 PM, Apr 13
  • I downloaded the beta of 1Password 8 for iOS. The UI is amazing!

    → 4:29 PM, Apr 13
  • Time to do taxes 😞. One of the disadvantages of being self-employed is you get to watch all you money leave you all at once… every April.

    → 4:52 PM, Apr 12
  • I upgraded TPP from Laravel 8 to Laravel 9! It took about an hour to go through and update all of my dependencies, update the PHP version on the server, and change the GitHub CI/CD action to run PHP 8.1.

    This was my first time every upgrading Laravel. It was not too hard. Great job Taylor and the Laravel team!

    → 5:09 PM, Apr 9
← Newer Posts Page 2 of 5 Older Posts →
  • RSS
  • JSON Feed