Next.js with a separate server - good idea?

ByteGrad
25 Mar 202422:53

Summary

TLDRDieses Video skizziert die Vorteile von Next.js als Full-Stack-Framework, das sowohl Client- als auch Server-Komponenten beinhaltet. Es erklärt die Implementierung von Server-Aktionen und Route-Handlers für traditionelle API-Routen und wie diese für Datenbankzugriff und Webhooks eingesetzt werden können. Der Sprecher diskutiert die Notwendigkeit eines separaten Servers für anspruchsvolle Projekte und die Herausforderungen von serverlosen Funktionen. Er demonstriert außerdem, wie man mit dem Authentifizierungstool 'Kind' einen sicheren Datenaustausch zwischen Next.js-Anwendungen und benutzerdefinierten Servern gewährleistet und die Vorteile eines solchen Aufbaus hervorhebt.

Takeaways

  • 😀 Next.js ist ein Full-Stack-Framework, das sowohl Client- als auch Server-Komponenten bietet.
  • 🛠️ Server-seitige Funktionen in Next.js umfassen Server-Aktionen, Route-Handler und traditionelle API-Routen.
  • 💡 Mit Next.js können traditionelle Server-Aufgaben durchgeführt werden, einschließlich Datenbankzugriff und Webhook-Verarbeitung.
  • 🔒 Für größere Projekte ist oft ein separates, benutzerdefiniertes Backend erforderlich, auch wenn Next.js bereits eingesetzt wird.
  • 🌐 Bei der Bereitstellung auf Vercel werden Next.js Seiten zu statischen Assets, während dynamische Berechnungen serverlose Funktionen zugeordnet werden.
  • ⏱️ Serverlose Funktionen haben Einschränkungen, z.B. können Aufgaben nur für eine begrenzte Zeit ausgeführt werden.
  • 🚫 Bestimmte Aufgaben, wie z.B. Video-Rendering oder Web Scraping, sind möglicherweise für Serverlose Funktionen ungeeignet.
  • 🔌 Serverlose Funktionen führen zu Verbindungsproblemen mit Datenbanken, wenn viele Anfragen gleichzeitig kommen.
  • 💨 Sie haben den Nachteil von 'cold starts', was zu Verzögerungen bei der Initialisierung führen kann.
  • 🗄️ Serverlose Funktionen bieten keinen persistenten Dateisystemzugriff, was bestimmte Anwendungen einschränken kann.
  • 🔒 Authentifizierung ist ein wichtiger Aspekt, wenn ein separates API-Server neben einer Next.js-Anwendung eingesetzt wird, und kann mit Tools wie Auth0 erleichtert werden.

Q & A

  • Was ist Next.js und welche Funktionen bietet es?

    -Next.js ist ein Full-Stack-Framework, das sowohl Client- als auch Server-Komponenten bietet. Auf der Client-Seite gibt es Client-Komponenten, während auf der Server-Seite Server-Komponenten, Server-Aktionen und Route-Handler existieren, die traditionelle API-Routen umfassen.

  • Was sind die Vorteile von Server-Komponenten in Next.js?

    -Server-Komponenten in Next.js ermöglichen die Durchführung von traditionellen Server-Seiten-Aufgaben wie Datenbankzugriff und das Empfangen von Webhooks. Sie können auch für die Server-seitige Renderung (SSR) von Seiten genutzt werden.

  • Welche Art von Projekten könnten von Next.js profitieren?

    -Einfache Projekte, die keine benutzerdefinierten Backend-Server erfordern, profitieren von Next.js, da es die meisten Server-Seiten-Aufgaben abdeckt. Jedoch für größere, komplexere Projekte ist oft ein separates, benutzerdefiniertes Backend erforderlich.

  • Was sind die Einschränkungen von Next.js-Serverless-Funktionen?

    -Next.js-Serverless-Funktionen haben Einschränkungen wie z.B. die maximale Ausführungszeit von Aufgaben, die auf bestimmten Plänen des Hostingdienstes begrenzt sein kann, sowie die Nicht-Unterstützung bestimmter Aufgaben wie z.B. lang andauernder Aufgaben oder Web Scraping.

  • Welche Probleme kann die Verwendung von Serverless-Funktionen bei der Datenbankverbindung verursachen?

    -Serverless-Funktionen können Probleme mit der Datenbankverbindung verursachen, da viele Verbindungen gleichzeitig hergestellt werden können, was eine Verwaltung der Verbindungspool erfordert. Des Weiteren können Cold Starts zu Verzögerungen führen.

  • Weshalb könnte die Verwendung eines separaten Servers neben Next.js notwendig sein?

    -Ein separates Server kann notwendig sein, um bestimmte Aufgaben wie Video-Rendering, Web Scraping oder Cron-Jobs auszuführen, die nicht ideal für die Next.js-Backend-Umgebung geeignet sind. Außerdem bietet dies Flexibilität bei der Wahl der Technologie und Trennung von Concerns.

  • Was sind die Vorteile eines benutzerdefinierten Servers im Vergleich zu Next.js?

    -Ein benutzerdefinierter Server ermöglicht es, die beste Technologie für die jeweilige Aufgabe auszuwählen, unabhängig von der Verwendung von JavaScript oder TypeScript in Next.js. Zudem kann ein Fehler im benutzerdefinierten Server nur diesen isoliert betreffen, nicht die gesamte Anwendung.

  • Wie kann man Next.js und einen benutzerdefinierten Server für die Authentifizierung verbinden?

    -Mit Authentifizierungsdiensten wie Auth0 kann man Token von Next.js für den Zugriff auf benutzerdefinierte Server routen. Diese Token müssen auf dem benutzerdefinierten Server validiert werden, um sicherzustellen, dass nur authentifizierte Anfragen durchgeführt werden.

  • Was sind die Vorteile der Verwendung von Auth0 für die Authentifizierung in Next.js und benutzerdefinierten Servern?

    -Auth0 bietet eine einfache und sichere Authentifizierungslösung, die bis zu 10.500 monatlich aktive Benutzer kostenlos unterstützt. Es ermöglicht die Erstellung und Verwendung von JWTs (JSON Web Tokens) für die Authentifizierung von Anfragen an Next.js und benutzerdefinierte Server.

  • Wie wird die Audience-Claim in JWTs zur sicheren Kommunikation zwischen Next.js und benutzerdefinierten Servern verwendet?

    -Die Audience-Claim in JWTs wird genutzt, um sicherzustellen, dass ein Token nur für diejenige API oder den Server bestimmt ist, der in der Claim angegeben ist. Dies verhindert, dass Tokens versehentlich an nicht autorisierte Endpunkte gesendet werden.

