Skip to main content
View on YouTube to comment and like!

Syntax Video

Modern Svelte Kit - Complete Svelte Kit Course for Beginners

Chapter 1 0:00

Intro

Hey, what's up? This is Scott from Syntax and today we're going to be doing a full spelt kit build. We're going to be using the latest and greatest features from speltkit like remote functions to build a blog. Now, this blog is going to be very, very simple, but it will have an admin experience. So, you will have user accounts via better off. You will have a database via Drizzle and Postgress. And you will be able to use remote functions to add new posts and load them and see all of the goodness about the new spelkit stuff along the way learning about the finer points of the speltkit framework. That's right, because this is going to be a fullon full stack complete spelt kit tutorial. So if you are interested in learning this framework, this is a great video because it gives you access to CRUD operations with the database, O remote functions, uh routing, any of the nice concepts, loader functions, and

various aspects of Spelkit. It is going to be a lot of fun. So if you're interested in that entire bit, uh get your terminal ready. Let's get going. If you just want to learn one or two aspects of this, I will be sure to be breaking down topics fully and completely. So, we will have everything in the chapter. So, if you want to zoom to a chapter of something very specifically you want to look up, that is cool, too. So, let's get started going. First and foremost, we need to use the new Spelkit CLI. It is SV. It's the spelt CLI. This thing is really great for a number of reasons that which you'll see. I'm in a directory in which it's going to be cool for me to build this site. So I can do npx svc create and we're going to call this remote blog. You can call this anything you want. I'm calling it remote blogs. It's using remote functions. Hit enter. So it's going to ask us if we want to proceed. Yes. Um you can see at the time of recording this this is 0.9.11.

Chapter 2 1:23

Creating a SvelteKit project

So something to take note. We are going to be using the spelkit minimal uh because we don't want any extra stuff. We are going to be using typescript. Non-typescript folks can largely just ignore TypeScript, but it's really nice to have and you'll see why. Um inside of here, it's going to give us an option for a number of different things we'd like. Now, one thing I really love about the SV CLI is that it gives you these options. And you'll see in a second when we select Drizzle for our database just how great this is. Now, there is things like Oxlint and Biome that are kind of working right now. At the time of recording this, I'm still on prettier and I'm still on ESLint. I'm also not going to use Vest because let's face it, who writes tests? Just kidding. Let's also go ahead and select DevTools JSON as the next one. DevTools JSON just it basically makes it so you don't get a console error when uh Chrome DevTools are trying to connect to the site with a Vit site. This is a newer thing in Chrome. you may have seen it. And then

Drizzle, we're going to select that. Uh we're not going to select Lucia because we're going to be using better off. It's the off platform that I personally use all the time. So um there's other options in here including MDFX which is like markdown plus felt or um internationalization or storybook for building an isolation.

We use a lot of these things on syntax. So we use storybook on syntax. Um we use playwright, we use vest, we use all these things. But for the sake of this tutorial, I don't want to throw too much at you. Now, there's also the spelt MCP, which we won't be covering in this course, but it's an interesting topic if you're interested in AI coding at all.

So, we're going to have prettier eslint, dev tools, JSON, and drizzle. When I hit enter, it's going to ask me for which database technology. We're going to use Postgress because that's what I like to use. And then as far as here goes, which Postgress client would you like to use? We're going to say Postgress.js. And then for drizzle, would you like to run the database locally? This is a really great option. If you have your own flow with Postgress on your computer, by all means just use that.

But if you have this going, let's say yes to this. And all we have to do is make sure we have Docker installed on our computer and running. And then we just run the database via Docker. It's nice and easy. Okay. So, which package manager do you want to install updates with? I typically use PNPM myself, but because npm is the most popular still, I'm going to use npm. Use what you'd like. Just know that you might need to account for that.

This is going to go ahead and get our stuff going. Uh, which is just great that it goes off and just does all this stuff for us, right? We love that. Beautiful. So, let's go ahead and change directories into our application. We're going to look at the source files, what came with this. We're then going to talk about uh some of these little messages that it has like what are the next steps here? We need to set our database URL for production. We don't have to do it for dev. Um but let's CD into our remote blog. CD remote blog. I don't think we need to run npm install. It just says we can run npm uh rundev and then open if we'd like. It did install our dependencies for us. You can I I like by just by nature end up running npm install a billion times. Um, so we can go ahead. I'm using zed as a text editor. You can use co VS code, you can use cursor, you can use neoam, use whatever you want as your text editor.

I'll be using zed. Um, so I'm going to open up the current site in zed as my text editor. So let's take a look at what comes with spellkit, especially when you get going using a system like this. So first and foremost, we have our speltkit folder. This is all generated stuff. You could delete this and the moment you run your dev server, it's going to regenerate it. Node modules, we should know what node modules is. It's where all of your packages get installed to. Um, this gets regenerated when you run npm install. Now, the main bit of our website runs inside of this source folder. Inside of source is going to be a couple of folders. One is lib and one is routes. Now, before we get into those, we also have our main app.html.

Chapter 3 5:27

Looking at the default project structure

You shouldn't need to edit this file really. It is just a straightup HTML file with some tags that get replaced. If typically if you're working with the metadata inside of a spelt app, you're using spelt head instead of editing this HTML file directly. There's also apps.d.ts, which is where you can have your global um types. We will be working with this in this course. So, I'll show you how to use that. Next, let's take a look at the lib folder. Inside of the lib folder right now, it actually created some stuff for us. Now, typically it doesn't, but because we end up having uh this database that we do have a server and a DB folder inside of here. It is fairly common convention to put your server side stuff in a server folder, but that's up to you. You can organize this anything you want inside of lib. Uh we just have an index file that is allowing us to set our database, which is some basic Drizzle code. If you've ever worked in Drizzle, which is an OM for our database, uh this should feel very familiar. if you haven't. What

Drizzle and what an OM does is it makes working with our database easier. So we don't have to write SQL commands and stuff. We can use functions and uh query builders and schemas to work in our database. Drizzle can manage our migrations. It can give us access to a UI to be able to see our data which you'll see. Drizzle is an excellent choice if you like OMS. If you don't, you don't got to use it. Uh we also have our schema. Now a schema it it created this basic user schema. We will be deleting this. We don't need it. So uh just a heads up there.

Now we also see just a generic index file. Now the one thing you need to know about the lib folder inside of speltkit is that out of the box lib is kind of a special folder in that anywhere in your application you can import things from dollar sign lib. This is just an alias but it's one that comes out of the box. So if you want to throw components or throw folder structure inside of lib, you can always get to it with dollar sign lib. If you're building a library, lib actually is where library code ends up being living, right? And then your demos might go in routes and then you when you build your package as a code package, then you're able to deploy the code in lib.

But for our use case in this video, lib is just a place you put stuff and you can organize to your own capacity. We'll get much deeper into where to put stuff when we talk about routing in depth. Now, we're not going to be going over basic spelt functionality. So, if you want to learn how to work in spelt, we have a complete spelt 5 course in here, but we will be talking a lot about spelt kit convention stuff specifically. And then we have our routes folder. Now, our routes folder is going to be where our page routes live. Now, I'm going to go deep into routes because routing, as we know, is it's not like a complex topic, but it's one that you have to use in every app. So, I will be showing quite a bit about routes as we go. But your routes go in here. All right, let's bail out of src. In static, we have a static directory. Now, this is going to be served as just forward slash in your application. So, forward/roots.txt is going to be available publicly.

Anything you put in static, it's going to be available publicly. So don't put your deepest darkest secrets inside of your static folder uh or else the whole world will see them. Okay. Uh next we have our env file. Now our env file has been graciously filled out for us because we chose to use the uh drizzle and docker setup. And in fact it has a database already set up for us and the right port with the right name and all this stuff ready to go. Now if you are managing your own Postgress, you can set this to be whatever you want. But heck, hey, it's here for us. It's just going to work out of the box. Um, it even gave us a ENV example file, which is great to have. A git ignore, uh, which is for git to ignore your files that you don't want committed to git. Um, and that includes the nice stuff like obviously node modules, but also env.

We don't want to expose our secrets, right? NPMRC, we don't need to worry about. Prettier ignore is just going to say what files prettier is going to ignore. Uh, we have a basic prettier setup with spelt parser plugins and everything going. So if you use prettier, you don't have to do anything there. Our compose file, this is for our database. This is our docker file that allows us to have our database up and running really quickly and easily. And we'll get going on that in just a minute. Uh we also have our drizzle config, which is going to be pointing towards everything that we'd expect it to be pointing to. In fact, even our database is set. Everything is set inside of here. It's all ready to go. I love the SV CLI because it gives you so much of a good head start. like we have a database, we have everything ready to go, even a schema. Same thing with ESLin as with prettier, just basic setup for spellkit. Our package.json is nothing special, but they did give us some interesting scripts in here like uh DB start to run docker compose up. Uh DB

push to push to our database if we want to be working uh rapidly. You can also generate and migrate with migrations as well as run studio to look at your data. As far as dependencies go, we have a handful of dev dependencies, but nothing outrageous or that you wouldn't expect. We have our drizzles, our eslint, all the stuff we told it we wanted. And then as far as dependencies go, it's really just Postgress right now, which is great. Now, we will be adding to that, but not substantially. Other than that, we have a speltconfig.ts.

This is a file that we will be modifying. In fact, it's probably worth modifying it right now because what this is is not a file that you edit very often, but if you want to add additional aliases, like you like lib and you want to add dollar sign state or dollar sign DB, you can go nuts. Uh, you can do that in here. You can also change the adapter. Adapters is something we'll talk about towards the end of this course, but it's really just where the heck you're deploying this thing. And if we want to modify some things that are spelt kit related specifically, you do it here. So inside of here, we want to add a new property named experimental.

Chapter 4 12:05

Enabling Remote Functions & Async

This is because we're going to be using some experimental new felt kit features that eventually will become the way of doing things. And you might as well learn the way right now. So inside of experimental, we'll have remote functions as a property be set to true. Okay, like this.

Now there's another thing we'll want outside of kit. outside of kit. It's important you know that this is outside of kit. That way you don't goof this up. Outside of kit, we want a property which is compiler options. If you see autocomplete completing this, then you know you're on the right track. Compiler options also has the options of experimental and then async is equal to true. Now the reason we need this is because async spelt is a spelt compiler feature where remote functions is a spelt kit feature. So, by turning both of these on right now, we're able to use them. Chances are when you're watching this, you might not need to do this part. In fact, it's probably best to consult the spelt documentation before doing this on the remote functions page.

That said, for today in November 2025, you have to do this. Uh, we also have TS config, which we shouldn't have to touch. There are some notes because speltkit does generate its own TS config file. So if you need to edit um things like include and exclude, there's some notes here. So read those.

And vit config do uh our vit config is just our V config. Um if you've been using VIT before, Vit is the bundler. It's the builder. Um it's the dev server. It does hot reloading. It's the very it's the best. It does all sorts of stuff for us. Um the only thing we need here within our VIT process is a speltkit plugin. And because we chose the DevTools JSON thing, DevTools JSON plugin also pre-installed for us. Okay, so those are the files that spelt kit comes with. Okay, congratulations.

You've made it through the first part of this video. Now, before we get up and running, uh actually working on our authentication system because once we kind of get running on that, there's going to be a lot of concepts in that and there's going to be a lot of stuff really quickly. I do think it's worth going back to the routing part and really discussing the routes in depth.

But to do that, I want to run our dev server. So, let's head back to our terminal. And if we were to just run npm rundev right now, it would fail because our database is not up. So, we need to get our database running. We need to run npm run db start. I always get this confused with up because it's docker compose up. So either way, it's npm run db start. That should get our database up and running. You should see something that looks like this. In fact, it says database system is ready to accept connections. Now, if this is erroring out and saying that Docker isn't running, you do need to have Docker running currently. So again, you need to have Docker running because this uses Docker to spin up your database or you can just run Postgress yourself. Okay, so now that we have that, let's do npm rundev. That should get us up and running. And we're on the classic V po port of 5173.

Chapter 5 14:28

Starting the DB & dev server

Okay, so let's open this up. I'll be using Helium browser for this tutorial, but you can use any browser you want. Everything should just work just fine. Okay, so welcome to Spelkit. Hey, we have our app. So let's talk about routing. And I'm going to bump this over side by side here. And we're going to look at the routing structure here because the way that routes work inside of speltkit is that each folder is a route. And inside of a folder can be a number of things. It can be another folder. So you could have forward/about/scott.

Chapter 6 15:38

Routing

So that's a folder inside of a folder. You could have a plus page.elt file. Now these plus files that you see over here are special route files. So plus page.spelt spelt is the spelt file that loads for that route. The layout is a file that loads for not only that route but every nested route below that. So let's go ahead and show this in action.

If I have a new folder, this is going to be about and inside of about is going to be a plus page.spelt. And just to keep this very simple, we're going to say about. Okay. Now, if I were to visit forward slashabout, says about, right, that's great because again, the folder is the thing. Now, if we were to modify our layout that we have in here, the layout that comes pre-existing, you'll see that all that's in here is spelt head and a render children. Right now, all it's doing is it's going to render the page because inside of a layout render children is just rendering the contents of the page.

Right? So, if we wanted to let's say actually have a site header for this, we can call this remote blog like here. You'll see that the remote blog header exists on both the about page as well as the main page here. So, if we were to be on this page, it says remote blog. We go back, it still says remote blog. We go forward, it says remote blog. And all that's switching out is the contents of children. Now, one of the really great things that SpeltKit does that many frameworks are just now figuring out how to do or even have never done well is this nested layout concept. So, you can throw a layout in any level of your route deep. If I wanted to throw a layout inside of about, then it would wrap every subsequent page in about. And likewise, you could have a layout at every step of your routing structure as

long as that works. Uh layouts and and page files are very very powerful. both of them need the plus on the file and that can become a little bit of a pain but hey uh it ends up working really well in practice because again you know the folders are the routes and the plus files are the special route pages but that means you can throw anything else you could possibly want into the route folders like do you have a remote function that goes off and fetches data but only for one section of your site or whatever it makes a lot of sense to keep that right there in in that route, you could put that there. Or let's say you have a component that you're only using in one section of the site, too. You could throw that in the routing structure and it's not going to goof up how the routes are loading because Felkit's looking for directories and it's looking for page files. Now, there's also parameterized based routes.

We're going to cover those when we do our blog post. It makes more sense there. But I did want to get this out of the way before we get into the authentication piece, which we're going to do really quick because the authentication piece will teach you all kinds of stuff. So, um, we're going to be using something called better o for our o system. Better o is a really great library that connects directly to Drizzle. It gives you a lot of great uh UI. It create gives you a lot of great client side functionality. It gives you server side functionality. It makes getting up and running with O super super easy.

