The BEST OAuth Golang Tutorial for Authentication | Sign In With Google

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
every time I have tried to search up a o tutorial on YouTube that uses goink I am not able to find a straight forward answer so in this video I'm going to introduce you to a package called goth or gothic by Mark Bates it is a gold length specific package that makes handling o and just all authorization authentication for your goaling application just so easy do you want your users to log in with Google or Discord or GitHub or anything this is the package for you it makes the breeze all needers three separate routes and I'm going to show you how to set all of that up on a front end as well so make sure you stay until the end to the whole tutorial so the package I'm talking about today which going to be the highlight and center point of this video is goth by Mark bat it is on get up 4.4 th000 stars and it's just a great tool goth multi-provider authentication for go package goth provides a simple clean and idiomatic way to write authentication packages for go web applications you can see here all these support providers I mean they lit Lally have everything GitHub Eve online OCTA sales for Shopify SoundCloud SoundCloud it has everything basically so the very first thing that you're going to want to do is head on over to console. cloud.google.com go to your API Library going to create a new credentials application an oath client ID as you can see I have a couple already that I'm going to use for this demonstration because this sets you up it gives you the client ID and secret that allows go to give the token for the handshake on your back in application there's a way to create and reroute the call back to Local Host 2000 or wherever your server is hosted again this is server side handling so you want it to be where your server is to hit the endpoint I'm going to show you to set that up on the back end right now but make sure you get the accurate EnV keys from Google to set this up because we're going to use Google in this example all right so now that you got your keys ready we're going to go ahead and spin up a new project using go blueprint create the name of this app is going to be called go off uh we are going to use cheat that's just my preference it doesn't really matter what DB you're going to use but let's go ahead and choose postest for this example Okay cool so we're going to CD into go off I'm going to open up this project and there you go now we have a completely spun up go Application ready for us to use all right now we have that set up we're going to have to actually install our goth package here you're going to do a go get github.com slm Mark baits slgo just press enter and there you go it adds the package for us it adds an addition Google protuff everything it needs to run successfully there's the oa2 package as well and one more is we're going to need the GitHub gorilla session so go ahead and do goget github.com gorilla sessions so after we installed gorilla sessions we basically have everything we need to start coding and actually getting this to run but just before we start I want to share one thing and this should be the go-to for everything for goth it actually has an example folder it has one file called main.go that's going to list everything you need to actually organize and run this with any provider that you want like I said earlier I'm going to do this Google but you can choose literally any single provider you want and if you ever get lost if this video is not clear go to the documentation go to the example it has it all here for you all right so the first thing I am going to do is I'm going to create a new package called o I'm going to go and create a new o.o file just going to go package off and this is basically going to do instantiate our Gothic store and since it's a global variable a global store once we Define it in our a file here A.O file and we instantiate in our main.go file over in CMD API right here we can basically use it throughout our application code all right so let's go ahead and start with our package off file here the first thing we're going to do is create a few constants here we're going to do a key so this signing key do a random string make it definitely more secure than random string our max age is going to be 864 * 30 I think this is 30 days I'm I'm not mistaken and then we're going to do an ISR vers varable which basically declares or is this a production or is this still in development and then we're going to create a new function called Funk new o and this is going to be the Constructor or what's going to get instantiated for our Gothic store first thing we're going to load up our go. EnV our environment variable so go load if error does not equal nil we going to do log fatal error loading. EnV file next I'm going to declare my Google client ID os. getet n v Google client ID and copy this and just put Google secret and then Google client secret all right cool so all we've done so far is load our secrets from ourv file using go. EnV next up we're actually going to declare this store exactly how it's written into documentation we're going to do sessions from gorilla sessions we're going to do new cookie store it's going to take bite of key our hashing key which is back on line 14 over here now that we have our our store which is just from gorilla sessions all we're going to do now is bring in our Gothic package here so Gothic store and then here we're actually going to provide the list of providers that we want our application to accept ooth Connections in this example we're going to use Google so we're going to do goth do use providers and we're going to paste in our google. new with our Google client ID our Google client secret and the call back for our redo now this has to be the exact same thing when you created it in your Google client API page which I showed at the beginning of this video all right so now that we have our providers running in Gothic and we're going to instantiate it with our new o in our main.go file the next thing that we need to do is actually grab these three routes from the examples from main.go from The Goth repository what these three are responsible for is actually the call back that triggers the handshake between goth and the oo provider or whatever provider you choose to use and then obviously this is the log out path here now because I'm using Chi as a middle when when I ran my project at the beginning with go blueprint there's going to be something that you're going to need to include if you want to use chi in your project the example they have here simply uses templating so they just write HTML and they generate from their server I want to use Chi as just my backend router in go and then I'm going to spin up a v react app on the front end all right so let's go ahead and write this one out so I'm going to write out the main call back function but the other ones I'm just going to paste in just to save time here going to call our first function get off call back function doesn't really matter what you name it it's going to take our classic HTTP response writer and our HTTP request and then we're going to just basically copy this block of code here from the examples going to paste it here and change this up here cuz we don't have res and request we have W and R and then all we're going to do is fmt do print line user and this will give us our user information but I want to give you guys a little bit more than just that like I said earlier this is actually not going to work if you use Chi uh if you spun up your project us can gold blueprint it's not just Chi it's not going to work with Jin it's not going to work with any of these middleware packages because the way the context is passed and the way goth looks you actually have to do a little bit of tuning so the first thing you want to do is create a varable called provider you're going to do g. URL param R and then provider so basically what this is going to look for the URL parameter provider if this could be anything you want to be Google could be whatever I'll show you how we Define it in the routes above but basically this can be a placeholder for whatever the provider you choose to use so I'll show you what I mean if you do an R get right and then you pass in the route for this provider call back is going to live it's going to be SL off SL provider SL callback and then the function is going to be the get call back off function so basically now this Factor here this value here this you don't have to change whether you change from Google or Discord or whatever provider you can leave this the way it is however we need to create this provider variable to pull in that provider link from our actual URL parameter and now this is not done we actually need to modify our rer our HTP request pointer because this needs to be passed into what this provider variable is and we can do that with r dowith context context with value context background or if you use an existing background you can use that that's fine and here we can pass in provider and the last one will be provider okay awesome so if you use this this will work but there's one more thing I want to include whenever you do get off call back function you want to redirect your user you don't want them to just stay at that page cuz nothing is going to happen you always want to redirect them back to the route or whatever page you want the user to go to after they successfully log in and you can do that easily with the HTTP redirect method to w r and then this path Local Host and then the path that you want to redirect to http Local Host 5173 this is the default Port that V spins up on and then you can do HTTP status found and now we're going to go back to our example here and we're just going to basically copy and paste the log out function and the off provider function as well now we are almost dump but let's go back to our main.go where we have to instantiate Gothic so up here I want you to go bring in the packet our internal package which we created earlier so internal off and in here what we're going to do is off. new off and that's it if you follow this remember this just pulls in our environment varable creates our store from the sessions gorilla sessions new cookie store and then puts it into our Gothic store like so and give it our list of providers which our application will accept all right so we have gone through what the magic of go o or goth is going to be I've explained a little bit of authentication I've even showed you to basically spin up an application with o here but now I want to show you how you can actually use this with a front end we're going to quickly spin up a typescript react project we're going to modify just a teeny weeny bit cuz we're goang developers not JavaScript typescript developers but I'm going to show you this end to- endend flow so stick around right now till the end okay so I'm back in my application you can see here I didn't do anything behind the scenes it's the exact same everything runs as expected but now we're going to use vit which if you haven't heard of it is this tool that spins up front-end projects using react you can choose what language if you want to use react or components it's really really neat it's pretty lightweight you know if you don't need something like next you just want to front end to do front end things and you hook it up to back it I think this is really the most vital option to you so go back I'm going to create the npm create V at latest command after you download it we going to call our project just our client and here you can pick react preact lit whatever I'm going to use react we're going to use typescript not server not s swc whatever that was and we're going to follow the instructions we're going to CD into our client I'm going to run npm install and next we're going to run npm run Dev and you can see here 5173 that's the port which we actually call for a redirect after a successful login if you just click this there you go we have a v app ready to go all right so now let's quickly just jump on over to react our V app here and we just have to modify it we have to just add a login function and log out function log in with Google if we go back to our V app you can see here we get our login with Google prompt like so all right now before we take this out for one spin we know here we have one terminal with our V app now in this second terminal all we have to do is Click make run this is going to launch application on report which is definer EnV file I have set mine for the purpose of this to Port 3000 again Alex you better edit these out okay so now our application our back end is running on Port 3000 while our front end is running on Port 5173 if you hop on over back to our local uh V app it's now private in cogn cuz I don't want you guys to see my emails but if you log in with Google you can see we can type in our email or phone number it's going to prom me to put in my password click enter so now we have logged in but we can't really see it how do we know it worked well if you remember back in our routes. go file here where we actually start to get off call back function we actually have a line here of printing the user after we complete the user off from Gothic and if we open up our terminal we'll look at what we have we have the entire user struct from Gothic and from Google more importantly we have our email we have the ID we have the picture we have a bunch of information we have the expiry of the token all of that information is available to us to consume through goth you can see the expiry of my refresh token everything here is available to be used and this is how you actually use and structure oath handling using Gothic now I have this struck and I'll show you where you can find more information about this you can see here over at PKG dog go.d and the mark baites goth you can see here that this user stru contains information common amongst most oos and O2 providers you can see here the ID token refresh token access token location Avatar user ID everything you need is going to be available in this user struck which I printed in the terminal here below all right so that was the entire breakdown the entire tutorial from start to finish of how you can literally spin up a project using go blueprint you can handle off using go off and you can spin up a react front in using V to literally have users sign in to your application using Google or any other provider we use Google in this example cuz it's probably the most common so I hope you enjoyed this video make sure to comment like And subscribe if you haven't already and let me know what video what go tutorial go breakdown go anything or any video would you like to see me do next time but I got to leave you guys off with one thing and that's just you got to [Music] power
Info
Channel: Melkey
Views: 31,250
Rating: undefined out of 5
Keywords: video sharing, video, sharing, computer science, software engineer, silicon valley, computer programming, coding, learn to code, machine learning, artificial intelligence, cloud, python, javascript, how to code, Data scientist, AI developer, Machine Learning, golang, tutorial, oauth, go programming languge
Id: iHFQyd__2A0
Channel Id: undefined
Length: 13min 59sec (839 seconds)
Published: Tue Nov 28 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.