Outlines

00:00

🌐 Next.js als Full-Stack-Framework

Next.js bietet sowohl Client- als auch Server-Komponenten. Auf der Client-Seite gibt es Client-Komponenten, während auf der Server-Seite Server-Komponenten, Server-Aktionen und Route-Handler zur Verfügung stehen. Diese ermöglichen traditionelle API-Routen und ermöglichen es, Datenbankzugriffe durchzuführen, Webhooks zu verarbeiten, Zahlungen zu bearbeiten und Inhalte aus einem CMS zu aktualisieren. Server-Side Rendering (SSR) ist ebenfalls möglich. Trotz dieser Funktionen kann es für komplexe Projekte erforderlich sein, einen separaten, benutzerdefinierten Server zu verwenden, um die Funktionalität weiter auszubauen und bestehende Server-Infrastrukturen zu integrieren.

05:01

🔌 Eingebettete Server-Funktionen und ihre Grenzen

Next.js kann als traditioneller API-Server mit Route-Handlern genutzt werden, doch wenn es auf Plattformen wie Vercel bereitgestellt wird, werden Seiten vorgeneriert und statische Assets wie HTML-Dateien erstellt. Dynamische Berechnungen werden serverlos hinter den Kulissen abgebildet. Dies hat den Nachteil, dass sogenannte Serverless-Funktionen nur während der Nutzung bezahlt werden, was zu Kosten führen kann, auch wenn keine Anfragen vorliegen. Serverless-Funktionen haben auch Einschränkungen, wie z.B. eine maximale Ausführungszeit von Aufgaben, die nicht unbedingt auf diese Funktionen übertragen werden können, wie zum Beispiel Video-Rendering oder Web-Scraping. Des Weiteren können Probleme bei Datenbankverbindungen und Cold-Starts auftreten, da diese Funktionen nicht dauerhaft aktiv sind.

10:02

🛠️ Die Notwendigkeit eines benutzerdefinierten Servers

In der Praxis kann es erforderlich sein, einen benutzerdefinierten Server neben der Next.js-Anwendung zu haben, um bestimmte Aufgaben auszuführen, die nicht auf der Server-Seite von Next.js möglich sind. Beispielsweise kann ein separates Node.js-Server für Video-Rendering oder Web-Scraping verwendet werden. Auch Chron-Jobs oder Job-Queues können besser in einem separaten Server umgesetzt werden. Zudem bietet die Verwendung eines benutzerdefinierten Servers die Flexibilität, die beste Technologie für die jeweilige Aufgabe auszuwählen, anstatt auf JavaScript oder TypeScript zu beschränken.

15:04

🔒 Authentifizierung in einer Next.js- und benutzerdefinierten Server-Architektur

Die Authentifizierung ist ein wichtiger Aspekt bei der Integration eines benutzerdefinierten Servers in eine Next.js-Anwendung. Es ist notwendig, sicherzustellen, dass sowohl Anfragen von der Next.js-Anwendung als auch direkt von der Client-Seite aus auf den benutzerdefinierten Server gesendete Anfragen authentifiziert sind. Die Verwendung von Authentifizierungs-Token ist hierfür essentiell. Das Video skizziert, wie man mit Hilfe von Kind, einer Authentifizierungs-Plattform, die Authentifizierung für Next.js und einen benutzerdefinierten Express-API-Server einrichtet, wobei die Tokens für die Kommunikation zwischen den beiden Servern verwendet werden.

20:05

🔗 Verknüpfung von Next.js mit benutzerdefiniertem Server und Authentifizierung

Das Video demonstriert, wie man eine Next.js-Anwendung mit einem benutzerdefinierten Express-API-Server verknüpft und die Authentifizierung zwischen beiden sicherstellt. Dies geschieht durch die Verwendung von Kind, einer Authentifizierungs-Plattform, die sowohl für die Next.js-Anwendung als auch für den benutzerdefinierten Server verwendet wird. Die Tokens, die von Kind generiert werden, ermöglichen die sichere Kommunikation zwischen den beiden Servern. Zusätzlich wird gezeigt, wie man die Authentifizierung auf Client-Seite und Server-Seite in Next.js implementiert, um auf geschützte Routen zuzugreifen.

🏢 Mikroservices-Architektur und unabhängige Bereitstellung

Das Video diskutiert die Vorteile einer Mikroservices-Architektur im Kontext von Next.js und benutzerdefinierten Servern. Es wird betont, dass separate Server unabhängig voneinander bereitgestellt werden können, was Flexibilität bei der Entwicklung und Wartung bietet. Dies ermöglicht auch die Zusammenarbeit mehrerer Teams an verschiedenen Teilen der Anwendung. Die Authentifizierung bleibt dabei ein zentrales Thema, da sie für den sicheren Datenaustausch zwischen den verschiedenen Diensten erforderlich ist.

🔄 Implementierung von Authentifizierung auf Client- und Server-Seite

Das Video zeigt, wie man die Authentifizierung auf Client- und Server-Seite in einer Next.js-Anwendung implementiert. Es wird gezeigt, wie man mit Hilfe von Kind Tokens für die Authentifizierung verwendet und wie man diese in Server-Komponenten und Client-Komponenten überträgt. Die Verwendung von Middleware zur Überprüfung von Tokens auf dem benutzerdefinierten Server wird ebenso behandelt, um sicherzustellen, dass nur authentifizierte Anfragen auf geschützte Ressourcen zugreifen können.