Chapter 7 19:05

Adding Better Auth & Graffiti

But at the same time, in the scope of a course like this, it will teach you about things like middleware. It's going to teach you about things like a page loader function. It's going to teach you a lot here, even redirects and stuff like that. So, it's a great time to add this into our project. Okay, I'm going to bounce this over here and we're going to get into installing some dependencies. Now, I'm of the mind that it's better to install dependencies off the jump that we may need instead of installing things peace meal as we go.

So, I'm going to open yet another tab here or another split window in my terminal. You don't have to stop anything, but we can do npm add. And we're going to add better hyphen off. We're also going to add a UI library. And I want to be very clear about this. You don't have to use this. In fact, if you're a Tailwind person, go nuts with Tailwind. If you're uh any type of UI framework you like, use that. I wanted something that was really lightweight and light touch. So, I have something called drop in drop.

It's atropy- in slashgraiti. And this is just a really simple CSS library that we can include that will give us some just like basic nicities off the jump. Okay. So, with those installed, better off and graffiti, that's pretty much all we need here for right now. But before we get going on better off, since you can see this bright white screen is blinding me, I'm going to it bring in graffiti to give me some uh uh color scheme stuff. So, in my layout.spelt, this is going to be something that you can do at any point in any file in your application. You can import CSS files. So, if I wanted a CSS file locally at any given point, I could throw it in here and import it just with JavaScript. That's a Vit feature for the most part. Um, but as far as drop in goes, we can just do it like this.

Import it, drop in. And you can see right away our heading showed up that now we for me it's a black screen that's actually tied directly to the color scheme that I have set your appearance mode. So yours might be white on black, but it's a nice little thing to get started off with. And we will use some of the the the graffiti things from here, but we're not going to go nuts on it by any means. So don't feel like you have to use it. Okay. So cool. We have this going. I'm going to remove this remote blog title for now. It's obnoxious. We're also going to just go back here. Now what I want to do with better off is to get us going. Now the better off setup does require an env variable. And this is the better off secret. So better secret. Now this is basically just a a a singular secret file that is used for generating hashes. It's something that you should maintain consistent and like if you change this that will be a problem. So you want to keep it as it is

once you set it. The better off.com betterhoff.comdocsinstallation will give you a generate button to generate one. You can really just generate a small little string anywhere you go. Whether that's one password, whatever that is just hucking it and typing it yourself. This is the one that generated for me. Um, typically you want to keep this in your env. You don't want to share this, but again, this is just a project that's running locally on my machine. So, so you shouldn't show people this, but for the sake of this video, it's it's no harm there. We'll also want to set up a better off URL.

The reason it needs this is it's going to be sending requests to our server and well, it needs to be able to successfully do that. And uh you'll want to have your your URL in here whether that is your production URL or in this case uh just the standard vit URL. So we have those two things. That's great. The next thing we really need is uh we need to create a better off instance and this is going to live inside of our lib folder. Now better off itself says that it should be in the lib or the utils.

Luckily for us lib is a common thing inside of cell kit. So that makes it easy. We don't have to do anything wacky to make this work. And we'll just want to have an off.ts file. Now, this off.ts file is going to import. And we're going to import better off from better off. Okay. And we're also going to export const equals better off like this. This is where we configure our instance, whatever plugins or whatever database we're using or those types of things.

What's nice is that since we're using Drizzle, we can use the adapter to set our database. So, we need database as a property inside of here. And this can just be set to Drizzle adapter. You can see mine uh in Z it gives me this autocomplete. So, I'm going to hit enter and it will bring in the autoimp import. So drizzle adapter is coming in from better off adapters drizzle. We don't need to install anything for that. Better off has like a mass amount of plugins and stuff that you just get. So Drizzle adapter and then we'll want to pass in the DB instance ourself. Again, Zed is being a really cool dude and is identifying here that the uh DB is coming in from server DB. If you remember, we could actually do dollar sign lib here instead of that if you don't like relative paths. I tend to always lean on the dollar sign lib, but if the autocomplete works, the autocomplete works, right? Uh, so second parameter here in drizzle adapter is

going to be our provider. Our provider is going to be PG. We're using Postgress, so it's going to be PG. Tada. We got it going. Love this. Now from here what you end up doing is you run a script that is going to generate your database schemas for you. We can run this but I will warn you that we will be running it again momentarily. So don't go too nuts with this just yet. I'm going to head here to a clean term and I'm going to run npx at better o oh my god cli and then generate. This is going to generate a Drizzle schema for us. And if you were not using Drizzle or using other things, it could actually do some of this all for you. I don't mind just having it straight up generate this for us. I'm going to hit yes twice, and it is going to generate a schema for me in the root of our project, which exists

now down here as o schema. Now, you can see it's a bunch of drizzle tables, including a user and a session and an account and verification. So out of the box, we get all this stuff and we didn't have to write it. That's great. That said, this doesn't have email password off. It doesn't have OOTH. It doesn't have any of the things we want to use.

You can do all of that, whatever you would like, magic uh magic code off or SMSbased off or any of that stuff with better off. And that's why I love it so much. In the sake of this video though, however, we are going to be using just email and password off. And to do that above the database here, we can just say email and password. And then this is simply just enabled true. Okay, that's really it. And we're going to have email and password off right out of the gate. Now, there's also going to be a plugin that we're going to be using.

And I I feel like it's worth setting this up right now instead of waiting. That way, we don't have to generate things twice. The plugin is going to be admin. So, another property we can have in here is plugins. And plugins is an array. Inside of this, we're just going to say admin. And I got to say, I love text editors that just really know when to update my stuff. So, admin is a function and it's coming in from better off plugins. So, this is how you have it set up. That's really great. We'll also need to do this on the client side since this is the server side of things, but uh looking pretty good here. Let's go ahead and now run that once again. Our npx better off TLI generate. It's going to generate our schema once again. We love that. Hit yes. And our schema has now been successfully written. And if we come back and look at it, you should see more things. Um we should see things like bands and rolls that comes from the

admin plugin. Uh, we should be able to see things like email on here. We should be able to see um, uh, password is now here. So, all of this now exists. And you might be like, okay, this file exists outside of my structure. What do I do? There's a couple of ways. You can configure better off to output this to your actual schema or you can output it somewhere and import it. I am a caveman and I like to just highlight this all and then come into our schema file and overwrite it. That's just how I like to do it. Um you you can do it different ways if you'd like. It depends on how many times you imagine going back to the well to generate things from better off.

Uh I'm going to go ahead and just delete the schema that it generated for us. That way we don't have to have it around. We have one schema. It's all living inside of schema ts now and it is the stuff that better off created for us. Now because we have this massive schema here um if we were to run npm run db studio this is going to give us a UI for seeing our database.

Let me bring that over here so you can see it. This is a good way to confirm that things are where you expect them to be. We have no tables currently. Even though we have a schema, we have a database, we have no tables. So to get tables, let's go ahead and I'm going to have one more of these terminal one. This is how my life exists. We just end up having a bunch of terminal splits everywhere. Um, we can end up running npm run db push.

DB push is a way to just push the current schema into the database. If you're working longterm, you're going to use migrations by generating and migrating. But for this first initial one, we can just say push. Go for it. It's going to say, do you want to do this? I say absolutely yes, I want to do this. I can close this tab. I can head back to my studio. I refresh and check it out. We have an account table, a session table, a user, and a ver uh verification table. We even have roles inside of here. This is all out of the box. The only thing we had to do to get any of this stuff working was to create this o.ts file and run a single command.

That's pretty pretty special if you ask me. Um, this is just really really great because you can get up and running faster on some other systems like Superase or Pocket Base or these other like managed database systems. But you own this. This is on my computer. Um, I can take this anywhere I want. I could even throw this on superbase if I wanted to eventually. Doesn't matter. So, you got to love that. Okay, so we have our better off server and we'll also want to go ahead and get a better off client going on as well because the better off clients how you're going to be interacting with this thing from the client side. From there, we're going to get into our middleware. We're going to be getting into our and from there we're going to be getting into our routes and actually building something off system hooks authentication all that stuff. So let's come here and what we want to do is head back to our lib folder and we're going to create another new file. This is going to be oy-enclient.ts.

So the o.ts was what your server side code runs on. This is going to be your client side better off code. So this is all better off. So we're going to say oclient is equal to create o client. And you'll want to import this from better off forward slashfelt. Okay.

It's so great that they have spelled everything for us. So from here, we also want a plugin. And that plugin is going to be the admin client plugin. And that's going to be largely the exact same as we had for the server, just client plugins. Okay, this is it. This is the entirety of the uh client file.

We're going to be able to then use this to do our login process and everything. It's going to be fantastic. Okay, so let's go ahead and I'm going to create a new folder inside of our routes and this is going to be O. Now people handle off in all kinds of ways. Whether that is a window that pops up, whether that is an actual route you route to, whether that is just client side changes. We're going to end up doing this within a couple of routes. So we have forward slash and then another new folder login and then another new folder inside of O which is sign up. Okay, so login sign up. Now obviously it makes sense to work on the signup flow first, right? Kind of makes uh sense to me, right? So let's go ahead and we'll want to go ahead and create our plus page.spelt inside of signup.

Chapter 8 32:04

Sign Up Page

Now, we're not going to be using remote functions for this process. You probably could, but the O client in Better Oroth gives you enough here that it's worthwhile to just use that. So, we're going to say script. It's going to be a lang of TS, meaning that we get TypeScript in here. And we're going to want to import our O client from lib O client. We're just going to have this thing around on this.

you'll get all kinds of methods on here, whether that is sign up or anything like that. So, uh, that's great for us. Next thing we'll want to do inside of here is we might as well put an H1 that says sign up. Might as well just toss an H1's around. And then we're going to want to have a form. One thing you want to get used to inside of spelt land is using HTML forms. That sounds ridiculous to any normal web developer, but a lot of JavaScript frameworks go out of their way to have you do everything in JavaScript. We end up using forms a lot.

Now, for this form, we will be using JavaScript, but other ones down the line, you will see uh maybe less. So, check it out. We're going to have, and I like to do this, I like to do a row. Um, this is actually a class I believe is in graffiti, but I like to do a row for my form rows to give them some space. Uh, you don't have to do that. You can set your form up any way you would like.

We're going to have a label. The label is going to wrap around the input. That way, we don't have to do the label for business. It is going to be required. And it's going to be a type of email. So, email. And then the ID is also going to be an email.

Okay. And the label the label should probably say email. Email. Em email. There we go. And we'll want another row for password. And then another another one for confirming your password. So password. And this is going to be a type of password. And then the ID of password. And then likewise p uh confirm password. And then you can have this also be a type of password. And then the ID of what do you think for an ID?

Password. Confirm. It's kind of obnoxious that it's the reverse of confirm password, but sometimes things just be like that. Okay. So lastly, let's go ahead and just have a button. And this is going to be a type of submit.

and it's going to be sign up. Cool. Now, we're not going to go too nuts with this form, but let's go ahead and at least see it in action for now. We can head toward slash offward slsignup. Okay, what a wonderful looking form this is. What a great looking form.

Um, this really takes you back to web forms of a certain era. I do have some classes to make this a little bit nicer. I hesitate to use too much graffiti stuff in here, but maybe I should do that. Um, we're just actually we'll wrap this whole thing in a div with a box hyphen one and like this.

And it put it in a box. It's not perfect, but it put it in a box. That's great. Now, if we want to get some more fancy class to this, even though I just said I would hesitate to do that on our main layout, and really the only layout we have going on right now, we can add a main HTML tag to this. I tend to do this anyways, and then just throw some classes on here, of which I have one called layout readable that just makes it a max width and then a uh center adapter on here that kind of centers it.

So, again, this is nothing crazy. You don't have to do it, but might as well. Okay, so our form is looking uh pretty ugly still, but we have a form at least. One thing we want to do on our form is add an onsubmit. Now, onsubmit is going to be just a classic uh spelt event. I got to spell onsubmit. I got to spell onsubmit correctly for it to work. And we're going to do it in a function here, which is going to be sign up. And we can then call sign up directly from unsubmit.

So from here we'll have access to the given event um which is just going to be an event type of event. And we're going to first and foremost say e.prevent prevent default. Why are we preventing default? Well, we're going to handle this form with JavaScript. Even though I just I just kind of ranted about not doing that.

We're going to do it and we're going to say first, hey, stop what you're doing. Don't actually do the standard form thing. And what we're going to want to do is run our signup. Now, we want to check some things first. We want to see if we can get access. We'll want to see if the email password and stuff are correct. So, let's go ahead and actually do that right now. We'll want to go ahead and say con let's get access to the form first. And the form is going to be equal to e.target.

And I'm going to cast this as an HTML form element. Okay. And we're going to get access to const email is equal is equal to form. Do value. And we're going to do the same thing with password as well as confirm. Uh now I've done this twice now. password confirm.

I think I call it password confirm. I got to make sure I'm naming this stuff the correct uh thing every single time. So, password confirm value. And actually uh one thing I'm just remembering this is a little obnoxious, but better off expects there to be a username. So, I'm going to make another row that is username.

Uh this is going to be type of text and it's going to be username. Another thing I'm noticing is that my ID for email was ID. Let's go ahead and fix that now. There we go. So, we have username, email, password, and password confirm. Um, in here we have email, password, password confirm, and we'll need username. Um, that way we can be covered. So, this all just works. Now, what are some things you might want to check in here? Well, like what if password and password confirm aren't equal? So if password is not equal to password confirm then we'll want to set some kind of error message possibly and return. Um let's create a state for that. We'll say let error equal state like this.

The joys of speltkit right? It's so easy to do this where we can say error passwords don't match. Beautiful. Okay. We could also check to make sure our stuff actually exists. Like if um username exists, if there is no username or no email or no password or no password confirm then also same thing. Now, I don't love doing forms like this. Typically, I might do a um a less JavaScript heavy approach here. Um we could just say all fields are required. Either way, some basic basic stuff to keep us uh in in check here. From there, what we can do

is we can do await O client. And this is where we actually get to do the fun stuff. We get to sign up. So next we get to do await oclients sign up. Beware of the camel casing here. And we're going to be able to pass in our stuff inside of here. I might not have made this function async. A sync function. There we go. Async function signup await client signup and email.

