The video begins by setting up a Turbo repo monorepo with a NestJS backend and Bun as the package manager. The focus is on installing dependencies for Better Auth, Drizzle ORM, and Postgres. The backend is bootstrapped with NestJS, and the database connection is configured using environment variables. Docker Compose is used to run a Postgres container locally. Better Auth is configured with Drizzle adapter, email/password authentication, and trusted origins.
Video bắt đầu bằng việc thiết lập monorepo Turbo repo với backend NestJS và Bun làm trình quản lý gói. Trọng tâm là cài đặt các phụ thuộc cho Better Auth, Drizzle ORM và Postgres. Backend được khởi tạo với NestJS, và kết nối cơ sở dữ liệu được cấu hình bằng biến môi trường. Docker Compose được sử dụng để chạy container Postgres cục bộ. Better Auth được cấu hình với adapter Drizzle, xác thực email/mật khẩu và các nguồn đáng tin cậy.
Hey everyone.
Chào mọi người.
Today I'm super snorts excited to bring you a brand new video on integrating better off in a tanstack router application with a Nest.js backend.
Hôm nay tôi cực kỳ [khịt mũi] hào hứng mang đến cho các bạn một video mới về tích hợp Better Auth vào ứng dụng TanStack Router với backend Nest.js.
Now, in my opinion, this is probably one of the best setups you can have today when developing a full stack web app with the scalable and highly maintainable Nest.js backend with your own custom better off setup which
Theo tôi, đây có lẽ là một trong những thiết lập tốt nhất hiện nay khi phát triển ứng dụng web full-stack với backend Nest.js có khả năng mở rộng và bảo trì cao, kết hợp với thiết lập Better Auth tùy chỉnh của riêng bạn, điều này
makes setting up secure JWTbased authentication so easy, so quick and so secure.
giúp việc thiết lập xác thực dựa trên JWT an toàn trở nên dễ dàng, nhanh chóng và bảo mật.
And finally, we can plug it into a React UI so quickly with Tanstack Router on top, which is quickly becoming my favorite way to create React apps thanks to the beautiful Typesafe Tanstack router.
Và cuối cùng, chúng ta có thể kết nối nó với giao diện React một cách nhanh chóng nhờ TanStack Router, đây đang dần trở thành cách yêu thích của tôi để tạo ứng dụng React nhờ vào TanStack Router an toàn về kiểu dữ liệu tuyệt đẹp.
Now, we're going to see how we can integrate all of this in today's video.
Bây giờ, chúng ta sẽ xem cách tích hợp tất cả những điều này trong video hôm nay.
I'm so excited to check
Tôi rất hào hứng để xem
this out.
điều này.
Now, before we jump in, I just want to let you know if you enjoy content similar to this, head over to my membership website at michaelguay.dev, where you'll get access to hundreds of hours of exclusive developer content in the form of additional lectures, as well as get access to all of my best-selling full courses here, including my latest one here on clean architecture and
Trước khi bắt đầu, tôi chỉ muốn cho bạn biết nếu bạn thích nội dung tương tự, hãy truy cập trang web thành viên của tôi tại michaelguay.dev, nơi bạn sẽ có quyền truy cập vào hàng trăm giờ nội dung dành cho nhà phát triển độc quyền dưới dạng các bài giảng bổ sung, cũng như truy cập tất cả các khóa học đầy đủ bán chạy nhất của tôi, bao gồm khóa học mới nhất về kiến trúc sạch và
domain driven design in Nest.js with over eight hours of exclusive content here going over how to build clean applications that are highly scalable using CQRS and domain driven design.
thiết kế hướng miền trong Nest.js với hơn tám giờ nội dung độc quyền hướng dẫn cách xây dựng các ứng dụng sạch, có khả năng mở rộng cao bằng CQRS và thiết kế hướng miền.
I know if you enjoy this kind of content then you'll get so much value out of my site.
Tôi biết nếu bạn thích loại nội dung này thì bạn sẽ nhận được rất nhiều giá trị từ trang web của tôi.
So I'll leave a link below and thank you so much for checking it out.
Vì vậy, tôi sẽ để lại liên kết bên dưới và cảm ơn bạn rất nhiều vì đã xem qua.
Let's jump right into the video.
Hãy bắt đầu vào video ngay.
All right, so we're going to go ahead and get started with creating our Turbo repo, which is the monorreo manager we're going to use in this lecture.
Được rồi, chúng ta sẽ bắt đầu bằng cách tạo Turbo repo, đây là trình quản lý monorepo mà chúng ta sẽ sử dụng trong bài học này.
I have a previous video on a deep dive into turbo repo in case you want to learn more about it, the benefits of it that I will link below.
Tôi có một video trước đây về phân tích sâu về turbo repo trong trường hợp bạn muốn tìm hiểu thêm về nó, những lợi ích của nó, tôi sẽ để lại liên kết bên dưới.
But really what it provides us is a nice streamlined way
Nhưng thực sự những gì nó cung cấp cho chúng ta là một cách hợp lý hóa tuyệt vời
to have a single repository and workspace with multiple applications.
để có một kho lưu trữ duy nhất và không gian làm việc với nhiều ứng dụng.
in this case are tanstack react router UI and NestJS backend but you can feel free to use whatever monorreo or structure you want.
trong trường hợp này là giao diện người dùng TanStack React Router và backend NestJS, nhưng bạn có thể thoải mái sử dụng bất kỳ monorepo hoặc cấu trúc nào bạn muốn.
I'll go ahead and also use bun in this lecture which is starting to become my favorite dependency manager and script runner of choice as it is the fastest in my opinion the best one but you can also
Tôi cũng sẽ sử dụng bun trong bài học này, đây đang dần trở thành trình quản lý phụ thuộc và trình chạy script yêu thích của tôi vì nó nhanh nhất, theo tôi là tốt nhất, nhưng bạn cũng có thể
feel free to use whichever one you prefer.
thoải mái sử dụng bất kỳ cái nào bạn thích.
If you don't have bun installed on your machine yet, I'll leave a link below where you can grab that with a oneline command.
Nếu bạn chưa cài đặt bun trên máy, tôi sẽ để lại liên kết bên dưới để bạn có thể tải nó bằng một lệnh duy nhất.
Once we have that up and running, we can use bunx create turbo at latest and let's go ahead and set up a new turbo repo.
Sau khi đã cài đặt và chạy, chúng ta có thể sử dụng bunx create turbo@latest và hãy thiết lập một turbo repo mới.
I'll go ahead and call it tenstack better off NestJS.
Tôi sẽ đặt tên nó là tanstack-better-auth-nestjs.
Call it whatever you like.
Bạn có thể đặt tên tùy thích.
And we'll go ahead and select bun as our package manager.
Và chúng ta sẽ chọn bun làm trình quản lý gói.
So, this will go ahead and set up the turbo repo with all of our dependencies where then we can cd into the newly generated turbo repo and open it up inside of our code editor.
Vì vậy, điều này sẽ thiết lập turbo repo với tất cả các phụ thuộc, sau đó chúng ta có thể cd vào turbo repo mới được tạo và mở nó trong trình soạn thảo mã của chúng ta.
In this case, VS Code.
Trong trường hợp này là VS Code.
So, now you can see our project all set up.
Bây giờ bạn có thể thấy dự án của chúng ta đã được thiết lập.
Here we have our apps directory where the applications
Ở đây chúng ta có thư mục apps nơi các ứng dụng
themselves live and a packages directory with any shared code.
tồn tại và thư mục packages với bất kỳ mã dùng chung nào.
We can go ahead and remove the packages directory since we're not going to use this and we can restructure the apps directory.
Chúng ta có thể xóa thư mục packages vì chúng ta sẽ không sử dụng nó và có thể cấu trúc lại thư mục apps.
So by default turbo repo will create a couple of next.js apps.
Theo mặc định, turbo repo sẽ tạo một vài ứng dụng Next.js.
Let's go ahead and remove both of these so we can start on a clean slate.
Hãy xóa cả hai để bắt đầu với một bảng sạch.
So now we have an empty turbo repo with bun all
Bây giờ chúng ta có một turbo repo trống với bun đã được
set up and ready to go.
thiết lập và sẵn sàng.
Let's go ahead and start setting up our NestJS backend where we can integrate and set up better off before finally wiring it into the tan stack router UI.
Hãy bắt đầu thiết lập backend NestJS của chúng ta, nơi chúng ta sẽ tích hợp và thiết lập Better Auth trước khi kết nối nó vào giao diện người dùng TanStack Router.
All right, so our first step is to set up our NestJS backend.
Được rồi, bước đầu tiên của chúng ta là thiết lập backend NestJS.
So I'm going to CD into apps directory and then use bunx which is the npx equivalent in bun nestjs/ cli new I'll call it api and choose package
Vì vậy, tôi sẽ cd vào thư mục apps và sau đó sử dụng bunx, tương đương với npx trong bun, nestjs/cli new, tôi sẽ đặt tên là api và chọn trình quản lý gói
manager as bun.
là bun.
Now you might see here that package manager bun is not managed.
Bây giờ bạn có thể thấy ở đây trình quản lý gói bun không được quản lý.
That is fine.
Điều đó không sao.
We should now see back in the project we have our API directory with a brand new NestJS application.
Bây giờ chúng ta sẽ thấy trong dự án, chúng ta có thư mục api với một ứng dụng NestJS hoàn toàn mới.
Now, just as a cleanup, make sure we cd into API and go ahead and remove any git directory that has been created here.
Để dọn dẹp, hãy đảm bảo cd vào api và xóa bất kỳ thư mục git nào đã được tạo ở đây.
And same thing for node modules since this is created globally.
Và tương tự với node_modules vì nó được tạo toàn cục.
And now if we go back to the root and run bunt install again, this should pick up our new Nest.js GS dependencies because this is a bun workspace thanks to turbo repo.
Và bây giờ nếu chúng ta quay lại thư mục gốc và chạy bun install lại, nó sẽ nhận các phụ thuộc NestJS mới vì đây là không gian làm việc bun nhờ turbo repo.
Here we defined our workspaces array and that tells bun to look for package jsons in any of these directories when installing dependencies.
Ở đây chúng ta đã định nghĩa mảng workspaces và điều đó bảo bun tìm kiếm package.json trong bất kỳ thư mục nào trong số này khi cài đặt phụ thuộc.
So now the API should be set up.
Vậy là api đã được thiết lập.
Our package json has all of our familiar starting Nest.js scripts to start the development server and start up the compiled JavaScript.
Package.json của chúng ta có tất cả các script NestJS quen thuộc để khởi động máy chủ phát triển và khởi động JavaScript đã biên dịch.
We can see inside of source we have our familiar maints file where we bootstrap the application server on port 3000 to listen for incoming traffic.
Chúng ta có thể thấy bên trong source có file main.ts quen thuộc, nơi chúng ta khởi động máy chủ ứng dụng trên cổng 3000 để lắng nghe lưu lượng truy cập đến.
We also have the default app controller which defines our
Chúng ta cũng có app controller mặc định định nghĩa
HTTP routes.
Các tuyến HTTP.
So we have our single get route here exposed at the root of the web server and we're also injecting this app service using constructor injection because it's marked as an injectable.
Vì vậy, chúng ta có một tuyến GET duy nhất được hiển thị tại gốc của máy chủ web và chúng ta cũng đang tiêm dịch vụ app này bằng cách sử dụng tiêm hàm tạo vì nó được đánh dấu là có thể tiêm.
And in here we just have a get hello method that returns the stub string.
Và ở đây chúng ta chỉ có một phương thức get hello trả về chuỗi stub.
So with all of this in place, we should be able to start up the Nest.js development server by running turbo dev at the root.
Với tất cả những điều này, chúng ta sẽ có thể khởi động máy chủ phát triển Nest.js bằng cách chạy turbo dev tại thư mục gốc.
We'll first make sure that we actually update our package JSON in Nest.js.
Trước tiên, chúng ta sẽ đảm bảo rằng chúng ta thực sự cập nhật package JSON trong Nest.js.
It comes out of the box with startdev, but we want to standardize this to be just dev, which will match the same command as our React UI later on.
Nó đi kèm với startdev, nhưng chúng ta muốn chuẩn hóa nó thành chỉ dev, điều này sẽ khớp với cùng một lệnh như giao diện React của chúng ta sau này.
To make sure we save this.
Để chắc chắn chúng ta lưu lại.
So now the commands are aligned and now if we run turbed dev from the root of the application you can see our NestJS server has compiled and
Vì vậy, bây giờ các lệnh đã được đồng bộ và nếu chúng ta chạy turbo dev từ thư mục gốc của ứng dụng, bạn có thể thấy máy chủ NestJS của chúng ta đã biên dịch và
is now listening for changes which is great and so now the web server is listening for traffic.
đang lắng nghe các thay đổi, điều này thật tuyệt, và bây giờ máy chủ web đang lắng nghe lưu lượng truy cập.
If we go to localhost 3000 inside of our browser or launch a request in any client like Postman you can see here we have the hello world stub string being returned back to us.
Nếu chúng ta truy cập localhost 3000 trong trình duyệt hoặc gửi một yêu cầu trong bất kỳ ứng dụng khách nào như Postman, bạn có thể thấy ở đây chúng ta nhận được chuỗi stub hello world được trả về.
So this is awesome.
Điều này thật tuyệt vời.
And we have the NestJS API up and running.
Và chúng ta đã có API NestJS hoạt động.
Let's now go ahead and set up our better off integration so that we can have authentication on this server.
Bây giờ hãy thiết lập tích hợp better-auth để chúng ta có thể có xác thực trên máy chủ này.
So the first thing that we'll have to do is go ahead and install the dependencies that we need.
Điều đầu tiên chúng ta cần làm là cài đặt các phụ thuộc cần thiết.
So make sure we are in the API directory and let's run bun install.
Hãy đảm bảo chúng ta đang ở trong thư mục API và chạy bun install.
We will add at phallispjs better off which is the nest.js implementation wrapper around better off.
Chúng ta sẽ thêm @phallispjs/better-auth, đây là trình bao bọc Nest.js cho better-auth.
We'll add better off itself.
Chúng ta sẽ thêm chính better-auth.
Drizzle RM for database persistence Postgress to connect to our Postgress DB.
Drizzle ORM để lưu trữ cơ sở dữ liệu, Postgres để kết nối với cơ sở dữ liệu Postgres của chúng ta.
Make sure we then go ahead and install -d for development cli which will help us to load in environment variables and drizzle kit which is going to allow us to create and execute migrations.
Sau đó, hãy cài đặt -d cho cli phát triển, điều này sẽ giúp chúng ta tải các biến môi trường và drizzle kit, cho phép chúng ta tạo và thực thi các migration.
So now we have all the dependencies that we should need to go ahead and get started.
Vậy bây giờ chúng ta có tất cả các phụ thuộc cần thiết để bắt đầu.
I'll go ahead
Tôi sẽ
and restart my development server and go ahead and start adding the implementation.
khởi động lại máy chủ phát triển và bắt đầu thêm phần triển khai.
All right, so now that we have the dependencies we need, let's go ahead and start off by cleaning up our application.
Được rồi, bây giờ chúng ta đã có các phụ thuộc cần thiết, hãy bắt đầu bằng cách dọn dẹp ứng dụng của chúng ta.
Remove our app service and app controller spec file.
Xóa tệp app service và app controller spec.
These are no longer going to be used.
Chúng sẽ không còn được sử dụng nữa.
Let's keep our app controller file completely clean and empty.
Hãy giữ tệp app controller hoàn toàn sạch sẽ và trống rỗng.
And now we're ready to start.
Và bây giờ chúng ta đã sẵn sàng để bắt đầu.
We can remove the app service from the app module as well.
Chúng ta cũng có thể xóa app service khỏi app module.
Now the first thing that we want to do is set up our database connection which is where we're going to persist our users details.
Điều đầu tiên chúng ta muốn làm là thiết lập kết nối cơ sở dữ liệu, nơi chúng ta sẽ lưu trữ thông tin chi tiết của người dùng.
So for this let's create a new DB directory.
Vì vậy, hãy tạo một thư mục DB mới.
And inside of DB we can add a index.ts file where we'll set up our database connection and our schema exports.
Và bên trong DB, chúng ta có thể thêm tệp index.ts, nơi chúng ta sẽ thiết lập kết nối cơ sở dữ liệu và xuất schema.
So inside of this index.ts
Vì vậy, bên trong tệp index.ts này,