🔒 Zusätzliche Sicherheitsmaßnahmen durch Audience-Claim

Das Video erläutert, wie man die Sicherheit der Authentifizierung durch die Verwendung von Audience-Claims erhöht. Hierbei wird sichergestellt, dass Tokens nur für bestimmte APIs oder Dienste gültig sind. Dies wird durch die Registrierung von APIs in der Kind-Plattform und die Überprüfung der Audience-Claims in den Middleware-Funktionen auf dem benutzerdefinierten Server erreicht.

👋 Schlussfolgerung und Dankesagung

Das Video schließt mit einer Zusammenfassung der Vorteile einer solchen Architektur und der Bedeutung der Authentifizierung. Es wird betont, wie wichtig es ist, die Authentifizierung richtig zu implementieren, um eine sichere Kommunikation zwischen verschiedenen Diensten zu gewährleisten. Der Sprecher bedankt sich für die Aufmerksamkeit des Zuschauers und teilt mit, dass er als Brand Ambassador für Kind tätig ist, was seine positive Einstellung zur Verwendung dieser Plattform erklärt.

Mindmap

Keywords

💡Next.js

Next.js ist ein vollständiges Stakk-Framework, das sowohl für die Client- als auch für die Server-Seite verwendet werden kann. Im Video wird Next.js als Grundlage für die Entwicklung einer Webanwendung beschrieben, die sowohl Client- als auch Serverkomponenten umfasst. Es wird verwendet, um eine Vielzahl von Aufgaben auf der Server-Seite auszuführen, einschließlich des Zugriffs auf Datenbanken und der Verarbeitung von Webhooks.

💡Server-Komponenten

Server-Komponenten in Next.js sind eine Möglichkeit, Code auf dem Server zu rendern, bevor er an den Client gesendet wird. Im Kontext des Videos werden sie als Teil der Server-Seite von Next.js genannt, die für die Durchführung traditioneller Server-Seitenaufgaben verantwortlich ist, wie zum Beispiel der Interaktion mit Datenbanken.

💡Server-Aktionen

Server-Aktionen in Next.js ermöglichen es, Code auf dem Server auszuführen, der reagiert auf bestimmte Ereignisse oder Anfragen. Sie werden im Video als Mechanismus vorgestellt, um Aufgaben auf der Server-Seite von Next.js durchzuführen, die normalerweise für die Verarbeitung von Daten oder die Reaktion auf Anfragen erforderlich sind.

💡Route-Handler

Route-Handler sind in Next.js verwendet, um API-Routen zu definieren, die auf Anfragen reagieren. Im Video werden sie als traditionelle API-Routen beschrieben, die für die Verarbeitung von Anfragen und die Interaktion mit externen Systemen, wie zum Beispiel CMS oder Zahlungssystemen, verwendet werden.

💡Serverless-Funktionen

Serverless-Funktionen sind in Next.js hinter den Kulissen verwendet, um Seiten vorzubereiten und dynamisch zu rendern. Im Video wird darauf hingewiesen, dass Next.js in der Lage ist, statische Assets wie HTML-Dateien bereitzustellen und dynamisch compute-intensive Aufgaben auf Serverless-Funktionen zu mappen, was Kosteneffizienz und Skalierbarkeit bietet, jedoch auch Einschränkungen wie die maximale Ausführungszeit einer Funktion hat.

💡Custom Server

Ein Custom Server ist ein benutzerdefinierter Server, der neben Next.js verwendet werden kann, um Anforderungen zu erfüllen, die innerhalb von Next.js nicht möglich sind. Im Video wird die Notwendigkeit eines Custom Servers für Projekte diskutiert, die bestimmte Anforderungen haben, die Next.js nicht erfüllen kann, wie zum Beispiel die Verarbeitung von lang andauernden Aufgaben.

💡Microservices-Architektur

Die Microservices-Architektur ist ein Ansatz, bei dem eine Anwendung in kleine, unabhängige Dienste unterteilt wird, die sich gegenseitig über Netzwerke kommunizieren. Im Video wird die Vorstellung eines Custom Servers als Teil einer Microservices-Architektur vorgestellt, um die Flexibilität und Skalierbarkeit einer Anwendung zu erhöhen.

💡Authentication

Authentication ist der Prozess, bei dem die Identität eines Benutzers überprüft wird. Im Video wird die Implementierung von Authentifizierungsmechanismen in Next.js und Custom Server-Umgebungen diskutiert, um sicherzustellen, dass nur autorisierte Benutzer auf geschützte Ressourcen zugreifen können.

💡JSON Web Tokens (JWT)

JSON Web Tokens sind ein Standard, der verwendet wird, um Informationen zwischen Parteien sicher zu übermitteln. Im Video werden JWTs als Mittel zur Authentifizierung von Anfragen an den Custom Server beschrieben, indem sie in den HTTP-Headern übermittelt werden und auf dem Server überprüft werden.

💡KIND

KIND ist eine Authentifizierungslösung, die im Video verwendet wird, um Authentifizierung für Next.js-Anwendungen und Custom Server zu verwalten. KIND bietet SDKs für verschiedene Anwendungstypen und wird im Video als einfache Lösung für die Verwaltung von Authentifizierungsprozessen und Tokens präsentiert.

Highlights

Next.js ist ein Full-Stack-Framework, das sowohl Client- als auch Server-Komponenten bietet.

Server-seitige Funktionen umfassen Server-Aktionen und Route-Handler für traditionelle API-Routen.

Mit Next.js können traditionelle Server-Aufgaben durchgeführt und auf Datenbanken zugegriffen werden.

Für komplexe Projekte wird oft ein separates, benutzerdefiniertes Backend benötigt.