And then from here it's easier than you might expect. We're going to just say email password and then name is equal to username. Now, I don't know why my formatting decided to go all haywire just there, but it it didn't. Now, you can have a number of things from here. Uh there's all kinds of protection. I want to avoid this becoming a complete better off course because there's an infinite well of cool stuff about better off. But from here you get access to a number of different uh potential methods as a second property. A second property being an object or second argument being an object here. And one of those is on success. And on success uh allows you to be an async function where you have access to the current context. We won't need that. And then we're going to have a function result here. Now, from here, this is like what you want to do on

success. In a moment, we're going to have us redirect. Okay? So, we want this to after you have successfully logged in or signed up, we want you to go to the homepage. So, we'll say go to forward slash like this. And we'll want to import go to from app.n navig or app forward slashnavigation.

So goto is one of these functions and utilities inside of feltkit that just exist. There's a ton of these cool little things and goto is just your programmic routing. You're not using anchor tags. You want to route via JavaScript, you use goto. Now this again comes in from app navigation. Now there's a number of things within app.navigation navigation as well as other uh different stores or functionality that we will continue to talk more about over the course of this goes and those are usually prefixed with a dollar sign. Um so dollar sign app for slashnavigation. Okay. Um you can see there's a number of different things inside of here. Spellkit is a deep well because it's very very capable. Um there's a lot of cool stuff that exists inside of here. Um, you can even see one app paths to get uh path data. There's a lot. So, we'll talk about them as they come, but um for a lot of parts we're going to be relying on the autoimp import to autoimp import it and then we

can examine that later. Okay, really cool. Um, we'll want to do something with this error message, too. We don't have to do anything super fancy with it because we're not that worried about building the world's most accessible form right now. But let's go ahead and and actually I'll throw it below or above the sign up button right here. And we'll just say pound if error then then output the error. Easy. There is a red color as a part of graffiti. So you could always do style colon color is equal to var red. I think it is just red. It might be c red. Actually it is just red. I'm going to be seeing red if that didn't work. Okay, so here we go. Let's go.

Let's first try to submit this form to see the error message pop up. We don't want our work to be for nothing. Um, okay. We get actual HTML validation. Let's have a passwords that mismatch. That's an easier way to do it. Uh, it wants me to have an email atgmail.com. Okay.

HTML is saving me from uh having a a demo. Okay, there we go. Passwords don't match. Our error message works. and one password you can just screw off. Okay, I don't want you. Cool. So, one last thing before we give this a rip. And this is a self kit thing that is brand new and something that you might want to turn off. Uh I personally really like this. This is a newer thing.

So, you'll see that the uh goto has a red underline and it says unexpected go-to call without resolve. Resolve is a new function inside of spelt that basically confirms that the route exists. This thing is saving you from yourself. So that way if you change your routing structure or you try to do some kind of routing and it doesn't exist, uh resolve is going to complain. You can turn this off, but I like resolve. Um one thing that I I have noticed though is that this is so new that I it doesn't import for me. I don't know why it doesn't autoimp import and I've like become so reliant on my autoimp imports that it's obnoxious. Um that said, maybe I'll just make a snippet. We're going to import resolve from app paths. Okay, so we have app navigation and app paths.

And you can see that this says, oh yeah, you do have a forward slash route. If we were to say um if we were to say forward slashcott, guess what? This errors. We don't have a forward slashcott. So again, resolve is looking out for you. And I like that. So I'm not being super defensive in this implementation, but that's okay right now if the signup does in fact work. What we should see is we should see this redirect. However, anybody who's worked in O systems right now will wonder what the heck how the heck this is even going to work. So, let's submit this. And it's going to fail. But I know why it's going to fail, at least. And that's half the battle.

I'm going to actually pop open my network tab so you can see. We're going to say my username is Scott, my email is my email, and my password is syntax is great. syntax is great. Now, this time, even though the error message didn't clear, that's my bad on the usability side of things, but you will see in the network that we had an actual failed request. We had a 404. And so, it did try to hit our server. And if you check this request, it tried to hit our server at a route of API o signup email. Now, if you've worked in this stuff for any amount of time, you'd be going, okay, where does that where does that exist?

Does that somehow magically exist when we created our server client? And the answer is no. Uh we need to tell speltkit that this route exists. But there's a lot of different routes that you might have for off like tons, right? Forgot password or sign up or whatever. So uh better off allows you to have a middleware that steps in here and registers all these routes. So that brings us to middleware. Okay. So middleware. Now, middleware is a really nice thing in most uh web frameworks that basically allows you to step in the request process at any given point.

Chapter 9 48:02

Hooks/Middleware

Various frameworks use middleware in different ways. Speltkit has a really nice middleware system and their middleware system is defined by simply creating a hooks.ts file inside of your source directory. So, inside of src, we want hooks.ts.

server.ts. Now, inside of hooks.server.ts, you're typically expected to be exporting a handle, something named handle. So, if we were to say const, actually, let's export const handle like this is going to be equal to an async function. Async. And I couldn't tell you why I always make these arrow functions.

They don't have to be. They can just be in any function. I This is just how I've always done it for some reason. And this is going to be the handle that steps in. And the way that middleware works is that you have access to the request is coming in. You have access to it. Then it does all the stuff whether that's rendering out the page and getting all that stuff done. And then the response is going back to the client and then you have access to it again. Everything works like a sandwich. Each middleware is like a piece of bread and then it's like a sandwich if you had the same toppings on each side of each thing until you get to the center which is your routes. Not everybody builds sandwiches like that, but maybe you do.

So, let's go and show how this works. So, we're going to say console.log, and we're just going to say page hit. And then we're going to um also have another one which is after page hit. This one should be before before page hit. After page hit in the handle, we get access to the complete event. We get access to a resolve function. We get the access to the whole request. You want cookies, you want something called locals, we got it like uh you got everything in here. In fact, if we want to get proper TypeScript stuff on here, you can define this as the type of handle.

Um, it's currently not happy with us because of we're not returning a response. [snorts] So, one thing that you get out of here, like I mentioned, is the event. You also get a resolve function. So, what we do here is we say before the page hit happens, then let's go ahead and actually resolve the request. So, we're going to say response is equal to await resolve event. Now, I should say it's not necessary to get this working to get better off working because you can just drop in their middleware and call it a day. But I do think it's worthwhile to take the the time to learn how to do this stuff. Um, the next thing you do is re return the response. So, again, it works like a sandwich. We have bread, we have the page rendering, then we have after the page rendering, and then we return the response. And that's basic middleware.

Okay. So if we want to see this in action further, we're on the signup page. We can actually console.log. We can have console log. We can have page load. Okay. Now if I refresh the page from here, since we have server side rendering out of the box, you're going to see before the page hit, page load, and then after page hit. That's the order in which things are happening. And then typically what happens inside of your middleware is that you end up having multiple middleware. You had um handle two. You would never call it handle two. Um or even this one. We could call this one log. Log and then log two. How about that?

And then we wanted to have both of these run. Have both of these being toppings in the sandwich. Here you could have export const handle. And then you use something called sequence. Now this is something I use in just about every project. Sequence is just an import from uh spelt kit hooks. And this basically is a function that just accepts a bunch of middleware. So you could say logger.

You could say uh or not logger log log two. And now they're going to run in sequence. So we could say log two just to make this nice and clear. And I I promise we'll be done with this part in just a second. We just want to make sure that you have the concept of middleware here. That way when we go to use it in real world, you got it. Okay, so we're running these in sequence. We have log and then log two. So take a second to think to yourself, how would you expect these to log out? Okay, the answer should be that we have before page hit, then we have before log two page hit, then we should have page load, then we should have uh after log two page hit, and then after page hit. So again, it's like a s it's like a pyramid up and then down, right? And sure enough, if we refresh our page, I'm zooming all around here. You can see

that exact sequence that I mentioned happening happens right here. So all of that to say, we want to bring in the better off middleware. So we're going to create a new const O handle. It's commonly how it's named. This is also going to be a handle. And this is going to be equal to an async.

Chapter 10 53:30

Add Better Auth middleware

Uh again, for some reason, I just find myself doing these as arrow functions. They don't have to be okay like this. And we're going to return something called the spelt kit handler. And this comes in as an import from better offspel-en kit. And this just accepts a few things.

It event the event. It accepts the resolve. And it actually needs to accept our o which we haven't brought in. So let's bring that in. That's the O from lib off. This was the server side better off client. So event resolver O and then the last one is something called building. Now building is just a true or false that comes in from spelkit. This is from app environment.

Building is interesting. I kind of find it kind of odd they need this but it does. Basically it analyzes your app during the build by running it. During this process building is equal to true. So while the app is building this is equal to true. I suppose that was the only way that the better off team could get that information. That's fine. Okay, so we have this. Let's use it inside of our sequence. A sequence can be a sequence even with one thing. So just like that, I'm going to keep it as sequence because typically you add on more and we will in this course. Okay.

So just by doing this, we now have registered all of these routes. This includes the forward slash API slash off all those routes that it just tried to use. So if we actually try to do this now, Scott, okay, uh, syntax is great.

Syntax is great. If yours succeeded here, congratulations. If it didn't succeed, check your your error logs. Check inside of here. Check to see what was returned from this. If it's a 404, that means that the server didn't get the route somehow. That means there's an issue inside of this. So, build a fake little route handler um in a sequence or whatever. Refresh, make sure that console log is happening. Uh confirm that that middleware is being loaded.

And if if that's the case, then if that's the case, plus your Better Off URL is set up correctly, then there's no reason why you shouldn't be able to correctly see that this logged you in. Now, what happens when this logs us in, we do have our our studio open still, or at least I do. And you can see it did create a user. Um, it created an account. It created a session. There's no verification. We're not verified.

That's not something we'll be doing in this course, but it's easy enough to add with the better o docs. And just like that, congratulations folks. You have yourself a working signup form on a database you own and you own it. How cool is that? So congratulations. You not only written your first off, but your first middleware. We're going to be running another middleware in just a minute here. But before that, let's talk about page load functions. There's a special file. We have page.selt. spelt and layout. With the with the plus sign, but inside of routes, you also get access to another type of file, which is the server file or even just thets file.

Chapter 11 56:50

.server.ts files

So, inside of any given route, you can have a new file, and this can be layout or page.server.ts. Now, if you have the server on there, it only runs server side. If you do it with layout.ts, TS it runs server and client side. If you're asking me, I find the the non.server files, loader files to not be that useful. I think uh they can be useful, but in my personal usage of spellkit, I almost I never use them really. I there are times when I use them, but it's not my most used feature. So, what's up with these files in current current versions?

And as going back as far as you know spelkit this was how you loaded data into your application all the time. If you wanted to bring data into your application you did it in the load file add a route and it passed that data into props and that was the only way you could do it. Now, with the version of Spelkit that we're using today, you can use remote functions. And remote functions tend to be how I load my data most of the time now. But the load functions, I don't know if they're going away in future versions of spelt or anything like that, but they can be still handy for a number of reasons. And let's go ahead and I'm going to show you how you write one of these things. It's just an export function named load.

export function and uh it named load like this. That's it. Um if this can be async if you want we can return from here. We can return name is equal to Scott from here like this. If we have this and you head to your the corresponding layout or page file like here this now becomes available as data and inside of data we could output data name and if I come to my site here you can see it is getting Scott output now I don't know why TypeScript is mad let me restart the language server uh that could be a thing typically spelt kit what it's doing is it's generating these types. So you can see now when I hover over this, it did in fact get that proper typing. Um, spelkit is awesome in this regard. If you have your data being passed from a server function to a

client function, either that's remote or loader function like that, it's almost always 100% typed directly for you. So just by doing this, Spellelkit knows that this data exists and it's a string. And not only that, we haven't really touched on this, but this is all being serverside rendered. Meaning, when we go to the HTML of this and we look for Scott, that is in the HTML that comes back from the server. If you want serverside rendering for your entire app, congratulations, you have it. And if you don't want it, you just come into your layout.server and you do export const SSR equals false. Congratulations.

That's it. You refresh. And now all of a sudden there's no more pesky HTML. It's just JavaScript baby. So let's go ahead and remove that SSR equals false. We're writing an SSR based site right now. Okay. So this is a page load function. This runs only on the server. Meaning that you could do database stuff here. Absolutely you can do database stuff here. But not only that, you get access to a number of things in here. You get access to cookies. You get access to uh the URL. You get access to a whole lot inside of here. The thing that ends up getting used the most for me is beyond cookies. That's something I use quite a bit here is something called locals. Locals is basically an object that gets used to pass data from uh even middleware down to every single load function in your entire site. So this is a layout load function. You can do this exact same file on a page.server.ts ts and it will do the exact same thing

except for pass in data to the page itself rather than to the layout. Um you can also again like I mentioned do it without the server and have something that runs on client side. Maybe that's a fetch call that's going off and grabbing data that you want um something that could happen on the client side. But if you're doing any database work it should always be in a server.ts file.

And for the most part, we're not going to be using these that much more in this course, which is crazy because this is how you've always done data loading inside of Spelkit, but we're going to be primarily using remote functions here as we go as the future way of doing this and something that I think a lot of people are really interested in. So, we have our signin process. If I were to go to forward slash off slashsignup, it's going to let me go to this page.

Chapter 12 1:01:51

Auth guards (+ locals)

It shouldn't let me go to this page, right? I'm already logged in. But how do I know that I'm logged in? Well, there's a number of ways you could do that. You could come and use your better off client setup or we could uh just pass that user information along into a page and on a server load function check to see if that exists and head that redirect off before it even happens and redirect you back. Right? A little bit of an offguard. So that's what we're going to do. We're first going to grab the user information in middleware.

We're going to pass it into locals. From there, we're then going to have a load function on a layout inside of O that intercepts it. And hey, if you're trying to get to the O pages and you're already logged in, let's get you back to the main site. Huh. Okay.

So, we're getting to write our second middleware. Pretty pretty uh pretty exciting. I I love middleware inside of Spelkit. It just feels right. So, we're going to make a new one. It's going to be const session handle and the session handle is also going to be equal to a type of handle and it's going to be an async function. And once again, my muscle memory just made these arrow functions. Consistency, right? I guess nothing else. Okay, I should have a snippet for this. I do this enough. Now, from here, we can use a better o method called get session from our o client. So we can say const session is equal to await and we're going to o do uh I believe it is api get session and from here we're going to pass in the headers and the headers come in from eventrequest headers so we have access to the actual

uh headers coming in on any request. I'm get some red in here. I'm going to save this to see if what I goofed up because there's always something goofed up if you're seeing red. Event is possibly undefined. There's uh I spelled async wrong. One little async and that can mess you up. Okay. So, at this point, if we console log out session, this should grab our session information from better off, which is great. Um, we do need to really quick though. We need to we need to actually do our resolve. I'm gonna just copy and paste this. We need to uh resolve our event and then return the response and then we need to use this session handle. Okay. So, with that said, what we should be able to see now when we refresh the page, we should be able to see the session being console logged. So, let's go ahead and do that.

