every webd is probably capable of adding a basic login or signup form to their web application but how would you go about implementing things like forgot password or allowing a user to sign up with a social provider or their cell phone number or adding two factor or with an authenticator app what about basic user management features like being able to block or suspend certain users there are so many things surrounding user or and user management that can be difficult and timeconsuming to implement in a web app but as a developer you don't need to worry about implementing any of these things if you just use an or service like kind the sponsor of this video so I'm going to show you how to set up kind to add user or to your nextjs applic so you can protect your server actions pages and your API routes and we're going to add user or to this basic GPT chat app that I built in a separate video so I could ask it to add user or to a next app using kind and you don't need to have watch the previous videos where I set this up because we're just going to focus on adding kind to this app right now so at the end of this video kind will be implemented so if we go to the app we won't see any of the previous chats cuz I'm not logged in and then as soon as I go to login either using an email or a social provider I'll be able to get a custom experience and actually see my own chats here that I've made so we're going to start by going to kind. com and you can click Start for free if you don't have an account already but I'm just going to log into my existing account here and for every unique application you make you'll just add a new business for that application so I'm calling calling this thing code GPT so that's good and I'm going to set up my region to be USA because that is the closest to where I am and then I'm going to select that business then you can start from scratch but I'm adding this to an existing code base so click next select the framework you're using we're using next and then we can select how we want the users to log in and you can always change this later I think I'm going to have email and GitHub as options for mine and then click next and then here I'm going to click connect conect to get to the quick start guide where I can copy in all the environment variables I need so I'm just going to come in here select that go over to my environment variables and paste all of those in so this is the ID the secret the URL of my actual kind app and the URL of my next app which is currently just Local Host 3000 uh this redirect URL I'm actually going to redirect straight to the homepage if a user signs out but I could have them go to a different page using that environment variable then I also need to install the nextjs library so bun add that and then it has some quick start instructions here but I'm actually going to go over to the nextjs documentation because I find that to be a little bit more thorough and contain pretty much everything you'll need to get this set up in your application so I'm in SDK version 2 because I'm using nextjs 14 and the app router and then we're just going to follow some of the instructions here here so if we scroll down one of the first things we need to do is set up the kind or router handlers so I'm going to go into my app into the app directory and create a new path here which will be API or and then kind or in square brackets so anytime that kind needs to deal with an HTTP request it's going to use this route uh and then we just need a route. TS with this code in it so I'll create a file route.
TS and just dump that in there and that's all we need for the setup of the API routes so then I'm going to scroll down and I think this is all the code I need right now so immediately I'm going to add a sign in and sign up link to my application if I look at the app over here I have this sidebar that shows me all the chats I have and then I have this little chat bar at the bottom I'm just going to put my user profile stuff in the bottom left corner over here so if I go to this chat list component this is a server component then I am going to import those links Let's see we got register and login and I'm going to throw these into the bottom of that left bar over here so I have all these links here I'm just going to make another div and if we justify between and set the height full then I should be able to flex over here too should be able to throw these links in here and these should always appear in the bottom left of the bar there we go we got sign in and sign up so right now immediately I should just be able to go in and sign up as a user and I'm allowing users to sign up with their email address or the GitHub account I'm going to go with the normal login here with my email address and this immediately sends me an email for a verification code so I'm going to log into my email provider in a different window and this is the code that was emailed to me I can paste that in and that will log me in and a really cool thing is that by default it doesn't deal with passwords so anytime we go to login it will email us a code and there's a post they have about this where they say why they default to not using passwords and if you really want passwords you can enable that feature but by default it just deals with these codes and you can read more on their website about why that's a better solution but I'm going to go back over to my application now because I should be signed in so I'm going to grab that other bit of code and we going to see if we can get the currently logged in users information so this is actually code that will go into my component I'm going to put that at the top of my component right now and then I need to import this so we'll grab this line of code and I'm going to put this underneath the links so basically when I try loading This Server component we're just going to try and get the user object so if they're logged in It's a Kind user otherwise it's null let's log this out and see what we get so we'll refresh the page and then over in turn terminal I should be able to see there are the user details so I have my name that I input my email address a unique ID for this user uh and I can use all of these bits of information anywhere in my application but I'm going to go back and I'm just going to make sure these links don't appear if I'm not logged in so if there is no user then we will display these links and actually we should add a log out link here log out link if there's a user let's change this up a bit if there's a user will display the logout link log out flip that and otherwise we'll allow the user to sign in or sign up all right so because I'm logged in I see this logout and actually let's display a little bit more information let's see here are all the options we have email family name given name ID let's go given name and if I'd logged in with my GitHub account I'd also be able to see like my GitHub profile picture let's throw this in the same div so if I'm logged in I get a different experience there I get my name and a logout button then if I click log out I have the options to sign in or sign up and if I actually go over to the kind dashboard here and go back to the home of this application that I built I'll be able to see I have one user in here and if I go into the user section I can see the user details and I have options to actually manage this user so I could suspend this user delete the user actually I could just view this profile F I could change details if I needed to so this is a really nice dashboard to be able to manage the users from but let's go back to the app and do some more with kind so in this application when I enter a message in here and submit that it will create a new chat for that user so I have this create chat function as a server action here and I am going to bring in the same function I used over here so get kind service session paste that in and again I'm going to get the currently logged in user so if I try and create a new chat if I'm not logged in I immediately want to probably throw an error or at least return down an error in a server action so I'll say return error not logged in so I can protect this action and only allow a logged in user to create a new chat and then when I go to create a new chat I want to associate that with a user and I'm using a SQL database here so if I go to my chat schema I need to add another column that is the user ID so this is going to be a text column because if we look at that user ID it's a string so I'll add that in here as user ID we'll make sure it's not null you can't create this unless you're logged in and then another thing I want to do because I'll probably be selecting chats based on the user ID is that I would want to add this as an index and because I'm using drizzle the Syntax for that looks like this where I'm going to create create that index for the user ID so now I can close that and because that is required my app is yelling at me hey you haven't provided a user ID that's fine let's provide a user ID that is going to be user. ID coming from kind right there so that's all I need to do to be able to associate something in my database with the currently logged in user so this is the action that will create the chat and Associate the user with that chat then when the user submits a new message into the chat that is going through an API route over here so the user will submit a new message and provide the chat ID that the message belongs to so I need to make sure that the chat actually belongs to the current user check the chat belongs to the currently logged in user because if you're not part of that chat then you shouldn't be able to create a message for it so I'm going to import the same thing up here and again get the user now if the user isn't logged in I don't want them to be able to do anything so I'm going to immediately respond with not logged in status of 400 or maybe a 401 then if they are logged in we want to make sure that there is a chat ID and then here I'm already selecting the chat from the database to make sure that it exists let's also make sure that the chat belongs to the current user so the where Clause is going to check for the chat ID but also it's going to check that the user ID is equal to the currently logged in users ID so the new select statement looks like that and this will verify that the chat exists and belongs to the currently logged in user and that should be enough to allow the currently logged in user to create new chats and messages I just need to update my database schema and now let's try creating a new message so write me a TS function let's see what happens so it creates a new chat it creates new messages for that chat and if I check in the database I had no chats and no messages but if I refresh I should now have a new chat and the user ID is that kind user ID for the currently logged in user so let's go back to the app I can see here's the chat I can load that in but if I open the app in an incognito window anyone is able to see the chats they are basically Global to all users so the next thing I want to do is only show the currently logged in users chats so if I go back to this chat list component I'm selecting the chats here really I just need to change this query so that it accepts a user ID and then we're going to only select from the chat table where the chat's user ID is equal to the currently logged in user ID I just need to import that EQ from drizzle and then I can pass in that user.