Next.js kann als traditioneller API-Server mit Route-Handlern genutzt werden, einschließlich SSR für Seiten.

Einige Aufgaben sind auf der Server-Seite von Next.js nicht durchführbar, insbesondere bei Verwendung von Serverless-Funktionen.

Serverless-Funktionen haben Vor- und Nachteile, einschließlich Timeouts für laufende Aufgaben und Kosten für未使用的 Ressourcen.

Längere Aufgaben wie Video-Rendering können aufgrund von Timeout-Einschränkungen nicht auf Next.js-Backends durchgeführt werden.

Für Hintergrundaufgaben oder Cron-Jobs kann ein separates Server besser geeignet sein als ein Next.js-Backend.

Serverless-Funktionen haben Einschränkungen hinsichtlich des Zugriffs auf einen persistenten Dateisystem.

WebSockets können in Next.js-Backends nicht ohne Weiteres implementiert werden.

Das Hosten von Next.js auf einem lang andauernden Server kann Komplexität verlieren, aber bestimmte Anwendungsfälle erfüllen.

Ein benutzerdefiniertes Server kann die Wahl der besten Tools für die spezifischen Anforderungen eines Projekts ermöglichen.

Ein separates Server ermöglicht die Isolation von Fehlern und unabhängige Bereitstellung von Komponenten.

Mehrere Teams können auf einem Projekt mit separaten benutzerdefinierten Servern unabhängig arbeiten.

Authentifizierung kann in einer Next.js-Anwendung mit Hilfe von Kind, einer Authentifizierungs-Plattform, implementiert werden.

Kind bietet SDKs für Next.js und benutzerdefinierte Server, um Authentifizierung und geschützte API-Routen zu verwalten.

Geschützte API-Routen können mit Middleware von Kind, die Json Web Tokens verifiziert, gesichert werden.

Die Verwendung von Audience-Claims in Json Web Tokens ermöglicht es, Tokens für bestimmte APIs einzuschränken.

Transcripts

play00:00

next JS is a full stack framework so we

play00:02

get both a client side as well as a

play00:04

server side so on the client we have

play00:06

client components and on the server side

play00:07

we have server components these days

play00:09

server actions as well as route handlers

play00:12

these are basically your traditional API

play00:13

routes and with any of these three

play00:15

options that you have on the server side

play00:17

you can do the traditional server side

play00:18

tasks you can access your database and

play00:21

any of these you can receive web hooks

play00:23

with these route handlers so you can

play00:25

process payments update your content

play00:27

from your CMS you can even use next Js

play00:30

just as your traditional API server with

play00:33

these route handlers you can also SSR

play00:35

your pages Etc so it covers most of the

play00:38

things that you would want to do on the

play00:39

server side and if you have a simple

play00:41

project that's probably enough but I

play00:43

found that in the real world if you're

play00:44

building a serious project pretty much

play00:46

always at some point you need a separate

play00:49

server a custom back end and in fact you

play00:52

may actually already have a custom

play00:53

server and you're thinking about moving

play00:55

to nextjs but you don't want to leave

play00:57

your already existing server behind you

play00:59

want to keep it that way and so you may

play01:01

have an architecture like this where you

play01:03

have your nextjs app which is basically

play01:06

this entire party is one next s app and

play01:08

then also a custom Ser maybe an Express

play01:11

API maybe PHP Ruby go python pick your

play01:14

flavor but even if you don't have a

play01:16

custom server yet you may still want

play01:18

that in fact you may need it at some

play01:20

point because some of the things you

play01:22

simply cannot do on the server side of a

play01:24

nextjs application because when you

play01:26

deploy it to for sale for example what

play01:29

you get is essentially a bunch of

play01:30

serverless functions behind the scenes

play01:32

right so your pages will be

play01:34

pre-generated as much as possible so you

play01:36

get a bunch of static assets like HTML

play01:38

files but anything that requires Dynamic

play01:40

compute will be mapped behind the scenes

play01:42

for you on forel to a serverless

play01:45

function and that's different from your

play01:46

traditional server which is what people

play01:48

call a long running server so this one

play01:50

will be running even if there are no

play01:52

requests it's still running and that's

play01:54

the downside of them you're paying even

play01:56

if you're not using them but serverless

play01:57

functions you're only paying for what

play01:59

you use and there are other benefits as

play02:01

well but there are also downsides with

play02:02

these serverless functions for example

play02:04

any task May simply not be able to run

play02:07

on a serverless functions so for example

play02:09

if you deploy to forel and you are on

play02:11

the hobby plan a free plan you can

play02:12

actually only run task take at most 10

play02:15

seconds so if it takes longer you'll get

play02:16

a timeout error now if you upgrade to

play02:18

the Pro Plan by default it's going to be

play02:20

15 seconds but you can actually maximize

play02:22

this all the way to 5 minutes which is

play02:24

already substantially more than what it

play02:25

used to be but there will still be some

play02:27

tasks that are going to be too long for

play02:28

that for example I was actually building

play02:30

a startup which was rendering videos I

play02:32

was using the remotion library that

play02:34

allows you to create videos with react

play02:36

so what this will do is it will stitch

play02:38

together the video but it may take some

play02:39

time may take you know if it's a longer

play02:41

video may take 20 30 minutes so I cannot

play02:43

run that on the nextjs back end I need

play02:46

to do that on a separate back end so I

play02:48

created a separate nodejs server for

play02:50

that and there are other tasks as well

play02:51

like web scraping that may simply take

play02:53

too long also if you have background

play02:54

tasks or like a a job queue or Chron

play02:57

jobs this may not fit properly within a

play02:59

next next as backend this may be more

play03:01

appropriate to spin out into a separate

play03:03

server as well also the way that

play03:05

serverless functions work is you

play03:06

essentially spin up a lot of them as

play03:08

more and more requests are coming in and

play03:09

they all will make a connection to the

play03:11

database in case you're accessing the

play03:12

database so you may actually also run