I'm going to refresh the page, check out my term, and check it out. There's the session information, there's the user information. We're building off systems. This is gold. Anything you could possibly imagine is now available in your middleware here. You could actually intercept the request and say, "Hey, if the person's trying to go to the O pages, uh, hey, stop that.

Let's bounce them back if they're logged in at this point." Or you can hot potato this through locals and do it in a page load function. You could do it either of those ways. I'm going to do the hot potatoing because that's how I prefer to do it. So, let's go ahead and utilize something called locals. Now, locals, as we mentioned, is just kind of a place to put stuff. And that exists on the event.locals.

So, we could say event.locals and then do user is equal to the session. Um, we can do a dollar sign um, optional chaining here like this. Now, this is all you have to do. Let's remove this session from here. We're grabbing the session object from session if I could spell it correctly. There we go. I'm grabbing the user from inside of session and I'm setting it to locals. And I don't have to pass in locals or do anything else with it like there. But if we were to actually come to any of our page load functions now, any of them, and console log out locals directly from here, you're going to see that user exists inside of here. Meaning that we'll now have access to the user information in a page load function sitewide. Uh, extremely useful. So, I refresh my page. I check it out. Here's the user. All the user information server side only. Available to check

roles. available to check if they're logged in or anything like that. Really super great. Now you will see some TypeScript errors which leads us to another topic. I love to talk about this stuff. So you will see a TypeScript error. Property user does not exist on type of locals. Super common error, super common issue, easy to fix. We do it in app.d.ts. I mentioned that I'd be showing this later. And here's where you define the global type stuff. In fact, locals is even here for us. commented out. So we can uncomment uh locals and we can add the property which is user.

Now the property for this I believe this is kind of an interesting uh better off thing. Better off folks if if you have a better way to get this type a a better off better way to get this type let me know. I found this to be a little unwieldly but hey it worked. So it's type of and then we're going to import uh dollar sign lib. So our o client.

So from our o client, we're going to get the oclient dot infer dollar sign infer session. user or undefined. Okay. Uh like I said, it's a little unwieldly and you could mess this up.

But if we hover over this, you can see that the user information is in here or undefined. And in fact, it works nicely enough that our TypeScript error has gone away. Session user is equal to that thing that we just pasted or typed in there. Uh you can always copy and paste it from the show notes or the uh the show notes the code example in this.

Cool. So we now have a complete O system where we have access to the user anywhere we go. Couldn't be any cooler here. I'm a big fan of how all this stuff works. And a lot of this stuff works once you get it set up. You can just keep ripping on it. Okay, so let's go ahead. I mentioned we wanted to have a layout load function that kind of stepped in the way here. So inside of O, even though I do not have a forward slash page, I'm going to have a new file here. And this is going to be plus layout.server.ts.

And we're going to have an export uh function, actually an async function. Async function load like this. weird that I'm not using an arrow function for this one, but I do the others. I don't know. We'll also get locals. Okay, remember everything that's on locals is uh what it's our user information. So, that's great because that means we can then step in here and say, "Hey, by the way, user is not logged in. Let's redirect them or the user is logged in.

Let's redirect them." So to do that we can just say if locals do user and then we will need optional chaining here do id if the user locals id does exist and they're trying to access the forward slash off routes then we're going to throw this is kind of a weird spelt quirk is that you throw a redirect not a reddit a redirect like this and The first parameter here is the type of redirect and the second is where the heck you're redirecting to.

Okay, like this. Um, it imported Reddit so you can log in with Reddit from Better Off today I learned. Okay, the redirect function actually comes from atfeltjskit. Now, I will say it's not worth memorizing where redirect uh app uh navigate or any of these things that get imported or resolve, it's not worth memorizing where any of them come from because to to be honest, like whether between resolve or whatever, there's a rhyme and a reason to it and I'm sure it makes a lot of sense, but for me, I've never even bothered memorizing it. I just autoimp import everything. If you're in a text editor, you can do command plus and that brings up code actions. It allows you to import that way. VS Codez, whatever you're using should just work. Otherwise, you can just type out what I'm writing here, but I don't re recommend that that way of working. Okay. So, if we save this, we should see possibly this redirect work.

Congratulations. Now, some things to note here that are interesting. One, we were able to do that and we didn't have to have a layout page file. We didn't have to have a layout uh page file or anything like that. All we had was a layout server ts inside of the author route itself. And that wraps and encompasses both login and signup. So even though forward slashlo doesn't even exist, um it will hit us with this redirect right now because it's not going to resolve a page. Let's actually create a page here real quick for login. Refresh this. And you can see we're already redirected. So that just works. Just works. So cool.

So congratulations. You've now done a number of interesting things. We've implemented an offguard using a layout server file that is getting information from middleware passed in via locals applicationwide. We've also written a couple of middleware ourselves. One of which was just kind of hot potatoing some stuff to better off. The other one was actually well grabbing the session information and making that information globally available. You'll use middleware to do all kinds of things from initializing sentry which we will do in this course uh to setting up logging. I use it uh myself for all kinds of stuff. So it's nice to have a good handle on how it works. So let's do a little bit of cleanup here and move on to the next thing. I want to get rid of that Scott on the uh site here. Just this data.name. We can get rid of this for now that we have the concept of how this stuff works. Probably not worthwhile to keep. So, just uh to keep things a little bit clean here. Okay.

So, now it's time to get into some of the cooler, newer stuff. And what we're going to be doing is we're going to get into remote functions for both querying data as well as mutating data. We're going to be talking about the form remote function and we're going to be talking about routing a base uh routing a bit as well. Let's go ahead and what I want to do is I want to create a new folder which is just going to be admin.

Chapter 13 1:12:45

Adding an Admin Section (+ Drizzle schema)

A admin is going to be a legitimate admin section of this site. Inside of here, we're going to have a new file which is going to be page.sfelt. And we're also going to want to have a layout. So, plus layout.server.ts.

Um, I just really butchered that. We're going to say layout.server.ts. My palm hit the uh keyboard when I was typing. Okay, there we go. So, for this admin page, we're going to want to create a blog. We're going to want to show all of our posts and we're going to want to be able to create a new post or even update the one that we currently have.

Right. So, before we can really get into that, I suppose we actually need data. So, this will be the second time we modify our database. We're going to head to our schema file and we're going to create some new database tables. And when I say tables, I mean uh table. We're going to have one new database table because you could get crazy with this and build out a legitimate crazy blog here, but I just want to really show these techniques. So, we're going to be building just a basic post system.

So we're going to say export const post post is equal to a pg table and pg table is going to accept the first parameter is the name of the table which is going to be post and then the second parameter which is going to be what the heck's in this thing. So we're going to have one that's an ID. The ID is going to be a uyu ID.

It's going to be default random and it is going to be a dot primary key. Here we go. Now, what else do we want on here? You think about blog posts. What do you have? What do you have in a blog post? We typically have something like a title. Uh you would have a slug, which is a um parameterized a URLized version of the title. So we could have a title which is just going to be simply a text and this going to be not null. Then we're going to have a slug which I'm going to manage these my own myself which will also be not null. And then there will be a body which is just going to be a text which is going to be not null.

And then uh we're going to have a created at um created at. It's nice to have created at update at those types of things that are going to be a timestamp. You know what? Instead of just typing these out by hand, I'm actually going to copy the expires at um or I'm going to copy the created at and updated at directly from the other schema we have. Not only does this keep things to be like the same, uh, but I also just don't want to type it out. So, we could have the created ad and updated. If this is a real blog, you might have a published at as well. Now, what's interesting, we can also do an author ID in here as well. We can say text and uh I believe it's a text. Let me confirm that it's a text. Yes, the uh most of their ID fields in Better Off are text fields. So, we can say it's text and it's going to be author ID.

It's going to be a references and there'll be a function in here. And this is just going to be the user ID, comma, we're going to say on delete, we're going to cascade. So basically, if the user's deleted, their their post should be as well. Okay, perfect. So, this feels like a pretty basic schema for a blog post, but like it's got all we need. So, I'm ready to hit hit go on this. We can go ahead and another tab here and do PNPM DB push.

Hopefully, nothing goes crazy here. We just say yes. And that's all good. If we want to confirm this is all working again, we can always refresh our Drizzle Studio. It's nice to have this open. You can see post exists. Congratulations, we did it. Cool. So, our database is ready to go, which means that like we're we're cruising. So, now before we actually get into mutating posts and dealing with forms and stuff, let's start with the very basics and query a post.

How do we do that? Well, inside of your Drizzle Studio or tables plus or whatever you're using to manage your database, you can just add a record. I'm going to say this is test. The slug is going to be all lowercase test. The body is going to be look at me. Um created by default default. And the author ID is going to be equal to um let's grab a user in here. Um how do I grab a user from here?

Let me just grab it from our console log. The user ID and let's save. So, it did connect the correct user to it because I grabbed the user ID. If you don't have this user ID handy, you could have just bailed out of that, gone over to your user, grabbed the user ID from there. Um, again, it's really just you can follow along here if you'd like, but we just wanted to get something going. So, let's go ahead and we want to head to the admin page, which is on our routes/admin and page.spelt.

inside of here. What we can do is we want to have a little bit of a listing of all of our pages. So, let's go ahead and create a script tag in here. And this is going to be where we get into remote query functions. Remote query functions allow us to essentially run a function on the component, whether that is client or server, and then Felkit just handles the rest. Uh, it goes off and does the server side stuff for you.

Chapter 14 1:18:50

First Remote Function

fluid. It handles the request for you and it's basically just like running a function and getting your data back in the component with serverside rendering or otherwise. It is awesome. It rules. So, you're going to love this. So, inside of this component, we're going to want to query for all posts. And then once those posts are there, we're going to want to list all posts. Simple. So, let's create our very first remote function. These things are awesome. I'm going to do it in the admin folder. You could put this many other places inside of a lib folder, whatever you want. The new file, this is going to be posts.ts.

Now, posts.reote.ts is going to have an export const get all posts. I'm doing this in snake case for some reason. That's how I like to work. You can have this function be whatever. This is not a magically named function. Uh, this is just a function. And this is going to be equal to a query. Now query is a function that comes in directly from speltkit. So when I add this, so when I add this missing import, you can see came in from dollar signapp forward slashserver. Again, probably not worth memorizing all of these. Just know that it is. Now query is a function that does what it says it's going to do. It's going to query. And this has a function as a parameter just like so. Typically, you might see these as async. I almost always have these as async but it this is a function when you return from here return hi Scott like this. If we were to then call

this on the client side here we can use this we can import this directly from our remote. Now again you can import it or you can just start typing like I do. So const data is equal to await get all posts which then will autoimp import from post.re remote like this.

We can then output just data here like so. Now, what's interesting is that this is fully type safe. So, we now have full types across the board here. And notice how we didn't define a single type. The types were just inferred all the way from server to client. And you can see if we go to forward slashadmin now, yes, object object, I believe it was hi.

And this should say Scott. Beautiful, right? So that's awesome. And it just works uh just by importing a remote. The cool thing about remote is that these are just server side functions. So if it's a remote, you know that it's running on the server, meaning that you know you you can do can do database stuff here. Oh my god. Okay. You can do database stuff here. This is remote. You don't there's there's no concern about the client server barrier. Unless you're sending in secure information from this to that, you don't have to worry about it. Fantastic. So, let's do some databasing. We love databasing. And with uh Drizzle, it becomes nice and easy where you can just grab all posts. Um so we can get all posts and that becomes as simple as like we can really reduce the code in here and we can simply return db

which is our database instancequery dot post dotfind many and that's it. I mean we're just grabbing we're not putting a limit on this bad boy or anything. We're just saying grab all the posts, get all of them, and then now check this out. But because Drup uh because Drizzle is type safe here and this is all type safe and this is all type safe automatically that is type safe. Meaning that we now have posts here is type safe. It's an array of all posts. We didn't have to write a single TypeScript type and the whole thing just knew about it. So, we can do dollar sign each posts as post.

Gosh, I can't get over how cool that is. I love that. And then from here, we could say what's in here. I believe since we're doing just a uh you could do this as an ordered list, you can do it as an unordered list, you could do it as paragraph tags, you could do whatever the heck you wanted to. Let's uh let's go nut nuts with it and do an ordered list, right? When was the last time you saw an ordered list? We can do li in here. I'm sure all the time we could have post.title.

Okay, now this is complaining about a key on our loop. But if we refresh this or even just come back to it, you can see that not only do we have our initial blog post here, but if we check the server side rendering, the blog post is serverside rendered as well. So simply using an await keyword inside of our spelkit components just stinking works.

Now another cool thing you can do with this is if you don't need access to posts within the upper part of your script here, you can simplify this even more by awaiting this directly in your each loop like this. Get a load of this. You can do this and it just works. Boom.

It just works. How amazing is that? uh spelt doesn't care. Just toss some a weight inside of your components. Now, you can see it is giving us it's complaining about it's missing a key. So, let's do the whole uh post ID thing to grab the post ID as a key. Um better have keys than not.

React folks uh are used to adding keys. So, you can add keys. You can also not add them too sometimes. Uh but it's probably better to add them. So, check it out. We now have our post listing. And could this get any more simple? We have a oneliner for a database call. And we have this in the client side. How cool is that? Anybody who's worked in other systems knows that this stuff doesn't get this easy. And this is so clear. It's not like magic. It's just what this is doing behind the scenes if you care. Is that when this um let me even navigate back here. when it navigates to this page. If we had actual client side navigation on this thing set up going right now with some kind of nav, you would be able to see that this is simply just running a fetch call.

Chapter 15 1:25:27

Mutations in Remote Functions

That's it. It's running a fetch call. Or on the server side, it's not even doing that since the the information comes directly from the server. So this is how you use query inside of spelt with remote functions. Now, not every remote function query is going to look like this. In fact, just about none of them will. Uh you can accept uh parsed parameters and type safe parameters into them that you can then use in a really nice way. Uh we also have other means of doing mutations within remote functions as well. And we're going to get into all of those very soon here. In fact, we're going to be getting into that right now as we build out our blog post form. So, let's get into it.

This is going to feel maybe a little bit backwards, but we're going to start with the database stuff happening first. We're going to be writing our remote function first and then we're going to write the UI. Why? Well, you're going to see why because it's pretty badass and it it's going to really really blow your mind here a little bit because it blows my mind every time I do this. So, what we're going to want to do is we're going to want to create a post function here.

