<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: RomkaCodeSome</title>
    <description>The latest articles on DEV Community by RomkaCodeSome (@romkecodesome).</description>
    <link>https://dev.to/romkecodesome</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F699545%2F89ba6b94-a7e1-4800-9083-e81cf66e3024.png</url>
      <title>DEV Community: RomkaCodeSome</title>
      <link>https://dev.to/romkecodesome</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/romkecodesome"/>
    <language>en</language>
    <item>
      <title>NextJS 13 app dir with Laravel Sanctum, Valet.</title>
      <dc:creator>RomkaCodeSome</dc:creator>
      <pubDate>Mon, 24 Jul 2023 10:01:57 +0000</pubDate>
      <link>https://dev.to/romkecodesome/nextjs-13-app-dir-with-laravel-sanctum-valet-2ih4</link>
      <guid>https://dev.to/romkecodesome/nextjs-13-app-dir-with-laravel-sanctum-valet-2ih4</guid>
      <description>&lt;p&gt;Here's a simplified way to implement cookie-based authentication with Laravel Sanctum and NextJS's new app directory (app router) that can work with a Laravel Valet setup.&lt;/p&gt;

&lt;p&gt;I'll go into the details later, but here's the short version:&lt;/p&gt;

&lt;p&gt;I'll go into the details later, but here's the short version:&lt;/p&gt;

&lt;p&gt;Install a package to enable making requests from middleware:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i isomorphic-unfetch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create or update the .env.local file in the NextJS root:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;NEXT_PUBLIC_BACKEND_URL="http://localhost:8000"
NEXT_PUBLIC_BACKEND_NAMED_URL="http://testdomain.test"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If it doesn't exist already, create a &lt;em&gt;middleware.ts&lt;/em&gt; file with the following content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
import fetch from 'isomorphic-unfetch';

export async function middleware(request: NextRequest) {
  const authCookie = request.cookies.get('laravel_session');
  const base = `${request.nextUrl.protocol}//${request.nextUrl.hostname}:${request.nextUrl.port}`;

  const csrfResponse = await fetch(`${process.env.NEXT_PUBLIC_BACKEND_NAMED_URL}/sanctum/csrf-cookie`, {
    method: 'GET',
    headers: {
      'X-Requested-With': 'XMLHttpRequest',
      'Content-Type': 'application/json',
      'Accept': 'application/json',
    },
  });

  if (!csrfResponse.ok || ! authCookie?.value) {
    return NextResponse.redirect(`${base}/login`);
  }

  const response = await fetch(`${base}/api/user`, {
    method: 'GET',
    headers: {
      'X-Requested-With': 'XMLHttpRequest',
      'Content-Type': 'application/json',
      'Accept': 'application/json',
      'Authorization': authCookie.value,
    },
    credentials: 'include',
  });

  if (response.status === 401) {
    return NextResponse.redirect(`${base}/login`);
  }
}

export const config = {
  matcher: ["/((?!register|api|login|manifest|_next|styles|scripts).*)"],
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What this middleware does:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If the response to api/user is unsuccessful, it redirects to /. &lt;/li&gt;
&lt;li&gt;If a user is already logged in, it redirects from /login to /.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When developing locally with Laravel Sanctum and an SPA like NextJS or Vue, some problems may arise. All requests must originate from the same domain. Usually, a NextJS app runs on localhost:3000, while our backend is configured to run with testdomain.test. In this case, it's impossible to authenticate with cookies due to the domain difference. This is a security measure.&lt;/p&gt;

&lt;p&gt;A workaround for this is to run php artisan serve in the backend directory. You can then access the backend API via the base URL &lt;a href="http://localhost:8000"&gt;http://localhost:8000&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Another issue is that NextJS server-side requests won't work with localhost due to IPv6 issues when hitting the csrf endpoint. So, we need to get the csrf cookie like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const csrfResponse = await fetch(`${process.env.NEXT_PUBLIC_BACKEND_NAMED_URL}/sanctum/csrf-cookie`, {
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All other requests will carry the authentication cookie, so they must go through localhost. Hence, we compose the base URL like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const base = `${request.nextUrl.protocol}//${request.nextUrl.hostname}:${request.nextUrl.port}`;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To create hooks for login/register, check the official Laravel repo: &lt;a href="https://github.com/laravel/breeze-next"&gt;https://github.com/laravel/breeze-next&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Using singleton in Wordpress Sage9/10 themes view composers</title>
      <dc:creator>RomkaCodeSome</dc:creator>
      <pubDate>Sun, 05 Sep 2021 07:59:52 +0000</pubDate>
      <link>https://dev.to/romkecodesome/using-singleton-in-wordpress-sage9-themes-view-composers-5bh1</link>
      <guid>https://dev.to/romkecodesome/using-singleton-in-wordpress-sage9-themes-view-composers-5bh1</guid>
      <description>&lt;p&gt;A small pro tip for devs who are using WordPress Sage9/10 for theme development. I saw many devs doesn't realize that when you are creating &lt;strong&gt;View composer&lt;/strong&gt; and doing stuff inside it, like calling internal functions or executing SQL, for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;get_bloginfo('name', 'display')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It will be executed as many times as blade files are involved, for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;protected static $views = [
    '*',
];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Will execute &lt;code&gt;get_bloginfo()&lt;/code&gt; for all blade files.&lt;/p&gt;

&lt;p&gt;You should make use of &lt;strong&gt;singleton&lt;/strong&gt; for this, inside &lt;code&gt;ThemeServiceProvider.php&lt;/code&gt; &lt;strong&gt;register&lt;/strong&gt; method add:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$this-&amp;gt;app-&amp;gt;singleton(App::class);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Where App::class is view composer class, from now on, service container will not load this class if it's already loaded. &lt;/p&gt;

</description>
      <category>wordpress</category>
      <category>oop</category>
      <category>laravel</category>
    </item>
  </channel>
</rss>