play03:14

into issues databased connections to

play03:16

many of them you need to manage the pool

play03:18

of connections they also suffer from

play03:20

cold starts because you're only paying

play03:23

for what you use so as you don't use

play03:25

them they're not going to be up and

play03:26

running that means that once you do

play03:28

start getting requests again may take

play03:30

some time before they're up and running

play03:31

and it could be a little bit slower in

play03:33

the beginning now it's not a huge

play03:34

problem it affects a small percentage of

play03:36

the total amount of request but still

play03:38

something you may run into you also

play03:40

don't have a persistent file system so

play03:42

these serverless functions they don't

play03:43

share the same file system so that means

play03:46

if you're using SQL light for example

play03:48

which is essentially just a file it's

play03:50

part of your file system you cannot use

play03:52

that when you deploy it to forell or if

play03:53

you're just using a Json file to manage

play03:56

data that's not going to work and there

play03:57

are other things as well like websockets

play03:59

that may not really work out of the box

play04:00

in a nextjs back end now some of these

play04:03

things can be worked around but it's

play04:04

just not ideal to put this in a nextjs

play04:07

backend you can technically also host

play04:09

nextjs yourself so you can actually also

play04:11

run it on a long running server it does

play04:13

have more complexity and you may lose

play04:16

some features that could be an option

play04:17

that solves it for your particular use

play04:19

case but most of you will just simply

play04:21

want to have a separate custom server

play04:24

because there are other benefits to that

play04:25

as well for example if you have a custom

play04:27

server you can pick the best tool for

play04:29

the the job so if you have a project

play04:31

that has a lot of machine learning or AI

play04:33

or data processing you may want to use

play04:35

Python because python is more popular

play04:37

for data science type of work or maybe

play04:39

you want to use go for performance or

play04:41

maybe your developers simply have

play04:42

expertise in a different technology so

play04:45

why not use that expertise you can pick

play04:46

any technology you want for this

play04:48

separate server with nextjs you have to

play04:50

use JavaScript or typescript also what

play04:52

if I make a mistake in one of my backend

play04:55

features harm the entire app in nextjs

play04:58

but a custom server if I make a mistake

play05:00

there well it's just it's isolated to

play05:02

just that service we can also deploy

play05:05

this independently of the rest of the

play05:07

application with nextjs if you make a

play05:09

change in a server action or in a server

play05:11

component or route Handler you have to

play05:13

redeploy the entire app imagine you

play05:15

actually have separate teams of people

play05:16

working on the app you're all working in

play05:18

the same app whereas with a custom

play05:19

server you can actually have multiple

play05:21

custom servers and each team could be

play05:23

working on one of them so you get all of

play05:25

these benefit right so I should also put

play05:27

here this is simply a more ergonomic

play05:29

setup I think if you have different

play05:30

teams working on the same project now if

play05:32

you have a background in microservices

play05:35

you probably recognize all of this I'm

play05:37

essentially rediscovering the benefits

play05:39

of having a microservices architecture

play05:41

now you may just want to have one custom

play05:43

server next to your nextjs application

play05:46

and you can still deploy this to for

play05:47

sale by the way so you can also deploy

play05:49

node.js or Express API to for sale now

play05:51

the biggest problem you're going to run

play05:53

into I found was authentication actually

play05:55

so there are solutions to add

play05:57

authentication to the nextjs application

play05:59

but how does that work if you also want

play06:01

to have let's say a separate API server

play06:04

and those API routes also need to be

play06:06

protected so what we want is that if we

play06:07

make a request from our nextjs server

play06:10

site to our custom server we need to be

play06:12

authenticated So This Server site will

play06:14

need to send some token let's say to our

play06:17

custom server custom server can then

play06:19

verify that the token is valid and we

play06:22

may actually also want to send requests

play06:24

directly from the client side to the

play06:27

custom server as well which then also

play06:29

should have a valid access token and

play06:31

what we of course also want is simply

play06:33

within our nextjs app our nextjs server

play06:35

sign may actually also do things like

play06:37

access the database and that of course

play06:39

should also be authenticated so here of

play06:41

course also the client when it sends

play06:43

requests to the back end we also want to

play06:45

make sure that only authenticated users

play06:47

can do that so I'll quickly show you how

play06:49

to do that I'm going to use kind I'm a

play06:51

brand ambassador for kind they have a

play06:52

wonderful solution for authentication

play06:54

and it's free up to 10.5k monthly active

play06:57

user so we have two things here we have

play06:59

the entire nextjs application we can use

play07:02

the nextjs app router SDK by kind and

play07:05

then we have let's say a separate Xpress

play07:08

API server now K actually also has an

play07:10

SDK for that but we only need the app

play07:13

router SDK to create a token and so on

play07:16

and then we can reuse that token to make

play07:18

authenticated requests to our separate

play07:20

server so we don't need both sdks we

play07:23

only need one now I like using the

play07:25

nextjs app router SDK because it also

play07:27

helps us with some utilities on the the

play07:29

client now just to show you how you

play07:31

could set that up in a folder structure

play07:33

right so here I have an API folder where

play07:35

I put my Express API server so all of

play07:37

the express API server stuff goes in

play07:39

here then I have a web folder for my

play07:42

entire nextjs app right so my entire

play07:44

nextjs app just as you would normally

play07:46

use it is all in here and then you can

play07:48

also have a shared folder right so if we

play07:50

have a database here of course my nextjs

play07:52

server site needs to access the database

play07:54

but my custom server may also need to

play07:56

update the database right so here in the

play07:58

shared folder you may have some database

play08:00

related things that you can use in both

play08:02

your custom server as well as in your

play08:04

nextjs application so let me show you

play08:06

the nextjs application as it is right

play08:08

now so here is my nextjs application you

play08:11

can see it's just a pretty standard

play08:12

boiler plate here if I open up the

play08:14

source and if I go to my homepage here I

play08:17

just have this sample app welcome to