So, export const create post. And I'm sorry I'm being inconsistent with the uh camel case and stuff. You can do this any way you'd like. And this is going to be the type of not mutation. It's going to be the type of form. And let me tell you, there's a couple ways to do mutations with remote functions. One is command.

And the other is form. Form is going to be the one you use the most often. It uses HTML forms. It progressively enhances them and it's going to blow your mind. So form also comes in from AppServer. And just like just like query, it takes two parameters. Now the first one I'm going to omit for right now and we're just going to say null for right now. I'll change that momentarily.

It's going to get mad. The next one is going to be the function the async function that runs to do your work. Okay. So do work and then whatever we return from here is just gets passed back to the client side. If you want it, you don't have to like return data from here. If you don't want, you can redirect. You can do all kinds of stuff. So, the reason I omitted this first one here is because this accepts a validation in here. Now, to validate this, they expect you to use some kind of standard schema plugin.

Those include Valibot, Zod, um they also include arc type. There's a whole bunch of different ones, but they have to follow the standard schema. And so I always stick personally to Zod, Valibot, or Arc Type. I found no reason to use anything else. And we're going to use Valibot in here since that's what the Spelt team uses in their docs. And I do really like Valibot. Um, so we have to install it first though. Uh, let's come to our dev term. Let's pop open another one of these. NPM npm add valibbot.

What this does is it validates data. It uh does gets your types all in order. It it's basically a means of making sure that the right data needs to go to the right places and that everything is correct. Okay, that that's uh there's a much more there's a much more technical definition to how all this stuff works, but it's not necessary for right now. We want to validate our data. We use Valibot to do that.

Okay, Valibot is installed. Nice and easy. I like Valibot. It's a nice small fast library. It does its job. Let's import V or let's import asterisk as V from Valabot. That's just how you have to do it. Uh you're bringing everything in and you're setting it to be V. Now from here, we're going to define what the information coming into a blog post is. Like when we create a blog post, what do you really need? You need the title and the body because the author ID comes in from the user session created at updated at those are just automatic and then slug is generated from the title right so to do that you use something called v.object.

So as you can imagine we're saying hey this thing is an object and what's it going to have in that object? It's going to have a title and we're going to have what? It's going to be a vstring. Now you can get crazier with this if you want. Um beyond just saying a string you can say v.pipe and you can pass in multiple things like v.non non empty and you can even give this validation text like title is required right you can also do all kinds of validation here whether that's length or or whatever right this thing is going to protect you from all kinds of stuff we can say body and we're going to do vp pipe again and this is also going to be a vstring v dot string and and voton empty.

I don't even need some text in here. This one's fine. Like this. We're getting some red squigglies, which means I definitely missed a pen here. There we go. And that's it. Now we have access inside of our async function to a type safe title and body.

Not only that, but we know that they're going to be nonnull, right? Because we say they can't be empty. And then we know that they're going to be strings. So that means reasonably we can do all of our database stuff inside of here. And this is pretty great. Now you might not understand the need for this bit just yet, but in about five minutes from now, you're going to be so stoked.

You're going to be really excited when you see what this can do. So we have this function. We're going to first we're going to get the session data. Now the one bummer about remote functions is that they don't have access to spelt locals or speltkit locals. So that means we have to do this whole rigomear roll of const event is equal to get request event. Request event is a function that comes in from app.server.

I I would prefer to just have locals in here. uh const session is now going to be equal to await oapi.get session. Now we see now we're doing this double work of having to get the session here. Um let's bring in that from lib off. And then once again we do need to pass in headers and this comes in from the event.est headers. Okay. So this gives us our session data. Okay. From here we can check to make sure the user actually exists. So if session user id if the session uh we'll we'll check to see if it doesn't exist first. If session do user id is null then we're going to error 401

and we can say unauthorized. Okay, error is another import that comes in from this time spelt kit. So again once again kind of the rhyme or reason where these things are coming in from it probably makes sense to someone. Uh does not make sense to me. Uh I that could just be me. Uh I can I can fully vouch for that. So if the user does not exist, return unauthorized. In a bit we'll add rolls and we can do that in a ro check there.

Uh we'll also want to create a slug which is going to be equal to the title. We can do this in a very simple slug and just say two lowercase um and then uh um just a quick little reax here where we're replacing all spaces with hyphens. Just a really garbage slugify here, but um something just to get on paper. And then we'll want to do our database. So we'll say await db.insert.

And the way Drizzle works is you need to then bring in the schema. So post from our schema. You can see it's been imported here. Post from our schema values. If you've used Drizzle, this stuff will all be old hat and you'll breeze through it. If you've never used Drizzle, some of this might feel interesting, but just follow along for now. [snorts] From here, we're going to insert some stuff.

What are we going to insert? We're going to insert the title. We're going to insert the slug. We're going to insert the body. And then the author ID, author ID, if I can type this out, is going to be equal to the session dot um I did I did mean to write author ID. And I did write it that way the first time. And check it out. Our TypeScript errors are gone. Uh title still has a red squiggly. Um it's because I misspelled that up top here. I wrote tile.

Typescript really saves your bacon. saved my bacon right here. So, this is awesome. This means that we can now create a post. And when we're done with this post, what do we want to do? Well, for now, we're just going to redirect. So, we're going to say redirect and we're going to do a 303 and we're going to go back to forward slashadmin for right now. Um, we can go somewhere else later. I'm going to import redirect. Redirect comes from the same place that error comes from. Okay, so check it out. This is what the top imports look like. Pause this here if you need to copy these. Sometimes this stuff can move a little fast, so my apologies there. The create post function again, it accepts an object with a title and a body that are not empty. We do our work here where we get the current event. We get the current session. We check to see if the user's there. We generate the slug. We insert in the database. And then we redirect.

Now, this isn't that defensive, but I'm want to show more of the spelt kit features here than I want to show fully defensive programming. So, now that we have this, we need to create our form. So, let's go ahead and we're going to have a new folder, which is post, and then another new folder named new.

Chapter 16 1:36:03

Connecting it to a form

Inside of new, we can have a plus page.spelt. And this is going to be our form.

Now I do want to import our new function from post.reote which was create post. I want to import that. And this comes in from I believe dot dot dopost.reote like here. Why are we bringing this in so soon? Well, I'm about to blow your mind with one of the cooler features of this stuff. We can actually use this form remote function to do so much work for our form system. So we can say form on here and then you can spread dot dot dot create post.

Now this doesn't look like a whole lot. Nothing's going on here. But let's go to post new admin post new. Okay, this page exists inside of our HTML. We scroll down. We look inside of here and you have a form with what in it. Now, it has a method of post and it has an action in it which is pointing to a remote function URL that was created by spreading this in here.

This form is now progressively enhanced with JavaScript. As in this form will work with JavaScript without doing a full page reload if JavaScript is available. If JavaScript does not, this thing is going to function like a regular ass form. That's incredible. Uh that that just works out of the box just by spreading this in here. And not only do we get access to that, but we get access to field validation and field types. Check this out. We're going to say dot row. And inside of row, we're going to have a label. Get rid of that for text. And the first one is going to be title. Okay. We're going to have an input here. And now, typically, you'd reach for input.type. But now what we're going to do is we're going to do dot dot dot create post dot fields dot what? Title

what? Uh but it gets better than that. We're going to say what type of input is this? We're going to say as text. Okay. Uh this allows us to control what type of input it ends up being. Right? Sometimes you want that control. So let's take a look. And and look, there's no red underlines, no red squigglies.

We're getting an input on here with this information. We didn't even set an input type. If we come in here and we look at the actual generated uh DOM code right here, you can see that we have an input type. And right now, it is just a name of title. The by default, a text type is just a text type. So, it doesn't need a type. But if we were to say as email, this might this will get angry, but uh if if we check this out um and we inspect this, it does in fact get the type of email. Perfect. And it's also validated per the rules that we had in our validot schema. So this stuff matters because now it informs what's happening on the client side and it informs what's happening every step of the way from the client validation down to the server validation and it will inform even getting errors onto the screen. Um in fact if we want to see that in action you could do that and

just below this label we could do an each because they'll there could be more than one issue. So you would say create post dot fields uh make sure I'm spelling everything correctly. Dot uh title issues is use as issue. Okay. Sorry, for some reason I was having a hard time typing that.

Each and then you want to have a key here where we could say issue dot I could say I suppose it's the issue message. I'd probably make this something else if I'm being serious about this. But from here, you're going to then do the same thing we did where it was uh P and then we could have issue message. So anytime there's an issue with this form input, it's going to output it here based on what we know about these form inputs. So, we'll be able to So, we'll be able to see this in action a little bit down the line, but just something neat that you can do here. Now, another thing we want to do is we want to have another row. This is going to be this time another label.

This is going to be the body. And we're going to be using a text area. So, we're going to just say body uh text area. Um, and uh zed is trying to help me out here, but it's not. I'm sorry. text area cannot be a self-closing component text area. Now we can do the same exact thing that we did with our prior one. So we can do dot dot dot create post dot fields dotbody and then as text and just like that uh we end up having a form. Now lastly, you'll want to have a button which is just a post and this button will be a type of submit. Okay, perfect. So we now have this blog post

and again this has become very simple but again we're able to essentially create a form where we don't have to have any controlled state but the state ends up being controlled essentially by this create post remote function that then becomes all validated the entire process. the fact that we can write this and even if it looks maybe a little bit messy with some of the spreading stuff, the fact that there's no types errors, there's it just all meshes really well all the way down to the server side where this information is going to come in. When you see this, it's going to be magic. So when you when you have a form here, what you might expect is that we run this form, it progressively enhances so that it runs with JavaScript. that create post function will get called.

The data will come into here. It will do all of this stuff and hopefully redirect us back to our admin. Okay, so now let's see this in action. We're going to say hello from SK. Um, hello. Okay, let's hit enter. Let's post this and check it out. We've been redirected back to our listing of posts and they just exist here. This is incredible that this just worked for us and worked effortlessly. Uh really super cool. Now, what we're going to do next is we're going to create an update form. Uh and we're then we're going to create a form component that encapsulates the update and the insert form. So that way we can uh show you even how to make this stuff even a little bit smoother using these remote functions. From there we're going to continue just to explore spelkit topics. We're going to talk about server endpoints. We're going to be talking

about uh the different types of using remote functions. And we're going to make this app a little bit better and nicer overall visually, aesthetically, but not spending too much time on it. So uh check it out as we continue to move further on this project. Okay. So let's take actually a little bit of a detour before we get on to some of that because there are some concepts that are well used in speltkit that we haven't touched on. They don't necessarily fit this video or this course in any sort of meaningful way. So I think it's important to take a small detour to talk about endpoints. Now, so far most of what we've seen with data loading in feltkit is in regards to RPC style where we have uh the a tight connection between the client and the back end. But what about writing APIs? Uh having an API served from your application or simply having an endpoint. Well, endpoints, believe it or not, are extremely easy to do in Splkit. And

Chapter 17 1:44:17

Detour: Server Endpoints

would you believe that they actually fit in with the general routing structure of it all? For instance, if we wanted to have an endpoint that just returned the JSON of all of the available blog posts for us, since we have blog posts now, we can do that. So, I can show you how to do that really quick. What we want to do is we want to create a new folder route and we're going to call this API. Now, this isn't something special calling this API. It is kind of a common convention. And you'll see people create an API folder. That way their uh address is forward slash API slash whatever. Now from here we can create routes just like normal. This is just a normal route, right? But if we wanted to say that it's the API and then posts inside of the post directory we can create a plus server.ts file. So we now have a page.server.ts.

We now have a page.ts. We have a layout.ts. A layout.server that server.ts, but we also have a just straight up server.ts. And a server.ts is how you create an endpoint. You can create an endpoint of any type with this, whether that is a post request or a get request or literally any of the other verbs uh that exist for HTTP requests. So, let's go ahead and I am actually going to do this as an arrow function. Export const get.

Sorry for the inconsistencies. I do it this way because you can do the types. You can say that this is a request handler from speltkit like this. And this is going to be equal to an async function. Okay. Now, inside of here, you can see that uh we have access to a number of things just like you'd expect.

Now, inside of here, we have access to all kinds of stuff like the locals, right? Like if we want to have the locals, anything that we put in our locals, whether that's the user information, everything that you get typically you can get inside of here, you can get access to the URL, right? Just like what we were used to with the page loaders. Um, however, what we also get access to is the ability to do our database stuff and just return data out of here. There's even a helper for JSON if we want to return JSON. Um, so you could do const posts equals await and then we could get all the posts from our DB. We could do DB. I believe it is just query post find many like so. This is going to give us our post. This obviously needs to be an async function. Should just make them all async, right? Uh, and then we're going to return JSON and then it's going

to be posts. Now this JSON is an import that comes from atkit uh speljskit and there's also other ones too that's the same thing that like error comes from redirect comes from um we've used those already um but JSON is one for returning JSON from out of here. So at this point we should be able to hit which route and get this data. Go ahead and load up your browser and at least try to get to the route that gets this data and see it working. I'm going to go to forward slashi forward slash and then it was posts. Now from here you can see if I bump this up way a lot you can see our JSON and again this works no matter what as any typical API endpoint you ever have. If you're used to working on the web sending git post whatever you can do all of that inside of one of these. You can even also have export const post post directly in the same file. So you can

have a post request go to the exact same route. So this is how you do endpoints. That feels too easy. Uh you're just writing an endpoint. You create a folder where that route is. There you go. You got your endpoint. These can be very handy for a number of reasons both in these type of sites. The syntax has endpoints for our API as well as for other things. uh sometimes you just need an endpoint to accept data maybe like a web hook or who knows what right so that is endpoints now let's get back to the regularly scheduled program here and let's work on this UI just a little bit because I mean let's face it right now there's a lot to be desired here and in fact let's go ahead I'm going to bump this font size down just a little bit we have our form I'm going to head to our general admin page our admin page has all of the posts but we're going to have a new link uh new href here. Just a link to forward slashadmin

Chapter 18 1:49:10

Add a "New Post" button

slashphost slash new and we're going to say new post. I think I can make this a class of button. I think that might do something. We'll see if it does or not. There we go. Still ugly, but it works. Now, you'll also notice that we have this. Again, remember that we could use the resolve function for this. Or again, you might be annoyed about this by now, but I do think Resolve, even though for the extra effort, I do think it is worthwhile to have. And once again, I have to figure out where this is actually imported from. Even though we just did that a moment ago, I cannot get my brain to process it. It's from app forward slash and I believe paths.

