Hey there, my web crafting wizards. Today I want to give you a guide on how to set up OAuth with Wix. This is great if you want to create an e-commerce store or if you want to work with Wix's services. They have a lot of them, like bookings and stuff like that. And this will give you the ability to just sign in the user, uh let Wix handle that, and then you can keep track of all the users through the nice UI that they have. And then after that, again, if they make any sales or any stuff like that, they'll all get synced up with Wix. So, let me give you a guide. We're going to be using Next.js uh because I couldn't really find any good examples out there. So, I thought I'd make this cool guide here. What is that sound? It is actually a big storm out there. But, why do we care? We're developers. We never go outside. Let's get going. All righty, so let's get going. Now, I have an empty uh Next.js project here. You can try Remix as well or whichever flavor of frameworks you want to. Uh but, Next.js is really popular, so I thought I'd do a guide on this one. So, what I have here uh is If you don't have not worked with Next.js before, don't worry. It's not too hard to set up. Um if you search Next.js, you can just run this command in your terminal here. It's npm create-next-app@latest. All right, so you want to get that, copy that. And once you copy that and paste it in the terminal here, which you can do with control tilde in VS Code for example, paste it in here, it'll install everything here that you need. Okay, so if you head over to your app here and the page.tsx, as you can see, I only have a H1 here that just says Wix OAuth. So, if we view that in the browser, there we go. And I also set up a little nav here that just has a text here, nothing else. So, that comes Where is that? Cuz we're it's not rendered here. That's rendered in the layout. So, if you go to the layout.tsx here, you can see we have a nav here. The children here basically represents um all the different pages that could be swapped out. Okay? Now, the nav, if we go over is just a with again a H1 here that says headless store. Probably shouldn't have two H1s, so let's update the uh let's go back here and update this to a H2. All right, you should only have one H1 on your page. Um okay, cool. So, how do we get started? Well, first we need to install some NPM packages. So, you can do that if you close up the terminal here and do NPM I, and I already installed them, so I'll just show you which ones you need. Um These are the ones. Uh I highly recommend you to install this because what we're going to do in the next video is we're going to build out a full e-commerce store using this authentication that we're using here as well. Okay? So, it's Wix stores, Wix SDK, members, ecom, and that's pretty much it. All right? I also added um shadcn here for styling, but that's not necessary. That really isn't. So, again, you can do Wix SDK, Wix members, and the rest of them. So, make sure you got that installed, and after you do, you can run back NPM run dev. Oh, one more actually. Uh install JS cookie as well. So, JS cookie here and the types for the JS cookie. We need this to access uh our cookies on client components in Next.js because otherwise you either need to do it through a server component or pass that down to a client component. Um but this just makes it work a bit easier. Okay. So, let's get going. What do we need? Well, let's see. Where are we going to start off? Well, the first thing we need to do is create a client. And that client um is going to be our Wix client. So, through that you can do a lot of things, pretty much anything uh that Wix allows you to do. So, whether that's, you know, creating posts, deleting posts, viewing posts, um creating products, you know, checking out with a cart, accessing payment methods, accessing members, and stuff like that. So, the place I like to put these little utility functions is in the lib file here. So, I'm going to make a new one and let's call this Wix client.tsx. Now, I'm going to name this TSX because what we want to do essentially is use React's context to basically inject it inside client components. Okay, so this is going to be a client Wix client, I'm sorry to say, but we're also going to have a Wix client for the server, all right, that can only run on the server. So, this way you can access all the Wix services through a client component and a server component as well. So, this one again is specifically to access it through client components. Okay, so let's head here to the top of the file. We're going to do use client here at the top. And then what we're going to do is say const Wix client and set this equal to create client. Now, this is going to come from the Wix SDK, so that's going to automatically import that. Here we go. And this is going to take in a bunch of different options here. So, one is the modules. Again, this is kind of like what kind of services you want Wix to be able to access. So, you can do like products here if you want. You can do collections for your CMS data. You can have access to the current cart, for example, redirects and members as well. We're going to need members in this case for the login. Uh these are optional again. We're going to expand this in the next video. So, let's just get these for now and you can import these through all the different SDKs that we installed through NPM. So, for example, members, that comes from Wix members. Uh we can get redirects as well. Uh that comes from Let's just import that here manually from do Wix. This is coming from redirects. See, I don't think we installed this, but that's fine. We won't even need this redirect for now. Let's import uh the current cart as well. So, that comes from the Wix ecom. And let's also do collections. Okay, cool. Let's get rid of uh end products as well. There we go. So, now through the Wix client, we have access to all of these different modules. Now, before we can access this, we need a way to authenticate. And the way we can do that is by passing down an off method here. And we're going to use the OAuth strategy here from the Wix SDK. All right, let's pop this open. So, this is going to need to take in a client ID. Now, where can you get your client ID from? If you head over let's head over to Wix Studio. There we go. So, on your website here, you can create a new website if you don't have one. So, let me just go here to manage. So, if you don't have one, just do create a new site and that's it. All right, just create the site even though we're not building it through the site, it needs to have a site to kind of redirect for the login and uh the sign up pages and stuff like that or the checkout for the cart. All right, it's going to have its own custom page that Wix makes for you. Uh so, for now again, you can create a new site here or you can go to an existing one, which I already have here. So, I'll just go to this one. And you're going to see that I have you have a bunch of settings here, right? So, you have your CMS if you want to create CMS stuff, you have your sales, and you also have your customers that you can access here. So, anyone that signs signs up uh through the Wix services, is going to be all available here. As you can see, I already have a couple of test ones that I did, but I can go here on the profile and see any orders that they did. You can have access to the invoices, for example, or any subscriptions that you create. Okay? Anyway, all we really need from here is going to be the client ID. So, you can head over here to settings, to headless settings at the bottom, and see I already made a bunch here. You can just create a new client ID here, and that's it. And then you copy this over. And before that, also go to settings here, you also will need to set the URLs here for the allow redirect domains to localhost 3000 and localhost 3000 down here as well. So, when you are getting redirected from the Wix sign up page or login page, it needs to know kind of where to go and what's allowed because if it's another kind of if someone tries to inject in another URL, for example, right? You don't want that to be authorized. You only want to make sure that it's your domains that you set up here. So, when you go live with a website as well, you can pass in the live domain where you have the website hosted. But for now, this is fine. We can just copy and do it on localhost because we are running in development mode. Okay, so you can pass that down here if you create an ENV file in your root directory. So, as you can see we have one and I pass this down as next public Wix client ID. All right, and that's all we need. So, here now to access this, all you need to do is prefix it with the process.env. Like that. And then you can pass it down just like that. All right, cool. One last thing we need is tokens. Now, Wix is going to use these tokens to basically identify whoever visits the page, like who are you? Are you a visitor? Are you a member already? Do you have any products that you bought here already? So, that's what we need to pass down here. Now, initially, when we create this client, we're not going to have any of it. But what we can do is read it off the user's cookies and see if it's there's something already is there. If there's not, then we'll just set an empty value for now. So, let's do that. I'll define a refresh token here. I Oh, sorry. This needs to be outside. So, I'll just go outside here. So, I'll just go here to the top and I'm going to say refresh token. I'm going to set this equal to JSON.parse and I'm going to say cookies uh dot get like that. And I'm going to get the refresh token. Now again, we don't have any of this set up. So, that's fine. Uh let's also import cookies from JS cookies. Since this is a client component. There we go. Okay, so we're we're trying to get the the refresh token here. So let's take this and pass it down here. All right, so we'll do tokens. Let's open this up cuz it's going to be an object. As you can see, it has two. So access token is basically going to say, "Hey, what does this user has have access to? What services?" All right, this is going to actually hold information of like the role of this user is a visitor, or the role of this user is a member with these specific things on it. Whereas the refresh token kind of checks that access token after a couple of minutes, every couple of minutes, and it says, "Hey, is this still true? Are we still logged in? Are we still this user?" So those are the differences between the two. So we can actually do that for this one as well, so we can do access token here. There we go, and then here we can just say access token like that. All right, so we can pass these two down. So I can say access token and refresh token like that. Okay, cool. Now that we have this set up, let's see what else we need to do here. And this is pretty much all we need. Now we can take this and just export it like that. So export const Wix client, and we can hit save, and that's pretty much it. Now that we have that set up, if we refresh the page here, let me just go here to our application. I'll just get rid of this refresh token that we already have set here and access token. I had this before I filmed the video. So let's just refresh the page. What I want here essentially is when the user initially visits the page, I already want to have an access token added here and a refresh token added. But that access token is going to be just like a visitor token. And once the user signs in, I want to essentially switch that and switch its role over to a member token. Okay? So essentially what we want to do now is when we head to the page, set an access token here. That's a visitor. And the way we can do that is through middleware in Next.js. So, that middleware is going to run before every request. And so, before you get to a page or a server component, it's going to run the middleware first. So, we can just go here to our root application here and make a new file called middleware.ts, like that. Here at the top, we are going to import that OAuth strategy and the create client that we had before. Let's go to the bottom. Here, we're going to say export const middle where. I'm going to set this equal to an async function. This is going to take in the request, and this is type of next request. We'll open this up. I'm going to get the cookies here, and in this case, now that we're working in a server environment here, what's going on here? Oh, I need to run the arrow function. There we go. Um we can just access the cookies without any packages. So, we can say request.cookies, like that. We don't need to call it. We can just pass it down like that. I'll also get the response here. So, I'm going to say here next response.next, which allows us to continue. Um and then here, I'll say if there's I'll try to get the cookies first. I'll check, hey, is there a refresh token here? If there is, then I'll just return the response here. All right. So, we just skip, and then we keep going uh to our server component or wherever we are. Otherwise, here, uh I'll create the Wix client here. I'll say OAuth. Let's set up the OAuth strategy on this. Pass the client ID. What did I name this? There we go. Let's just copy it over. Oops. There we go. And now, what I want to do is set those tokens. So, you can just use the Wix client here to generate that. You can see await Wix client auth generate visitor tokens like that. And then on the response I can set the cookies for the refresh token. And I'm going to say JSON.stringify tokens. uh refresh token. Not only that, I'm also going to say res.cookies.set access token. I'm going to set the What is it? JSON the stringify tokens.access token as well like that. Perfect. All right. So, now that we have this set up, um what we can also do is set maybe an age on this if you want. So, you can do it on the refresh token here. You can set a max age. Let's do 60 * 60 * 24. All right? Leave that in the comments. Do the math. Let's see who gets it right. Okay. And that's pretty much it. So, now at the end here we can return res, which is going to again just kind of go to the next uh bit of our code. All right. So, let's have a look now. So, if we head back to our application, as you can see we don't have anything set up. Do a refresh. Boom. Would you look at that? Look at that. We have an access token set up with an expiry date and the auth here. And then the refresh token has a value and holds the role. And look at that. We have the visitor. Lovely. I think we're ready to set up the login button now for our user. So, let me kind of show you how I um we're going to do that. In our components again I have a nav here. And inside here I'm going to import this nav user, like that. All right. So, this is the nav user. It's just a client component. It's just a div with a H1. However, the way I'm going to import this is just a tad bit different. Rather than doing it like this, I'm going to be using this dynamic feature up here. So, we're going to import it like this. And what this is essentially going to allow us to do, if you set SSR false here, is you can work with the window object. Otherwise, you're going to get an error thrown saying window is undefined. That's because Next.js will try to run everything on the server first. And of course, the window is not a thing on the server. Okay. So, let's head back to our nav user now, and we can start hooking this up. Okay. So, what's the first thing we need to do? Well, let's go here. Let's create a function that logs the user in. So, let's just do it all here. You can again separate these into separate files if you want. But, I'll do it here, so it's nice and easy for us to see. Okay. So, we're going to make a login request here. And this is going to come from the Wix client. So, let's import that. The OAuth. And here, what we're going to do is say generate OAuth data, like that. And this is going to take in one argument here, and that's basically going to be the redirect URI. So, in our case, it's just going to be localhost 3000. So, let's pass that down. Okay. So, we got that. And next up, what we want to do is go to our local storage and set an item here. And we're going to set the OAuth redirect data. All right, just so we have context on um Let's pass in this login request here. Here we go. This is going to keep track of all the all the links that we set here for the redirect URI and some custom data as well from Wix. Uh we're going to be able to view this in just a second. Last but not least, we're going to need to get the actual OAuth URL here. And this is going to come from await. We're going to say Wix client OAuth .getOAuthURL like that. We're going to say login request. So, they're essentially going to return you a code and a token or a status uh that you can read from the URL uh and set that in your access token to essentially authenticate the user. So, here at the end you can see something like window.location. Oops. .href and we can set that equal to this OAuth URL. And you're going to see that our URL is going to update. And that's pretty much it. So, that's how we do the login. Okay. Now, let's start hooking this up a little bit. Uh let's head down here to our returns. I'll just get rid of this. And what I'm going to do here is do a Let's do a button for now. Well, let's see. We can do Wix client here the OAuth is logged in. Where is it? Logged in like that. And conditionally display uh either a button that says sign in or the user. So, I'm going to do is logged in like that. That's a boolean. I'm going to put a question mark here. I'm going to render out a div like that. Otherwise, I'm going to render out Let's do a button here. This is going to be a normal button that says sign in like that. I'm going to put an onclick on here that's going to have this login that we just made up here. Uh we can also control the UI a little bit of this. So, I can do something like create a state up here const loading and set loading. I'm going to say use state. I'm going to initialize it with a false value. And here, I'm going to set the loading to true when we initially start this. And at the end here, I'm going to set the loading back to false. So, here now I can see something like, "Hey, make sure this button is disabled when loading is true." So, I can just say loading there. And also here, I can conditionally render this with something like this. I can say, "Hey, if loading is going on, then say signing in. Otherwise, we can say sign in." There we go. Something like that. Um Okay, and then otherwise here, right? We want to display something else out. Let's just say logged for now. And go and give this a go. So, let's head back here. As you can see, we have a nice sign in button there. When I click this, oh, signing in. And then, would you look at that? We get redirected over to the workspace. So, here, now this is all managed by them, so you don't need to worry about actually hooking up Google Auth and getting your bloody uh app authorized and all of that jazz. Uh we can just do a tree here or like log in with email, right? And then you have to bcrypt everything and all that crap. So, I just do a Google. And look at that, we are getting signed back. And look at that, the URI here uh is getting set with a state and a code up here at the top, right? Here we go. Now, nothing happens uh because we need to update the access token and the refresh token to the one that we got back here. Otherwise, we cannot read the user. So, that essentially doesn't really do anything, but we know it works and the redirect and everything works. So, if we head here, as you can see, we still have a role of visitor and none of this got updated. So, what we're going to do is create a little function here that's going to verify the user um after we get redirected back. So, let's head back here. I'll do it here at the top. I'm going to call this an async function. And this is going to be verify login, like this. Let's open this up. I'm going to try to get and parse the data here from the local storage. We're going to say get item. And I'm going to get this OAuth redirect data here. Like that. And then what I'm going to do is try I'm going to say const get the code and the state from the URL. I'm going to call the Wix client. I'm going to say OAuth. Parse from URL. That's essentially going to extract uh that state and that code from our URL here. And after we have that, what we can do then is actually pass it down to our cookies. So, um what we can do is say Wix client .auth.auth.set tokens. And we can pass it down like that. Sorry, this tokens, where is this from? I'm silly. We need to do up here. We can say let's try and the try here. We need to get the tokens. Await. We're going to say Wix client.auth.get member tokens, like that. Now I can pass in the code, the state, and any additional data that we get here from the redirect. And that's it. I don't even think we need to set the tokens here on the client. Uh we can just set in in the cookies. So, let's do cookies.set. And then let me just cop- copy copy this over. There we go. So, set the access token and the refresh token over to the tokens that we got here from the members. Okay, now that's erroring out because we don't have cookies imported. So, let's just add that as well. And that's pretty much it. And then down here we can catch the error and say console.log I'll just do something went wrong. All right, there we go. So, that's all set up now. That's good to go. Last but not least, what we need to do is run a use effect here. So, let's run a use effect. And then we can just pass uh the verify login here. And then this should run only when the user changes. Now, we don't have the user here. So, how can we actually get the user? Let's head back here to our nav. But, the problem is that we're trying to fetch the user here on the server. And this uh the other one the client that we have here in our lib, this one is only going to work in a client component. So, what we're going to do is set up a similar uh little function that kind of looks almost like this one. Let's just call this Wix server.ts like that. I'll paste this in here for you so you can see it. So, essentially it looks the same. We're importing the same stuff. Again, we're trying to read the refresh token here from the cookie store. Again, here we we're not using JS cookies anymore. So, we can just read it like this uh straight from cookies from next headers here. Again, I'm passing down the modules, the OAuth, and just returning that client. Okay? So, now we can use this in a server component as well. So, let's give that a go. So, here we can say const um Let me think how we're going to do this. What if we also do the fetching inside a new file? Let's do it here. Get user.ts like that. So, this is only going to run on the server. We can mark it as well like that. Use server. And I'm going to import this Wix client server that we just made. What did I name it? Wix client. There we go. Um No, it's Wix server. Wix There we go. Wix server. Wix client server. There we go. Cool. Here, I'm just going to run an export async function get user. Let's open this up. I'm going to try to do a try and a catch here with an error. Here, if there's something went wrong, we'll just return undefined because we couldn't get the user. Otherwise, let's get the Well, they call a member here, so let's call it member as well. We can run an await Wix client server {dot} o off. Sorry, not o off. We'll do members like that. And we'll say get current member like that. Okay? So, oh, and at the end here, we can actually return the actual member like that. So, this is either going to return us the member or undefined. It's as simple as that. So, now in our nav component here, we can use that. We can say const user is equal to await get user like that. Simple as that. And then we can optionally pass this down here as well like that. Okay? So, if we head over to our nav user, we should be able to import that here. So, you'll have it access here. And we can also pass down the types for this. So, nav user props. Let's just define it here. Type nav user props is equal to So, we're going to have the user and the types for this I'm going to leave it. And the types for this I'm going to leave it here for you. Um I'm going to paste it in the file here where the profile. As you can see, we have a nickname, a slug, a photo, a cover, and a title. All right, that's pretty much all you need to get from here. Let's actually head back to the get user here and just from the member here, we can just get the profile directly like that. Cool. So, the type here is going to be the profile type. Let's import that and we are good to go. Okay, so we're passing the user down here. And now here as well, we can do some conditional rendering. So, here if we're logged in uh rather than showing this, we can just show the user. But, let just see if this works. So, let's do sign in. Let's try with Google. And we are getting redirected. And now if we Let's try a refresh, maybe. Oh, yeah, look at that. Now it says logged in. Now, as you can see, it didn't uh instantly re- refresh because Next.js does some caching, uh but we'll get through that as well. Okay. So, now that we're logged in well, let's try and make the log out file as well. So, let's go here. We're going to say log out. We're going to run an async function again. And we're going to say set is loading true as well. There we go. We'll remove the cookies, right? We don't want the access token or the refresh token to be the there anymore. So, let's pass that down like that. And then we're going to say log out URL here is going to be await Wix client .auth. log out like that. And then we can just pass uh the window.location.href, which is our current URL. And then we can set is loading back to false. There we go. And then finally we can update the window.location.href over to this new log out URL. And that's pretty much it. That's all we need to set up. So, now here let's customize this a bit rather than just saying logged here let's expand this div. And what I want to do is add a image stack here. So, that's comes from Next.js. And then the alt here is going to be user. nickname. The actual source for this image is going to be user. photo URL. There it is. And then we need to set a custom width and height, so I'll do 32 and 32 for both. There we go. Lovely. So, we got the image tag. And besides the image tag, I also want a button here that says sign out. So, if we go here and add an on click on this, we can attach this little function that we made up here. So, log out. All right, let's give this a go. Let's head back here. Let's do a refresh. It says couldn't read the nickname of the user. Let's add a question mark here at the beginning. That might be it. And there we go. Okay, let's do a refresh. Um For some reason, this is breaking. Let's actually try to JSON.stringify the user. Maybe we can figure it out. Let's pass the user down there. So, now if we refresh, looks like this might just be undefined for now. Let's make sure here it says the user is a type of profile or undefined. Maybe we'll try to console.log out the user here and see what happens in the terminal. Okay, so it does say undefined. Oh, we're never returning the member here. So, let's say return member and hit save. There we go. Look at that. Boom. There's the user. Bloody hell. Okay, let's head back here and see now. Oh, look at that. We got the JSON.stringify, we got the image, and the sign out button as well. So, let's just go back here and remove that JSON.stringify. We don't need that anymore. Perfect. Look at that. And finally, let's add some styles here. We can add a class name, say flex, to kind of put them one beside each other. We can add a gap of six, item center. There we go. Style it up a tad bit. Let's try the sign out as well. So, I'll click on it and look at that. Boom. It refreshed the page as well. So, as you can see, we don't have that caching issue anymore. And now if we check the inspector here, you're still going to see the You'd probably think, "Oh, okay. So, the access token and the refresh token got removed." Uh it did, but at the same time the middleware also ran after that. So, it just switched it back to a visitor role here as you can see. Okay. So, that's pretty much it. We are good to go. Our authentication is done. If you check here as well, you're going to see that the member gets added here in the UI. Uh one last thing we want to do is make sure that it refreshes after we sign in. So, to do that, it's going to be super simple. All we need to do is head here to the top and import router from use router next navigation. And then here after this gets all finished here with the set loading after the login, we can just run router.refresh. And that should do the trick. So, let's head back here. Try the signing in. Oops, my apologies. We need to run this router refresh here in our verify login. So, let's paste that there instead. Okay, let's give this a go. So, let's hit signing in. So, now that we go through the login process, you're going to see when I hit login, it's automatically going to refresh and then update the UI for us. Now, look at that. Boom. We are officially signed in. So, there we go. That's a little tutorial here for you how to set up and work with off and Wix both on client and server-side. And then from here on, you can expand on it and build your e-commerce website on top of it or your a CMS block or whatever you want. So, that'll be the next episode. Really excited for that. And yeah, thank you so much for watching. Drop a subscribe if you like this type of content, and I'll see you in the next one. Peace.