play08:19

stock prices and here is what you would

play08:21

see this is the homepage welcome to

play08:23

stock prices log in to VI stock prices

play08:25

and new so let's say we have a dashboard

play08:28

route that should actually be protected

play08:30

where you can view the stock prices and

play08:32

the news right now if I go to/ dashboard

play08:34

I can just go there I'm not

play08:36

authenticated we have zero

play08:37

authentication right now and that's this

play08:39

page here under dashboard you can see

play08:41

here I have my dashboard page and this

play08:43

is the home page this should be public

play08:45

but this should be private so this is

play08:47

all just nextjs right so if we go back

play08:49

to the diagram here what we're doing

play08:51

here is we just have two pages those are

play08:53

actually server components in xjs so

play08:55

those two pages are just here so what we

play08:57

want to do is just protect this first so

play08:59

we we can just use this one app router

play09:01

SDK so in the dashboard I can register

play09:03

my application here with kind I can call

play09:05

that stock prices now next JS is

play09:07

actually considered here a backend web

play09:09

application and then here we have a

play09:11

quick start I will just pick nextjs of

play09:13

course all right now we already created

play09:15

an App so it's existing codebase where

play09:17

is your project running so this is

play09:18

something you need to pay attention to

play09:19

if you have multiple apps running if you

play09:21

would run your Express server let's say

play09:23

on Local Host 3000 then your next GS app

play09:25

is probably running on some other server

play09:27

I am actually running this on Local Host

play09:29

3,000 I need to set these call back URLs

play09:32

boom boom I need to install this package

play09:34

I already did that I need to add my

play09:36

environment variables right so here in

play09:37

my nextjs application I have .lo I'm

play09:40

going to paste that right here all right

play09:42

let's see all right now within nextjs we

play09:45

also have these route handlers right so

play09:47

your API routes essentially this is all

play09:49

just within nextjs has nothing to do

play09:51

with our custom server we do need to add

play09:53

that here as well so I will copy this

play09:56

path and then here I can create a new

play09:58

folder and then in there we need route.

play10:01

JS and I will just copy this and paste

play10:04

that right here right this is just a

play10:06

bunch of setup all for our nextjs

play10:08

application okay and that should

play10:09

basically be it now we can add sign up

play10:11

and sign in buttons so here what we want

play10:13

to do of course is we want to protect

play10:15

this page the user needs to be signed in

play10:17

so let's actually try protecting this

play10:19

before we add sign in and login buttons

play10:21

so if I go to the dashboard page this is

play10:23

a server component in kind if we want to

play10:25

protect this we can do it right in the

play10:27

server component or we can create a

play10:29

middleware file right middleware also

play10:31

only runs within our nextjs application

play10:33

has nothing to do with our separate

play10:34

custom server so they also show you how

play10:36

to do this with middleware so you can

play10:38

easily protect many different pages in

play10:40

one go so in next this is all all within

play10:42

next JS right so here I would create a

play10:44

middleware dots file and then here I can

play10:47

determine which pages should be

play10:49

protected so here we only want to

play10:50

protect the dashboard page we could do

play10:52

it manually for each page as well with

play10:54

get kind server session I have a

play10:56

complete tutorial on kind so check that

play10:58

out so here middle where right now I'm

play11:00

just protecting dashboard so if I do

play11:01

this I'm not logged in so you can see

play11:03

actually automatically when I try to go

play11:04

there we actually already have a

play11:06

redirect there so if I go to the

play11:07

homepage this is still public if I try

play11:09

to go to dashboard we should be

play11:11

redirected to the login page hosted by

play11:13

kind now it will say something about not

play11:15

being able to authenticate because we

play11:17

haven't decided yet how the user should

play11:18

be able to authenticate so if we go to

play11:20

our application here we can decide how

play11:22

the user should be able to log in maybe

play11:24

with their password passwordless with

play11:26

their phone I'm going to use GitHub here

play11:28

and I will save that right here all

play11:29

right successfully saved if I just try

play11:32

accessing dashboard now again I'm not

play11:33

logged in so automatically I will be

play11:35

redirected to the login page and now I

play11:37

can log in with GitHub I haven't created

play11:39

an account yet so it will prompt me to

play11:40

create an account and now after logging

play11:42

in with GitHub I'm redirected here to

play11:44

dashboard so now I'm logged in and you

play11:46

can see indeed I have my access token

play11:48

here now this is all within nextjs now

play11:50

of course we want to make these requests

play11:53

to our custom server which is also going

play11:54

to be protected and we need to send

play11:56

along this access token to there so we

play11:58

have now protected our nextjs

play12:00

application there are there are many

play12:02

other things we can do here but you can

play12:03

see now when I want to access a

play12:05

particular page that should be protected

play12:07

which is just a server component

play12:09

actually in nextjs and when I try to go

play12:10

there without being logged in I'm

play12:11

automatically redirected to kind and

play12:13

after login I can actually access that

play12:16

pit so now we have set up authentication

play12:18

for our nextjs application but now what

play12:20

if we have a custom server let's take a

play12:22

look there I'm going to close out of

play12:23

this I'm going to close the web folder

play12:25

here let's say we have an Express API

play12:27

server and in here here we have well

play12:30

let's see we have a setup for very

play12:31

simple Express server and so course is

play12:34

of course one of the things you want to

play12:35

take a look at I'm just going to allow

play12:37

everything here and but here we have an

play12:38

API route and here we have API protected

play12:41

and let's say this holds some protected

play12:43

stock prices data how do we make sure

play12:45

that only logged in users can access

play12:48

this API route or at least get back a

play12:50

response with this only when they're

play12:52

logged in so what we're going to do is

play12:54

we're going to make a request from our

play12:55

nextjs server back end to our API route

play12:58

here and then we'll also try to do it

play13:00

directly from the client as well let's

play13:02

try doing this let's try making a

play13:04

request somewhere from our nextjs back

play13:06

end to our API server here so let's see