Perfect. Got it right. Okay. So, now we have the ability to create a new post directly from here. That's great. Um, I will spend some time, I promise, on this UI by the end of this course, but I don't want to spend any time on it while we're learning this felt kit stuff. Okay, so we have a new post the option to do that. Now, the next thing we'll want to do is be able to edit any of these posts. And you'll remember that we have no route for that. So, let's go ahead and create a post. And then inside of this post, make sure you're creating the folder at the right level. We're going to have another new folder. And this folder is actually going to be our first parameterized based route. We haven't done any of those yet, but to do so, you have flat brackets and then the name of the parameter. So in this case, if we're like using the ID as a parameter, we could have the ID like this. From here, we can create a new page.spelt.

Chapter 19 1:50:42

Editing posts (+ parameterized routes)

And then we have this file where we could have script um lang ts. Cool. Now, we'll want to have a remote function to pull the uh current post with this ID. So, let's go ahead and do that. We can get it from our remote file. So, just like we had our other things where we had create post and get all posts, I'm going to copy this get all posts and we're going to say get const get post by id. And this is going to accept a value in the first parameter here. And that first value is going to be an ID. But since it's only going to be one thing, we don't need to do a v.ject. We can just do a v.string and say, hey, the thing that it's coming in here, it's going to be a string. And then likewise, the async function here that we have, sorry, this is all goofed up. Likewise, the async function that we have here now has access to a single

parameter, which is going to be the ID. We're going to want to find first. And when we find first, we'll want to do and then we're going to say where and then the way this works is it's a callback here and where the uh current and we can't say post. I'm just going to say P.

Sorry for the short hand, folks. And where the P is EQ right here, we have a function that gives us these. And we can return straight up where the current p ID is equal to the ID that is coming in from our function. Sorry, I had an extra semicolon there. So the way this reads here, if if this is a little tight, my apologies. The way this reads is that we have a new query. It accepts a single parameter that's a string. That string is then available to us as what we're calling ID. we are then able to use that in this uh DB query where we're doing hey find me the first one where the current or where the the post itself id is equal to the ID that's coming in.

Okay, this should be ready to go which means we should be able to do get post by ID inside of here. Now if we want to do this anywhere we can say const post is equal to await get post by ID. Now get post by ID after we import this is going to be accepting a parameter. Right now we can have access to the page params via the page state inside of spelkit. The page state includes a bunch of cool stuff. So we can say page um and then let's import page. Now you will see that there is an import from state or stores. Uh this is a hold over from older versions of spelt kit. If you're using spelt 4 and lower, you're going to use store. felt five, you're going to use state. State is the way to do this going forward. And stores will give you a warning saying it's deprecated. So, it's not going to let you keep using it and tell you that it's fine to use it. It works, but it's still deprecated. So, we'll say page here and

we can get from page a whole lot of stuff. So at any given point on a spelkid site, you can have access to the page data, the error, the forms, the state, the status, the URL and we can say params. And notice how params is actually here and typed for us. Now it does say that this is possibly undefined. I have a hard time with that because we're on a parameterizebased route. On this route in specifically it should be here. Now that's fine. I get it. But the way we can tell uh Typescript to chill out, we got this is just by doing an exclamation point here and saying, "Hey, we know this is not going to be null." So that gives us access to our post which is completely type safe. So uh at this point we could say post.title. Okay, just to output to see this working. So now that we have this post page that exists on admin post and then the ID, we should be able to

add a link around these list items here where once again we'll want to use the resolve helper. And the resolve helper is going to be forward slashadminost and then you can add on the post ID to this. Um you could also obviously do it in string concatenation which or this is string concatenation but you could do it in the uh back tick style. Actually I think it prefers that. So we'll just do that. I've had it complained when I use string concatenation in this thing. So, let's just use string interpolation.

Instead, the back ticks, right, cool. So, let's go ahead and go to our blog post here. You can see hello from SK. Now, there's a chance yours might error out. It does say that post is possibly undefined. So, we'll want to do an if post in here. If post. Now, let's actually make the form for this so that we can update our post, right? Because we had our create new and our new had a form in it that looked just like this. Let's copy this post uh or this form. Let's paste it in here.

Instead of create post though, we're going to make a new method which is going to a new remote function which is going to be update post. I'm going to copy the create post and we're going to say update. You could always make an update or create an upsert, any of that stuff, the way you like to work, your choice.

Up uh date post like this. And [snorts] when you update a post, it kind of takes a little bit more, right? It takes in a title, it takes in a body, but we also typically need to take in what? Uh an ID typically, right? Because if you think about it, um we need to be able to identify which post we're updating. Now, this is a very simple blog thing, but the the slug is still going to be generated. Now, from there, we don't need to change too much else. We're going to check to make sure the user exists. If the user exists, we're then going to regenerate the slug because perhaps the title has changed. Now, this time we're not going to do insert, but we're going to do update. Now, obviously, the values is going to uh get angry here because an update isn't the same as an insert. So, what we need to do is change values to set. This is a drizzle thing. And then the wear will look like this. Um, for wear inside of

here, we can do EQ. And EQ actually needs to be imported this time. I do find it odd in Drizzle that uh sometimes you're using EQ from a uh from a function and sometimes you're using it from the import. I I I probably just haven't looked into why, but uh always found that to be a little odd.

So, we have the uh update and we're going to update it. We're going to set these values. We don't need to change the author ID really. We're just changing the body and the title. And then we're going to say where the post do ID is equal to the ID that we're bringing in here. Now, the ID is still undefined. We need to bring it in in our function here. So I'll go over this once more right as before we run it. But basically what we're saying here is we have an update form. The major changes are that we accept now the ID. We also have a title and we have a body. Um we're going to grab those. We're going to make sure the user's locked in. We're going to generate a new slug based on the new title. Then we're going to update it and then we're going to redirect back to the admin. Okay, perfect. So now that we have that, let's head back to the form and let's do uh

update. Let's bring in our update post. So update post. Now update post can then replace create post throughout this entire file like so. And nothing should change really because we still have these parameters. Now the only little thing that we need to think about is what about like the ID, right?

We want to pass the ID in here. There's a number of ways you could do that. Uh, a way that I do it typically would be to have an input and that input will be for the ID. The only difference is that input will be hidden. Um, you can see when this loads here, that isn't there.

But when we send the message to the server, this is part of the form and this will get sent with the form. That's how I personally do this type of thing. You can also have it not be hidden and have it be read only. um because you don't want uh people updating this, right? And if we wanted to have the initial value set up in here, you could do this a number of ways. Um I think the easiest way to do this would just be since we already have the post here, you can just say the post do ID and that's the initial value. Um you can see that's populated. And then likewise, we can do the same thing for post.title as well as the post text area. Now for text area. Um I think you can do it on value in here. Let's do body. Yeah, you can.

Text area is always a little bit weird for me for some reason, but here we go. So with this change, we should be able to have this just working, right? Uh all we did was we had an update post. You can see how effortless this all is. So if we want to say hello from update, we click post. Fingers crossed because who knows, right? And look at that. Hello from update now exists. I can refresh.

That's persistent. So it clearly saved to the database. I can go to it. It is now here. What's up? Remember that. [sighs and gasps] Perfect. Oh my gosh. Effortless. It looks ugly as hell right now, but I'm really impressed with this. Uh I think one of the reasons it looks ugly is because I didn't put a row in between these inputs because it was hidden. it would, let's face it, it would look ugly anyway. So, cool. So, we now have an update form. We now have a uh an a new form and we have our blog listing. What's left to do in terms of functionally on this thing would be to create an actual blog listing on the main page and then a detailed blog post.

Chapter 20 2:01:46

Adding post list & post detail pages (challenge)

But you can actually do that now that we know what we're doing here with this stuff. So, if you would like to go ahead and attempt to get the blog post and the listing done, go ahead and just knock that out. Pause this video and knock it out. Otherwise, we're going to do it together for some reps. Before we do that though, we're going to take a minute to talk about another additional couple of features in SpeltKit. One of which is going to be CSS. We talked about it a little bit within the adding graffiti to this. Now, that's just an import of a single CSS file. Part of Vit and uh Spelkit is that you can just add CSS as CSS files. So, there's a small change I want to make to the CSS, and it's something I would want to have globally throughout this entire site. I want labels to be display block. Um, that's right. In fact, let's even add a label for this ID, especially because I'm wrapping them in here. I want a label to exist on its own

Chapter 21 2:02:20

CSS

row. So, let's go ahead and let's head to the root of our route. So, just inside of routes here, I'm going to make a new file. And this is just going to be style.css. A lot of people put these in different places. If you have different CSS files, you can have them in a CSS folder, a styles folder, or whatever. I'm just going to add a simple line of CSS that is label display block. Okay, that's it.

This is going to be a global line of CSS that gets loaded onto everything. To make sure the CSS gets applied globally, I'm going to head to my layout.selt and then after graffiti, I'm going to import. And then you can import from where we're at style.css. This is a really common way to do things in here. And to confirm that this worked because obviously because these labels are wrapping around the content, I'm a I'm a dummy. uh they even though they're wrapping around the content, it's fine that they're display block, but you can see it is getting that CSS here. You can have multiple CSS files, you can connect them. Uh what you don't want to do is import from inside of a CSS file to another CSS file. Uh believe it or not, there's actually a native import for CSS and it can cause uh performance issues.

So don't do that unless you're using something like post CSS to actually do that at build time. And if you are, you probably know how to do that stuff anyways. to fix this label issue because I'm uh being salty that I I was thinking about the uh the display block was going to fix it. I'm going to add break tags here. Um just a page break after each of these just to show this label what's what to show it who's boss here. I am. Let's do that for this one too.

Cool. Okay. Uh, lastly inside of this before we get out of this, let's get rid of this if post title business. Let's just get rid of that. There we go. Perfect. Perfect. Great. Cool. All right. Now, let's talk about another thing. Um, I actually really love Sentry at century.io. And I, this is not an ad read. I'm not joking when I say I really love this because I use Sentry in all of my projects before I worked for Sentry. So, if you want to see how to add Sentry to your projects, I'll show you how to do it right now.

Chapter 22 2:04:53

Adding Sentry

And and what I don't want you to think is that like I'm just telling you to do this because I work at Sentry. I legitimately think this is a tool you should have on most sites that you work in. That's just me. So, I'm going to log into my Sentry here. Here is my Sentry dashboard. And uh for those of you who don't know, Sentry like catalogs and logs your errors. There's also like a cool MCP that allows it to uh give you the same functionality in your development flow and then you can actually have the AI look at that and stuff. Very neat. I'm going to go ahead and create a new project. And this new project, they give us an option for spelt inside of here and spelt kit. Uh one of the reasons I like to show this is because just how stinking easy it is.

So, I'm going to select Spellkit and I'm just going to say this is the local blog or it was called remote blog. That's the opposite of a local blog. And the team is going to be level up tutorials. I'm going to click create project. That's it. From here, it gives you this oneliner command. That's a wizard. Um, I love a good wizard. So, check this out.

I'm going to come into my terminal here and we're going to find an open window somewhere. I don't usually have my text this large, folks. So, it's usually easier to have a whole bunch of these. We're going to paste this in here. Now, what this is going to do is it's basically going to set you up with the Sentry wizard, it's also going to modify our hooks files.

So, there's going to be some stuff to learn from here. So, even if you're not using this wizard or Sentry in general, uh I'll want to show you some stuff that it creates from here. So, it says you're not inside of a Git repository. Yeah, whatever. Go yolo it. I've done this thing enough times to know that it's uh definitely reliable. Um, it's going to make you log into Sentry, which mine did. It's going to come back. It's installing Spel Kit. Do you want to enable tracing? Yes. Hell yes. Session replay.

Session replay logs. Uh, give me all the stuff. And it's even going to make a route to test Sentry. Um, it says, you want a project scoped MCP? I'm going to say yes. Why not? Um, and then we'll do that inside of uh I use cloud code. So there with that out of the way, we have our sentry here. We have our site. And let's actually take a look at what it created for us really quick. [snorts] It did a few things. It did create a Sentry example page route of which we can navigate to. To confirm that Sentry is working, we can throw an error. It says a sample error was sent to Sentry. We can say take me to issues. And then it looked like it actually collected an error from something else already. Um, if I refresh this, you can see there's the sample error. Now, we can use this to solve bugs on our site. We can also use it to debug slow queries. We can do all kinds of stuff with it. We even get like session replay if stuff is not

working correct. Um, so it's like this node.reove function. There's clearly something going on here that I wasn't paying attention to and Sentry caught it. We can even do this uh quick fix with seir to get an answer. Sorry, this is sounding a lot too much like an ad. So, I'm going to stop with the sentry stuff and let's actually take a look at the interesting parts of this code that it added. Uh it added an instrumentation.server.ts file. This instrumentation file is something that is new in Spellkit.

Chapter 23 2:08:25

instrumentation.server.ts file

allows us to work with um different types of observability platforms, one of which being Sentry. And this is a special file inside of Selkit that allows us to set up and initialize observability via instrumentation.server.ts. So that's great that we get to have this now. It just wrote it for us. It also wrote us a couple of hooks. Now we had our hooks inside of hooks.ts.

You will see it added a second sequence. That little cheeky sentry wizard did add a second sequence. You can see this works. You can have nested sequences all day long. But if you want to keep this clean, you can always remove that. The reason why they do that is they want the Sentry plugin to be first in the chain of things, and it just makes a lot of sense to have it wrap in a sequence, but this works too. Now, the next thing that you'll see inside of here beyond the session the uh handle here for that is that there's also a client side one. The client side one, this is where you set up things like session replay, uh, session samples, logging, trace samples, and stuff like that. So, we didn't talk about hooks.client.ts, but we did talk about hooks.server as our middleware. So, now's a good opportunity to talk about hooks.client because this is there's there's actually three hooks files. There's hooks.

Chapter 24 2:09:43

More on hooks

Hooks.client, and just hooks.js. I'm going to be real with you. I have never used hooks.js. JS not once. This file runs on both the client and server. What's it used for? I don't know. I'd be interested in knowing what people find this to be useful for. I've never used it. Hooks. Runs on every server request.

This is where you do middleware, authentication, authorization, that kind of stuff. Hooks.client.js is client side hooks. Uh, as you would expect, that runs on the client side, but what's available in these is a little bit different. And really the hooks that you have in here aren't just handles. There's a few different hooks.

One of those hooks is handle error. And that's a hook. This this export here is a special export from speltkit. So ones that are available on the server and the client are handle error um as well as innit which is a function that runs when the whole thing is starting up. Some people use that for a database connection. Um that's a really great place to do that. There's also one reroute that allows you to potentially do internationalization and routing.