play13:08

let's go to our nextjs application as

play13:11

well our dashboard page is a server

play13:13

component right so this is server site

play13:16

right so if we look at the diagram one

play13:17

more time that's a server component

play13:18

that's running within the nextjs server

play13:20

site now from here we're going to make a

play13:22

request to that APN R so in the server

play13:24

component if you watch my channel you

play13:26

know that we can do async A8 directly in

play13:28

here without using effect I can make a

play13:30

fetch call directly in the function body

play13:32

here right and we get a response from

play13:33

that so my nexts application is running

play13:35

on Local Host 3000 but the API is

play13:38

running on 3001 and the route to there

play13:41

was API protected so this hopefully

play13:44

gives us some response and let's

play13:45

actually try logging that so console log

play13:48

data and let's see what we get so here

play13:49

on the left side I have my nextjs app

play13:52

running on the right side I have my

play13:53

Express API running remember this is a

play13:56

server component so if you log something

play13:58

you need to open up your ter Al it's not

play13:59

going to be in the browser tools that's

play14:01

client component and actually you can

play14:02

already see I get a message here this is

play14:04

protected stock prices data now how is

play14:07

that possible because I'm not passing

play14:08

along any token so right now this is not

play14:11

protected this API route here is just

play14:13

sending this back to whoever makes this

play14:15

request so if we want to protect this

play14:16

API route we can very easily do that

play14:18

with kind as well so kind actually also

play14:21

gives you a Json web token fifier that

play14:24

you can use here as middleware I can

play14:26

actually just import this from kind's

play14:28

other packet which is called kind note

play14:30

Express the only thing we need from here

play14:32

is actually just this utility they have

play14:34

to verify Json web tokens so here I can

play14:36

then create the middleware we use the

play14:38

imported Json web token verify and here

play14:41

you need to give the domain that you

play14:43

used with kind so this is going to be

play14:45

something like this it's kind's domain

play14:46

but it's going to be your own name in

play14:48

the as the subdomain here now there is

play14:50

something called the audience claim as

play14:52

well that we want to verify and we'll

play14:54

talk about it in a second for now I will

play14:56

just leave this empty this middleware

play14:58

when there is a request coming into this

play14:59

route it will go through the middleware

play15:01

first and it will pull out the request

play15:03

headers and specifically it's going to

play15:05

look for a bearer token you can do that

play15:07

manually as well right so you would do

play15:09

something like request headers

play15:10

authorization and you can grab you can

play15:12

grab the beer token from the incoming

play15:14

request like this yourself and then

play15:16

verify it yourself however here it's a

play15:17

bit easier it will do all of that for

play15:19

you you just plop it in there so now

play15:21

when I try to access that API route like

play15:24

this from our own nextjs server let's

play15:26

see what happens I just need to invoke

play15:28

this and again let's go to our dashboard

play15:29

page so that it will run again now if

play15:31

you keep making request it may seem that

play15:33

you can still access the API route even

play15:35

though we are not passing along the

play15:37

access token so how is that possible

play15:39

well actually it is not possible nextjs

play15:41

actually caches This Server component so

play15:44

if you invoke it again it may actually

play15:46

use a cache from before so that's why we

play15:48

get the same results here so what I like

play15:50

to do is sometimes you kind of have to

play15:52

delete this next folder here so if I

play15:54

delete this and if I just restart the

play15:56

server here the nextjs server now it's

play15:59

restarted if I now invoke This Server

play16:01

component again if I refresh here let's

play16:03

see what we get see so yeah so now you

play16:05

can see we indeed get something here

play16:08

with forbidden and actually here in our

play16:09

Express server we also see something

play16:11

here so here you can see Json web token

play16:13

parse error empty Json web token so

play16:16

we're not passing along any Json web

play16:18

token and so what this middleware will

play16:19

do it will return a response saying for

play16:21

a one error forbidden right so that's

play16:23

why now we're getting this error here so

play16:25

now we have protection in place here on

play16:27

our custom server you need to f a token

play16:29

now right so now when we make a request

play16:31

here and we don't have that we get that

play16:33

error so now of course we want to send

play16:35

that along right so and actually my

play16:36

co-pilot wants to get it from local

play16:38

storage which is not possible because

play16:39

this is a server component there is no

play16:41

local storage but how do we get the

play16:42

access token here then well on the

play16:44

server side anywhere on the server with

play16:46

kind you can use get kind server session

play16:49

so this allows you to get the excess

play16:51

token on the server side all right and

play16:52

actually I just need to add the at in

play16:54

front of it so here we get all sorts of

play16:56

things now what we want is to get the

play16:57

access token but we don't want it in

play16:59

decoded format because we want to send

play17:02

this along so here we want to get the

play17:03

raw access token so then here we get the

play17:07

access token we now that's a function so

play17:09

we do need to call that to get the

play17:10

actual token get access token raw so

play17:13

then here we get the actual access token

play17:15

which we can then pass along right so

play17:17

here we just plop this in there now if I

play17:19

save here now we are passing along the

play17:22

access token to our server and let's

play17:24

actually try logging the access token to

play17:26

double check that we actually get an

play17:28

access token here I will comment out the

play17:31

actual fetch call for now so here of

play17:33

course we need to await this so This is

play17:34

actually a promise and if we do that and

play17:36

now save you can see we are indeed

play17:38

getting this raw access token right so

play17:40

here you can see it's not decoded that's

play17:42

what we need to pass along the API

play17:44

server right so now I'm passing this

play17:46

along as the access token here and now

play17:48

you can see it says something about this

play17:50

is protected stock prices data so now if

play17:53

I go down here on the express server as

play17:55

well you can see we get a message here

play17:56

token is valid so now we have

play17:58

successfully made a request from our own

play18:01

nextjs backend to a custom server which

play18:04

is still protected all right so now what

play18:05

do we want to make a request directly

play18:07

from a client component to this custom

play18:09

server well it would be very similar so

play18:11

let's actually turn this into a client

play18:13

component we can do that by just adding

play18:15

use client typically not recommended for

play18:17

the entire page of course but let's see

play18:19

if we can change this a little bit to so

play18:21

this is not going to work so get kind

play18:23

server session that's all for Server

play18:24

sites whether it's server component

play18:26

server action route Handler that doesn't

play18:28

work on the client side so we can also

play18:29

not make this async so what we can use

play18:31

on the client side is use kind browser

play18:34

which gives us similar things here on

play18:36

the client so here we get access to all

play18:39

of this right so we have all of these

play18:41

properties here now what we want is

play18:43

again get access token not the decoded

play18:45

one the raw one because that's what we

play18:47

want to send along just to quickly log

play18:49

this to the console now we actually need

play18:51

to take a look in the browser so if I

play18:53

refresh here let's see we see access

play18:55

token raw and here you can see now we

play18:57

have our access token on the clock now

play18:59

if we make a fetch call to our API

play19:01

server let's say we're doing some old

play19:03

school fetching with use effect well you

play19:05

would end up with something like this so

play19:07

here you have you would create a

play19:09

function get data so here we would make

play19:11

a fetch call again to same route we pass

play19:13

along the token as part of the

play19:15

authorization header and we will log the

play19:17

actual response so if I save here and

play19:20

now if I scroll up a bit Yeah you can

play19:21

see here I get message this is protected

play19:24

stock prices data so we got a valid

play19:26

response here and indeed here we also

play19:27

see here from the Express server that

play19:29

the token was valid so we use the nextjs

play19:31

SDK it will give us a token and then we

play19:34

can just pass along the token to our

play19:36

custom server which is protected with

play19:38

middleware that's also coming from kind

play19:40

now there's one more thing that we can

play19:41

do to make it safer which is to also

play19:43

work with the audience claim because

play19:45

it's just checking if the token is valid

play19:48

right so when we log in with the next

play19:49

chance SDK it will give us some token

play19:51

but can we just use that token

play19:52

everywhere well no we want the token to

play19:54

only work for this specific custom

play19:57

server so you can use the audience claim

play19:59

for that so in kind I can go to settings

play20:01

and all the way here at the bottom

play20:02

there's API so you can register your

play20:04

apis with kind and actually I already

play20:07

did this so typically in a Json web

play20:09

token you would then see the audience

play20:10

claim typically it's a URL but it's not

play20:12

going to be called or anything but it's

play20:14

just typically a URL since my Express

play20:16

server is running on Local Host 3001

play20:18

that's what I would use to make it work

play20:19

with the audience claim we do need to

play20:20

add this environment variable here so it

play20:22

needs to be the exact same as how you

play20:24

registered is in your Cent dashboard so

play20:26

this token that's going to be created is

play20:27

going to be meant to be consumed by our

play20:30

API server which is locally at least

play20:33

running on Local Host 3001 and then I

play20:35

want to connect this API to this

play20:38

application nextjs application I have

play20:40

here so very easy to do just one click

play20:42

boom now the separate custom server is

play20:45

sort of connected with my nextjs

play20:47

application so the next time that a

play20:49

token is created in that token there

play20:51

will be an audience claim so let's

play20:53

actually do that let's go back to our

play20:54

application here I'm going to log myself

play20:56

out uh kind of course has login log out

play20:58

buttons as well I have a complete

play21:00

tutorial on kind like I said so here on

play21:01

the dashboard page we could have a log

play21:03

out and I will import that and we will

play21:06

say something like log out okay so we

play21:09

need to click on that so here if I make

play21:10

this a little bit bigger you can see log

play21:12

out if I click on that you can see my

play21:14

cookies are going to become cleared on

play21:15

the homepage we might as well quickly

play21:17

add some login and register buttons as

play21:19

well login link and there's also a

play21:21

register link very easy so now here I

play21:23

can click on log in to log myself in and

play21:26

get a token again I'm going to log in

play21:28

with GitHub again all right so now I'm

play21:30

back in the dashboard page now I have a

play21:32

new token and of course it's encoded

play21:34

here but if we actually decode it here

play21:36

decoded access token so we can see the

play21:38

audience claim so if I save this here

play21:40

here you can see now I have an access

play21:42

token all right so now here when we log

play21:44

this here I get the decoded access token

play21:47

you can see in the audience claim there

play21:49

is now this URL for our API server so

play21:52

this token is meant for this URL that's

play21:54

how you can view it so then here on the

play21:56

API server here we also we have this

play21:59

audience option here and we can specify

play22:01

that we only want to allow tokens that

play22:03

explicitly have that Local Host 3001 in

play22:06

the audience claim so then here when we

play22:08

make request let's see if we can still

play22:10

get the data so I'm going to refresh

play22:12

here and you can see I'm still getting

play22:14

the data here so now this is protected

play22:16

even better there may be other checks

play22:18

that you want to do here like check for

play22:20

the Scopes but that's a topic for

play22:21

another day this was an example of how

play22:23

you can set up your architecture with a

play22:25

custom server next to your nextjs

play22:27

application in including authentication

play22:29

which is which is typically the hard

play22:31

thing to do right in a setup like this

play22:33

but you can see with kind that's very

play22:34

easy to solve so I'm Wesley I'm a brand

play22:36

ambassador for kind so they are

play22:37

sponsoring me but even if they weren't I

play22:39

would still use their solution for my

play22:41

own nextjs applications because

play22:43

authentication is just very hard to do

play22:45

properly yourself especially once you

play22:46

start deviating from the standard path

play22:48

hopefully you found it an interesting

play22:49

video thanks for watching and I hope to

play22:51

see you the next one have a nice day

Rate This

5.0 / 5 (0 votes)

関連タグ
Next.jsbenutzerdefinierter ServerAuthentifizierungKindServerless-FunktionenMicroservicesExpress APIReactNode.jsWebentwicklung
英語で要約が必要ですか?