There's one called transport. These things are definitely use cases where you might need them. But uh a lot of the times I'm personally on average sites not using them. But if you do need things like internationalization, that might be the way to go here. That said, I'm still looking for a good reason to use the hooks.js file. So let me know if you have one for that. So this is a client hooks. All of this is available in the spelt documentation. Um I will say this uh spelt kit is a deep well where if you need something, chances are it's there. Chances are it's in the documentation and they've already thought about it. So we're not going to be able to cover every last area, every single export or or hook or whatever, but it's good to know that that stuff is there because otherwise I would just be really rapid fire shouting things at you this whole time. there's just too much stuff to cover. So, thankfully for that, we now have a hooks.client, a hooks. And

an instrumentation file. The instrumentation file, like I mentioned, is something that is super new within Spellkit. Now, before we get going on this rest of this UI stuff, let's even take a minute to talk about some other APIs to the point where I'm even just going to throw up the documentation here since it's not something we're going to be using. Now, service workers are a great way to have offline access or intercept requests.

Chapter 25 2:12:09

Service Workers

I've done a lot of neat stuff with service workers. And to have a service worker in your speltkid site, you just have a service worker or serviceworker.js in the src directory of your application. They have a default service worker inside of here if you want offline access uh that you can just copy and paste. Know what you're doing with this stuff before you just do it. But hey, uh if you want a service worker and you know about service workers, this is a great way to do it. If you're experienced in this stuff, it even better right now. Another neat things about Spelkit is that they use some stuff with the anchor tag. Now, by default, Spelkit does preloading data.

Chapter 27 2:15:29

Advanced Routing

And you can even throw a rest into the middle of a routing structure. Read over these docs because it's kind of interesting. It's not something you use a whole lot, but it is actually we use on something we use on the syntax site to direct people to the right episode on very specific URLs. And you can use these for 404 pages as well as it shows here where they act as a catchall. So if you have here's your routing structure and maybe nested deeply you want to have a special 404 message for that specific thing, you can say catch all the routes because that catchall only applies after it hasn't found the route. looks for here, here, here, catch all. If it's there, then you could throw a 404 page and not have to throw a 404 and just have it collect that information. Uh, you can also have optional parameters.

You can do so with double brackets. These things are matched greedily and unless the optional parameter is there, it's not going to be used. Uh, you can also write a custom match function. If you wanted to have parameters be very specific, oh this is only a number, this is only a fruit, whatever, you can do that. And then inside of your routing, you would say page is equal to and then the route matcher that you have. In this case, it is a fruit. This is a feature I have used before to make sure that a show number exists in our route for the syntax site, but is not a commonly used feature. The sorting section here just shows you the order in which routes are applied. First, the explicit route, then a parameterized based route, then an actual parameter-based only, then an optional, and then a catch-all. So, that's the order in which they uh it looks for each of those. This graphic to me makes more sense going the other way, but you know, either way, this is the lowest priority. This is the highest

priority. And it actually looks like they have that here. Uh so, hey, cool. There's also some cool stuff around encoding. If you need to get into any of this stuff, you can. Now, the one feature in Spellkit that always something I end up needing but don't really love is route grouping. If you [snorts] want to create a group of routes, you use the PNS and a group name to wrap that stuff around. Now, this is great. I'm not going to apply this to our current site because it's going to goof up the entire routing structure that we've already set up. But let's say you have very different layouts, like a landing page, like a marketing page, and then a website page, and then maybe potentially even an admin page like we have. You might want those to have very different layouts. And that could get really hairy with if statements and whatever. Maybe you want custom layouts, and maybe you want a layout that wraps all of those. You can do that with route grouping, and that's a really handy feature, but definitely something you want to think about uh if you don't want to have to update all of your your uh

your URL paths and stuff down the line. Um, again, a route group doesn't affect the actual URL in the browser. So, uh, just by having the parenthesis, it's like a way of grouping things together, being able to wrap things in a layout without having to create an actual route. Not my favorite API in terms of the syntax used, but something I end up using all the time. Uh, definitely something that's useful here. So, a lot of neat stuff, and you can even break out and target a specific group, but this stuff is getting really hairy to me and it feels like a little iffy, but if you need it, it's there. Okay, so one last concept before we get into finalizing and just making the site beautiful. Deploying uh spelkit sites are great because they can be deployed anywhere. And I'm serious. You can deploy these things just about anywhere.

Chapter 28 2:19:22

Deploying

They're fantastic. You can deploy them as a static site. You can deploy them as a function to on AWS or on Cloudflare. Uh you can throw them on Koolifi as a node app or static site. Whatever you want, you got it. So that concept is actually done through something called adapters. Now adapters as the documentation shows there is a number of official ones. So if you want to host on cloudflare you install and use the adapter cloudflare netifi node static forcell etc. And all you need to do in these is you just have the adapter in your config. So let's say config spelt config.ts. I wanted to change this adapter. Right now, we're using the adapter auto. If I wanted to do the adapter node, I would first need to install it, but I just changed this word from auto to node. And that's it. From that point forward, that's it. I don't have to do anything else. And then when

I run npm run build, it's going to build me a node site that will run and operate as a node site. And Cloudflare or Versel or Netlefi, they all function the same way. and static just spits out a straightup static HTML site. Really stinking cool. The adapter pattern is incredible because it allows you to deploy easily anywhere without getting locked into anything and not being beholden to a platform for any specific reason, dumb or otherwise. So, I love this. It makes it really hyper portable.

You can move your Spelkit site just about anywhere as long as the runtime you're running on supports the features that you're using. So, we're not going to cover deploying a SpeltKid site because frankly all you do is commit it. You put it on GitHub. You go to the platform of your choice and you say select that repo go. It's easy. If anybody would like a dedicated deploy video, we can certainly do that as well for each individual platform. There is just there's just too many platforms and they're all basically the same but just a little bit different. So, uh, depending on where you want to deploy it, just make sure you grab that correct adapter and then use the standard git process to build and deploy. Cool. Let's go ahead and actually get this going in terms of a nicel looking site here because we just spat out a whole bunch of miscellaneous docs and miscellaneous stuff, but there's a number of issues with this application. Um, I can tell you a whole lot of them. One of which is

Chapter 30 2:24:44

Adding session info/logout

bring in our o client o client from our lib o client from inside of here we can use it kind of is kind of a little reacty but you'll say const session is equal to an oclient dotclient dot uh use session now I am hoping they update this sometime soon because this is not actually a spelt uh rune style state it's actually a writable so meaning that when we reference this down here. Let's say if we wanted to, you would have to do dollar sign session and from dollar sign session data do user.e.

Um, is that the right thing? Data.user.e. I think this might be potentially undefined. Session.data is possibly null. So that is a problem here because again this is client side information. So if we come and look at our HTML and we look at this and it's Scott.

It's not here. This is strictly client side and that's fine. But if you want server side stuff here, you're going to want to bring in the server side data loading for this and therefore you're probably not going to want to use this like this as a writable. So let's go ahead and do that. Let's go ahead and create a remote function and let's do things the spelt way. I'm going to do it inside of our routes. We're going to have a userreote.ts.

We're cruising on this stuff now. You've done it a whole bunch. We're going to have an export const And this is going to be get user is equal to a query. That query again can be imported directly from dollar signapps server. We don't take any parameters in here. So the first thing is just going to be an async function like this and we're going to be able to grab the user from the session. Now the way that we did that before was where you remember we did that in um one of our remotes in post. remote. We grab the current session like this. And this is going to be the exact same. So, we're going to just copy this and paste it in here. And this is getting our current session. We need to import the get request event. We need to import our O from lib off. And therefore, we now have the session. And we can now return from here the ID of

the user if we want just the ID of the the user or we can return the whole user object if we would like depends on what you need here. We could simply return from here. Let's do in fact an ID. We'll get that from dollar sign sess or dollar sign session question mark dot um and then let's do what the autocomplete's telling me to do. User ID or null. Okay.

And likewise, we'll do email for the same. And you could do um you could do username too if that's what you're interested in. Um email. Okay. So from here, this is the current user, right? This is great. Uh let's go ahead and what we want to do is use this on the front end of the site to then import this. We'll import get user from and we'll look for that remote user.ts.

Now from here we got our get user. We can constate this. You can await and get your user that way and you could just have it be your user. But there is something interesting here that we haven't covered and this is a little bit of nuance. Okay. So if you have data that is changing and like over here I'm telling this data to refresh and rerun this function. If you do const user is equal to await get user will this rerun?

The answer is no. The answer is no. It won't rerun because this is running once. It's running once and it's sending the user. So at any given point if this changes and we tell that remote function to refresh, it's not going to rerun. So to rerun it, we need to do derived like this.

So we're deriving the data that comes from await user. And now this will be reactive when the user logs out. So that's important. So inside of this layout readable, let's go ahead and add another div down here. And we're working towards a logout button here. And we're going to do uh actually let's add another class to this. And this is going to be split.

Split just kind of adds a justified content um space apart to these. And we're going to add a if statement in here. If the user exists, so if the user is not null, then or we're going to want to have a log out button. Now, log out buttons can work all kinds of different ways, but we're just going to say log out and we're going to have a function that is on click. We're going to log out. Okay, on click, we're going to log out. And log out is going to be a function log out.

And this is going to be some better off stuff. Okay, we want better off to h to to hang with us here. And we're going to be able to use our O client, which we need to have even though I got rid of it. And we want to use sign out. Okay, sign out does it has some things in here. One of which is fetch options.

I don't know why this isn't something called fetch options, but it is. So fetch options and then on success is going to be the parameter or the property here eventually. And this can give us an async uh callback function that happens whenever success whenever we have success with our logout. What do we want to do? Well, what do we want to do? I think what we want to do is we want to refresh the get user. So we want to say await get user and then refresh like hey go go go actually regrab the user data because it might not be available then or not get did I spell this get did camel case get user and then we'll want to then go to and we'll want to resolve and then we'll say hey go to forward slash offward slashsign is it login we'll do forward slashlo so if there's signing out. We do need to import We do need to import go to. So

what happens here is they click the log out button which you can see exists proudly up there and you can say hey log you out if it succeeded which for the sake of this demo it will again this is not very defensive programming. We're going to say on success hey go regrab the user and then go to the login page.

So fingers crossed that this should send us to the login page. And it did, which is a perfect parlay into writing our login page. Now to confirm that we are actually logged out, you can come in here and you can check your application.

We can check our cookies. And I do have some stuff in here. Um, but one of which is not going to be the better off key. The access token is for something else. A whole bunch of stuff gets put on localhost 5173. Cool. Okay. Uh now let's work on our login. I'm going to actually head to our signup page first and I'm going to copy the entire signup page. I'm going to paste it into the login page. It's really similar code. We just need to then delete the confirm password because when you are logging in, you don't typically have that. And then get rid of the username as well. And then we'll change the parts that say sign up to say login.

Chapter 31 2:32:57

Login page (+ Remote Function .refresh())

Um, and let's do a find and replace for that because who knows?

And we'll just replace these. Um, I know there's some people who are screaming at me about how I'm typing all this, but hey, it works. So, sign up. We can do rename definition. We can rename symbol on this and say login. That's going to change the function of the name to be login. And now the major differences are that we no longer have password confirm.

We no longer have username. Therefore, we don't need to check this. We don't need to check if username exists or if password confirm exists. And we'll want to remove this. The method is not going to be sign up, believe it or not, but the method will be sign in.

Interesting. Sign in, s log in, log out. Um, always interesting language. And likewise here, we have the resolve. Now, the only difference here is that when we log in now, something that we didn't do before that we'll want to do this time is we'll want to actually run our git user and we'll want to run a refresh on this.

This will cause the the git user that's being run sitewide to refresh. [snorts] Now, I need this on the other one as well. We want to make sure that your git user actually imported as well. Mine automatically did, but we need get user to import from user remote. And then we'll want to change that on the signup as well. So after success, let's refresh the get user. Make sure this is in here.

Okay. So just like that, we have a login form and a signup form. Now, if this was a longtime project, I might abstract this into another form component and then uh be able to toggle between them. In fact, even on this signup page, you probably want to do something like already have an account.

Uh anchor tag refol. Now, we got to do resolve. Uh, o login spell team, if you're listening, I want resolve to be automatic. We're a compiler, right? I have no idea if that's possible. I'm going to be serious. Um, okay. Here we go.

That said, I still want it. I want it to be automatic. All right, let's add the same thing to the login page. Um, that way we can already have an account. Don't have an account. Sign up. Sign up. Beauty. Okay. It's the type of thing I would like to make a component library for. Honestly, if this is type of me, I'm going to build a form library. I'm going to build a component library for stuff like this. In fact, I already am working on something like that. So, uh, stay tuned there. Okay, cool. So, we have this going. We have logged out. Our login form is now working. It's It's pretty It's pretty ugly still. And as you can see, I'm now here. That's amazing. I can actually confirm I'm here by visiting the blog admin. I can click new post. Hello.

What's up? Um, hello admin. Post it. I'm very clearly an admin and this I'm very clearly logged in at least and this is working. Which does kind of lead me to something else. Everything leads in to the next thing. User roles. Uh user roles are really great to have and uh most systems that have users need user roles or permissions in some sort of way because frankly if you have a user account you don't want them to just be able to create blog post and the admin or you might not want them to have access to the admin in general. Now this is a better off type of thing rather than a selkit type of thing and it largely uses things that we've already done. So that is why I'm saving it for the end here. Now I'm zooming out of the interface because the large interface is kind of driving me nuts. I do want to add another something to my CSS. Always do some incremental fixes here. We're going to say on uh button on actual buttons and button class items. We're

Chapter 32 2:37:07

User roles (+ styling improvements)

going to say display inline block. Um because anchor tags by default, not inside block inline block. There we go. That solves some of this weirdness right up top here. Another cool thing that we could do and by all means it's not cool at all but the main tag here we can say main not main uh the main tag here can just get some uh vertical padding here.

So we could say padding and this is padding block. I want to add this to the top and bottom. So, padding block and this can be vari for vertical space medium. Again, this is my own CSS system. You don't have to use it if you want, but hey, vertical space medium, that works.

Um, another thing I want to do is align items center. And I think I actually need this on the layout readable because we did tell this to center horizontally but not vertically. So, let's do that. Align items center. And that should give us a little bit of a fix there where the word blog now looks quite a bit nicer.

Perfect. Um, this is great. The header itself could also get some padding on the block dimension. We can do VS and we'll do let's just do small. VS small. There we go. Just to give it a little bit of padding here and there. Perfect. Perfect. Perfect. All right. Let's go ahead and now let's knock out some other features here. Okay, so now let's get into the point where we talk about roles. The user access the admin role can then get the ability to only get to the admin and no one else based on its role. Now when we got our off set up using oclient and o.ts, we did add in the admin client plugin as well as the admin plugin. That admin plugin gave us the ability to do roles automatically.

And this type of plugin is really handy because it allows us to do filtering. It allows us to uh set up different types of roles. It allows us to check roles. But by default um out of the box, you do need to do some stuff to get using it. So, but it all integrates very nicely. So, what types of things can we do? And to even illustrate this, I'll pull up the better off documentation here. Now, the better off admin documentation. And okay, this is showing how to get it going. But if we create a user, by default, yes, they have a role here.

You'll want to set their role. Let's see what our users have a role of. And I'm going to get my studio running because it looks like that stopped at some point. We're going to get my studio going. And we can see that the user I have has a role of user. So there is a default role. Without doing anything, the default role is user. Okay. And that's great. It's great to have that. But what about setting and changing that role?

Now, obviously, what we could do is we could come in here. You could come into here and you could modify this. If we click on this, you can just straight up change it to whatever you want. But there's also ways that uh you'll want to have this set up depending on how many different roles you actually want. So, to set a user role, you have to run oclient.admin set ro. The role of admin is required for you to be able to set roles for other people. So you might be thinking, well, I'm in a situation now where to be able to set roles for other people, I need to be an admin. How do I be an admin? Well, there's ways that you can do it through better off um instead of.

But I think the easiest way, the easiest way is to come into your Drizzle Studio. It's to come enroll, type admin, click save changes. You're an admin. Congratulations. Oh, is your email verified as well? Oh, yeah. Your email is also verified. Oh, great. Because you can I mean, if you if you're the admin, you can just do this stuff. Admins can just do this stuff. But once you're set up with an admin account, it makes it a lot easier than having to go through the process of getting an admin account set up without having that. But as an admin, you can do all kinds of stuff. You can set another user's password. You can update another user. You can ban a user.

You can unban a user, list all user sessions. You can do all of this via the functionality in the O client. Super easy, super useful, and something I would highly recommend using. Now, one thing I love about the better off documentation here is that they give you the access to do it on the client side, which we saw only client side, no server side rendering there, or the server side if you want to do this on the server.

Um, which by all means, uh, is a great way to do either. You also get access to impersonating users. So pretending to be other users and seeing the site as them. Very common functionality in most sites here. Now the access control stuff is the stuff that I am most interested in. So by default admins have full control.

Uh users have control over themselves but nobody else which is reasonable right? You can also add custom permissions. You can add as many as you want. But what if I just want to see is this user an admin? Right? What do I want to do? I have a c I have a user and I have an admin. I don't want to get into any of the crazy stuff. How do I just check to see if this user is an admin? Well, you can run this o client has permission or the server here. Now, this is really easy to say like, hey, um, now you might see some of this stuff like access control usage has permission and think that's it. But this is more for checking if a specific user has the permissions to do something. Uh, keep in mind all of this stuff just exists on your user object. So you could just say in your database is the role of admin there. You can also actually pass in uh hard-coded admin ids into the code itself if you don't want to have to deal with roles to say who's an admin. Uh that's really nice to have. But for the

most part, if you want better off to really control that entire flow of checking access outside of this stuff, you will want to get into custom access roles here. So whether or not they can create, share, update, delete, whatever on individual stuff. I'm going to save you for that because what we want to do here is that we have access to this system of where we can um as an admin run these O client bands and whatever.

But generally, if you're just checking if somebody has access for something, you're generally just checking to see if that role exists. So likewise in our routes, we can head to our admin and inside of our admin, we check this out. We have a layout file inside of our admin, but nothing exists. Um, this layout file can be kind of what you'd expect. We can do an export async function. And this is going to be our load function. Our load function is like a page loader. We've talked quite a bit about these. We also have access to locals. If you remember inside of locals is the entire user object and this is a simple check. This is happening server side. So you can say if locals user uh roll is not equal to admin believe it or not then get their ass out of here. Let's throw a

redirect to the 302. not to the 302 as a 302 to forward slash and then we can otherwise just return like this. Let's go ahead and make sure our imports are here and then let's fix this TypeScript error because it's saying that it's possibly undefined. I think it's just user that's possibly undefined. There we go. Cool. So, if the user is their role is equal to admin, bingo bango. If you need more advanced permissions than this, check out the Better Off documentation on assigning individual permissions and RO access.

But a lot of the time you're just checking an individual role depending on what you need. So with this here now, uh let's go ahead and see this in action. I have my site here. I'm going to go to the admin page. I'm an admin. This works just fine. Let's go ahead and change my role from admin to user. User. Save changes.

Refresh. Kick me out. I'm not an admin anymore. So, you can see it's working just fine. And there's nothing crazy here. This is all just uh simple JavaScript checks. The better off stuff comes more in handy when you're dealing with uh larger systems of checking individual users, uh checking individual permissions and stuff like that. But for simply creating roles, managing and accessing roles, uh swapping roll out until you have like a ton of different access levels and roles, a simple check will work.

Now, we can also go ahead and do that inside of our uh post forms. You remember inside of post remote, since this stuff all lives here, we were just checking to see if the user ID existed. But now we can say if the user is, and this needs to be session by all means, not locals. If the session, which is possibly null, is not equal to admin, then they are not authorized to make a blog post. Okay. And likewise, if the session is not um right up top here for this one there, since I am an admin, I should get proper access to this. Let's make a new post. Hello.

New post. I make every post name hello. What's up with that? what's up with that post. Cool. And look, that all works. Cool. Now, let's go ahead and add some other nicities here. So, we now have some authentication stuff. We can create a new post. Let's go ahead and do our blog post listing really quick. And then we can add some fancy animations. So, this should be fast. And again, I already challenged you to do this a while ago. Maybe you already have this done. Um, that would be great. I'm going to create a new route which is just post and then forward slash the slug like this. And we're going to have a new file inside of here page.spelt.

Chapter 33 2:47:47

Adding post list and post detail pages

And I am going to have a script and lang equals ts. Okay. Now for the main page, the general main page of our application which is just page.spelt living in the root of our route. Right now it just says welcome to speltkit. Let's go ahead and change that to give this a script with a language of ts as well. Uh again once again I should be making a snippet for this. Now we have get all posts right. So we can import get all posts from our admin uh remote.

Doesn't matter that it lives in admin. Doesn't matter where it lives. It's just a function. Now from here we can do that await trick where we would say each await get all posts as post each like this. And then once again we can have these be an individual post.

Now I'm going to put these into a box one. I don't know why box one. Box one doesn't even look good. I got to change box one. Uh we're going to have an H2 for these. And the H2 is going to be the post.title. And we can actually make this entire box in an anchor, believe it or not. You can wrap things in anchors. Now,

and this is going to be once again a resolve. And resolve is going to take in back ticks because we want to do string uh interpolation here. And we're going to say post forward slash and then dollar sign brackets post ID post ID. I'm going to save this because it's clearly mad about something. It's mad that I'm not using a key. So let's do a PNS postid as a key. Bingo. We got a key. Uh we don't have resolve imported. Autoimp import killing me. Um, it's funny when we rely on these tools like autoimp import when you don't have them just drives you nuts. Cool. How fast did we build that? Uh, I also want to add in a Let's do a section tag with a stack class. A stack class just adds a display flex with a gap in between them.

Really nice and simple. I've been using this uh technique for all kinds of vertical layouts lately where I make sure that nothing as a child has any margin or padd or margin on it and then I do a vertical stack and I control the width or the uh space between everything with gap. It rips and like I said the box one looks ugly. I got to change box one. But look at this. Cool. Now we do need an additional remote function. So let's head back to our post. We had get post by ID. We had get all posts. We'll want get post by slug. And this is really the exact same. The only difference here is that the thing coming in here is now going to be the slug. And we're going to say, hey, check to see if the p slug or the post slug is equal to the slug that's coming in. Couldn't be any easier, folks. And we could call this function directly from our new file and just load that bad boy up. So, let's

do it. Let's head to that page.spelt that is in the slug folder. And again, apologies, we're rapidfiring some of this, but that's okay. We're we're experienced now. The post is equal to await get post by slug. Let's import this bad boy. And we need the slug. The slug comes in from where? Page what? Params. And let's bring in the page from app. What? Stater or storage?

You tell me. It's from app state. So page.par params from app state and we're going to have access to the slug and we're going to give this an exclamation point to promise TypeScript that it actually exists. From there, I think this will complain that post is possibly undefined. So, let's do an if post.

If post, I don't agree that it's possibly undefined, but in reality, it actually is. So, the H1 here is going to be the post title. Um, although let's put this in a section because this is our blog post. We have an H1. It's going to be the post.title. And then likewise, let's just have a paragraph tag. This is the most lowfi blog of all time. It's a single paragraph tag.

Cool. Perfect. Ideally, in a real world situation, you do markdown. You'd be having this resolve as markdown into HTML. But for all all intents and purposes is fine for right now. Um, let's go ahead and click our blog post. Um, I am uh getting Oh, I had an error. At some point, resolve was not defined, so therefore it got mad at me about that. Let's see if that is still the case. Um, I think that might be still the case.

Uh, looks like, oh, I'm sending it to the post ID. So, that's a problem. I'm sending it to the post ID. Um, it is a good thing. I just laughed about this not being undefined. I ah the the link that we're going to is the ID not the slug and therefore this post is not defined because there's no slug with the name of the ID. My apologies here. The resolve should be post slashpost slug. If you did this correctly and I didn't uh shout out to you that's great.

Okay so we have our posts and you can see the detail page is working. Now, let's take a look at potentially adding some animation in here. Believe it or not, we have something called the view transitions API that allows us to do view transitions in the browser. This thing rules for a number of reasons, and view transitions are something that you should spend your time on. There is a really nice 8020 way of getting going with view transitions in a way that like is really effortless within SpeltKit.

Chapter 34 2:53:45

Adding View Transitions

I'd be remiss if I didn't show you that because I think it's a great technique. So let's head to our main layout, the main layout of the site. From here, we're going to be able to use a spelt on navigate function. So on navigate and on navigate gets called when you would expect when you're navigating, right? So this gives us a navigate and a callback function here. So when this uh function happens, we're going to say if dollar if if the exclamation point document dot view start view transition doesn't exist, we're just going to exit out.

We're going to short circuit it. We're going to return. So like, hey, if view transitions doesn't exist, don't even bother. But if it does, we're going to return a new promise. And we're going to have the resolve for that promise like so. And we're going to say a document start view transition. And we're going to say this is async arrow function in here. Functions on functions on functions. And we're going to resolve inside of here. And then we're going to await the navigate.complete.

I have to spell things correctly, which apparently has been a major challenge for me. Navigate. Cool. So, what is this doing? This is uh Oh my god, I didn't even spell complete right. There we go. Uh what this is doing is whenever we navigate, it's going to return a new promise. That promise is going to start the view transition. After that page has completed, we're going to uh say that the navigation has completed. So by wrapping any function in JavaScript in a start view transition, you can transition items to be their open and close state. You could this works with all kinds of stuff. Something entering or exiting the DOM, something changing the side, just start view transition, wrap it in that function, change the values, and you can see it animate. It's pretty neat. By default, out of the box, we do get a default fade between pages.

Look at that. Nice little default fade. That's really neat. But what if we wanted the title from here to zoom all the way up here? That'd be kind of cool. And it's actually really pretty easy. What we need is we need to give our uh we need to give a unique identifier for the listing page as well as the H1 on the detail page. And from there, CSS just got your back. CSS and JavaScript, the browser's got your back. So, let's do our page.sfelt. And this is going to be the blog listing that's on the main page. And we're going to find the thing we want to transition. In this particular case, we're going to say style. And the thing that we're we're transitioning is going to be the view transition name.

Okay. The view transition name is a property that allows us to assign this a name. Now, it needs to be a unique name. And this unique name, it's getting mad this is not a string. This unique name, let's do a string concatenation here, is going to be heading. And then we're going to say what it is. We're going to say post and then we can say let's do the slug. Slug makes great sense for this. Okay. And likewise, let's copy this entire line right here. We're going to add this exact same line into our detail page right here on the same element. And what we're essentially doing is saying on the page you're transitioning to, this item which has this unique identifier is the same thing as this uh this item right here on the listing page. And so therefore, when we come here, let's give this a refresh.

Hopefully this works. Yeah, look at that in action. and you get that nice, beautiful transition happening free. That's awesome. Now, I highly recommend you taking the time to learn about view transitions. There's so many neat things you can do with them, but out of the box, like just having access to something like this is really pretty slick. Uh, and it adds a lot of nice connective motion to your site. Now, with anything, don't overuse it, but it's really pretty cool. And with that, we've pretty much covered all of the spelt APIs that we need to cover in this course. Now, that doesn't mean we covered absolutely everything. And like I mentioned before, the docs are very comprehensive. There are some things that might look interesting to you that we didn't talk about, like form actions.

Chapter 35 2:58:34

Thanks!

Now, form actions are going to be kind of largely gone now that we have remote functions because in just about every way possible, remote functions in my mind are way better. So, I didn't cover these. Uh, you can feel free to read about them. It's a way that we did data mutations before the advent of remote functions. Other things that we didn't talk about are things like page options where you can specify pre-rendering for certain routes or SSR or CSR uh at various levels of your application. Uh there's a lot here. Basically, like I mentioned, if you need to do it, it's in it's in the docs. As far as state management goes, there is some notes about uh if you don't want to have state spread across the server and then to the client boundary. So oftent times uh it's recommended to use context to share state. The state management page is worth reading about. Now granted most of the state management topics in speltkit

are spelt topics really but there is some special things about using uh data in SSR and when that's a problem when you could potentially leak sensitive information via SSR by accident. Um so go ahead and read that. There's a note on using context there as well. So, nothing really crazy that we didn't get to in this course. Let me know if there's any individual topics you would like to dive in further. And again, this stuff is still experimental. So, by checking that I'm using this experimental thing you're opting into, there potentially might be some issues until it gets out of experimental. So hopefully they remove the experimental tag soon and it's just fully released possibly with a spelt kit 3. And when that happens, I will update this course to use all of that. So as always, this is Scott with syntax. Drop below what you'd like to see out of me here with spelt tutorial content. I'm going to be

doing a local first style tutorial. It's not actually local first, but local cached data via something like zero. It's really excellent. Zero also an alpha. this stuff in experimental. That's too much experimental stuff to throw at you right now. So, we're going to wait until some of those uh dependencies are a little bit more firm before getting into that territory. But, let me know what you want to see out of these courses. As always, this is Scott and I'll see you in the next

Quote it with credit and a link. Quoting Syntax