Golang HTTP User Authentication Yabi Series 3 | Golang Web Development | WebAssembly Auth System

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] okay guys welcome back to the channel this is tutorial series number 23 here in harlequin's code okay uh we will continue again guys with our uh um sub series here like uh this is what we call the yabby series um this is part still part of our bigger topic here bigger discussions regarding this golan web development series uh right now we need to continue guys our users registration as you as a brief introduction again about tcib series this is a complete user's authentication system that we are we are building it from scratch using a golang web programming language go like programming language of course we are using the web assembly here and then let's let us continue again this discussion guys because this one is uh we're about to create a new user of course with the mysql database so we already created the database in our previous discussion and now we we are about to create the api which is to cater these user's registration so uh yeah our end goal for this one is now we need to this yab package we call it the yabi yabi package we will publish this on our github there in our it republic github and then so that we can reuse this whenever we have a new project so that we will not be redoing every time we have a new project there is always especially the web development as always have this uh user's authentication system okay so we will dive into this tutorial guys okay guys welcome back to the channel okay this is what we have done earlier in our previous discussion guys we have successfully created our wasam here our webassembly in this case we have this api called the user's registration here but we don't create it yet at the moment now we need to continue creating uh this user's registration guys uh this one will be inserting a new user from our mysql database here this is what we have done early early on we have successfully created this our yabi user table here in our database mahalikans code right now we have some very generic users what they call this user's information like the username password email first name all these things guys so these are all the things that we need to insert into this table every time there is a user's registration there so at the moment we need to fill up we don't have a data yet so we need to create this api called this uh regis register api so with the post method okay guys so we need to i think we need to copy for this this type okay so i just uh what they call copy at the moment guys so we can fast track our tutorial here okay so we will be creating this uh what's the name there this is the exact name so we'll be creating this uh register uh user endpoint okay so this is to register okay we just put some information guys is to register a new user okay like that okay so we will uh these are all the standard headers guys so we just skip for a while we don't talk about it it's just like an elephant in the room kidding you don't know this one is done we have already uh done previously guys it's like an elephant already there guys so just sit there and just uh just just leave it there guys okay so these are all the things uh these are the standard also this is also called an elephant in the room okay there is a username password okay maybe we will uh okay since we have some information for our username password there so we already pass on all the payload here username all these things guys so we need to uh copy all these things so okay uh maybe okay i just just paste here guys so because we need to copy paste we don't want to uh get all this from other from the other tab so okay so probably this one put down okay so we have username here so the username ensure that it's matt yeah it should match guys this one is our local variable so don't um don't bother about this this is our local variable name this is the most important part because we already have this payload here we need to be exact all these things okay so remember again these are all strings guys as you notice that there is a boolean value here although it's a for true or false but we need to translate everything in our payload as all uh strings okay so uh we will continue on remember always guys this one all strings okay later then we will convert it here so username already pass on the email okay so this one we this would be the email as well and then the password okay then confirm password so we need to copy there the toes should be you should copy this and then just put everything the same guys so easier for us to copy paste okay so also we need to have some active one here so also uh true or false it's a boolean value okay so we have done so ensure all all the things there okay so present this one we don't want so at this point um since because our goal here is we need to reuse this uh package guys we don't um we don't validate here uh everything in our uh in this old api because this one is more on the customized part here what we can do is uh we have a yabi uh package here although we still have only one we only have one model user go so we need to add a new file here guys so later on it will add on so instead of doing here we will migrate everything to our yabi there so we'll just remove these guys so this one later part we will just pass on so there is a user endpoint okay so okay so we need to maybe we need to minimize this okay i will create a new file here guys called the user um probably this would be the user.go okay so our user that go we need to initialize this package yeah be here and then this user go probably we will be we need to create a new function here guys so at the moment we can create a new function for the create user um remember that this yab package will be exporting to or publishing it to the github there so it's easy for us to go get once we have done this project for the crb so we will be published there so everyone can install it in your project guys in your golang project so you can reuse this uh usage authentication systems okay guys so this is the very important guess when we have these uh users or something like once we have done like like for example this user authentication so once we have done this project we can always reuse that uh whenever we have a new golang project guys so so okay so we need a database here okay uh but we need to install the database here first guys sql.db okay so i think i already installed enough oh in my laptop here so we can uh early on i shared with you guys so so we need to search this um if you don't install yet uh okay i don't know what they call this uh golang mysql driver okay oh yeah this one is so this is the official golang driver guys so for our mysql we don't use any orm here we will use it directly to to reduce uh the frequency of these dependencies guys so we don't rely heavily there so we straight away use the mysql driver because this one is um sufficient enough for for us to manage our mysql database so but it's up to you guys if you are using a orm there or something then we can you can always use that but you cannot follow in my tutorial series here so because most likely and most probably the any frameworks there they will create their own um style guys or something like uh there is a functions there okay so we will be skipping that we don't want to use the orm or something like uh gorm or something guys so we don't want and then we will straightaway install this uh in our project so to install there is a installation here guys so i already done this okay so if you want to install then you can just copy this and then head over to here and then you just paste guys so okay so we will continuing so great user this is our first step to create the user guys so we need to create this uh very generic function here so okay uh probably this is the add and you user to the user's collection okay and just say like that guys if you want to elaborate you can also elaborate okay so uh probably just want to put so we will uh accept mysql connectivity here i mean your db connection guys uh this what you call the pointer for this sql driver later on we will use this function i will show you guys no worries okay so we will have this um user uh this one is from our struck here from the model user remember in our earlier early discussions we have this uh first uh what i call this a user struct uh paired it with our mysql database here this is the the exact thing here guys so okay so we will reuse now these users strut okay to to require information from our okay so our user so email configuration probably we skip this at the moment but later we will enhance this great user guys because once we created the user our plan is to there is a two option uh there is uh like a localized uh addition of a user without any email confirmation guys to confirm the user's registration then there is some certain links then they can confirm their email address and then the user will be the new users account will be activated as well based on our model here there is is active here okay so in our main um our payload here because this one we targeted with the registration page normally when there is a user and this one is not new to all of us because every time when we sign up for a certain website or any kind of a thing there you need to have some account you need to create after you successfully created fill up everything all that forms there or something and then with your username and the password and then you click sign up or register or something guys then it normally the system or the any platform there they will be sending an automatic email an email based on your email address that you provided from that registration form and then after that they will you will receive an email saying that there is you need to confirm your new users account or something like that yeah so so here you need to specify the is active this is the key here so these times if you say it's false then we don't send email to or we will send email to the users because by default user signup is not activated of course guys because unless you are you don't you don't want to um authenticate their email address so but that one is a very rare case question but it's up to you okay so we will set this as a default one but there is also another case that's why we are providing this information exactly because if you set this as true that means in your localized system there or whatever whatever you're using that you don't want to like internal users guys like your own staffs or employees or something like that um if you have some system that you integrate this uh yabi package then you can set this as true when you when you register or create a new user it will be automatically activated so this is the two option that we are planning to do here in a yabby series so but in this case this is a user's registration is a public one so we want to validate each of the registered email there or new users account we will be validating their email address so it will be sending an email automatically to their email and then after that they will receive and then click that button there something or the links okay so uh we will continue on um later part we will just apply guys so uh our goal is we do we just want to have some the decent um checking of something like an empty string so empty username password or something and then guys and then after that we will insert the users to our the new user registration okay to our mysql database to test that we really can insert to our mysql database first and later on we will enhance this create user then we will we will have this kind of uh email automatic email confirmation okay guys uh and this kind of a feature okay so at the moment we will just provide with this some basic information here so we want this uh confirmed password as well as a stream this one we don't want to put on our model here guys because um this one is a quite irrelevant one so we don't we just pass on this information guys are also the terms of service uh later i will just explain to you guys why we are having this kind of uh information here okay so we will have this in 64 and then as our return guys and then we'd have of course the standard diode okay so we just want to set something here guys so we will use this now we need to do some validation now guys here we need to check first of course um uh check if uh username guys so these are all the standard validation guys it's empty so we will do this okay just hold on for a while guys just take your cool viewers guys you that now we can use this username okay okay zero then we will return with the zero value here uh this one is a very raw um what they call this programming case okay some validation here then uh username is required uh something i think is okay so we can just uh return something there also for our okay so uh this one also we need to um all those required one guys so we need to have something like the username email all these things guys so these are all the standard here email if the user never key in the emails or email address email is required okay another one is we need to check for the password okay so a password okay password is required oops wrong spelling another one is the confirm password so this time we are not using the you um i mean what they call me directly key in this confirm password here guys because it's not belong this camera password and the tools those [Music] uh the terms of services uh this one are all it's nothing i mean we don't want to keep in the database that's right okay we just pass on uh to this create user function so right now we need to have this confirmed password so confirm password is required okay so another one is for this confirm password that we don't want we have the email username password uh terms of service okay so uh okay this one we need to match first guys uh probably uh we need to match this what i call if this username match with the i mean this uh both password we need to match first match both passwords okay like that it strings that stream space again our favorite command confirm password not equal to strings that trim space and then of course this you password here and then if it is not then we will return something here guys so just saying like passwords passwords did not match please try again again and again okay these are all the information that we need to send back guys uh remember that we are in the yabby package here guys so we validate everything here because this is a one-time validation guys so once we have done this uh it's all in our yabi package already so we don't want to store any validation from our old api here because this is your own file or customize file or something guys you need to provide this registration and then all the necessary validations and the mandatory required information will be all in this future package called the yabi so we will be storing uh everything here so we just pass on this information everything here what we are doing and then it will do the necessary uh insertion to our mysql database there okay so we have done some basic uh what i call this validation here so later we will enhance these guys so our goal is to insert the database there of course the terms of service we will the uh the tools there oh sorry um terms of service um okay check hey terms of service uh okay sorry has been checked or not all right okay so if not uh this means it's false guys so if it falls return zero here um errors new uh okay so we will have some like terms of service is required okay by uh joining okay we will try to concatenate guys um whatever what is our app name here i think it's a con config and then side short name we have side short name okay so this is our site settings i think site's short name so what is your settings here you have some site short name okay site full name okay so go web this is our slide short name here so we will just this is our go web so probably we can use the same thing marley can school so we will use the site short name here and then then we'll try to concatenate guys everything so you are agreeing okay hold on follow my guys i think this one you should you're uh agreeing quite long right you guys so we can just [Music] put down below there you are agreeing to our terms and conditions okay so enough then it will return like that after that we will try to insert now guys um okay so before that we will uh try to we will test first guys you know it really can can uh send all these things uh i mean it can validate or not before we can insert first guys to our database there so probably we will call that okay guys so we will continue on here uh we will call that the function there guys so uh we need to check first check if uh is uh active is uh is true then we don't send we don't send we don't send an email guys uh email confirmation confirmation to activate the new user's account okay so we will check first if it's active this is active here so this one if it is true guys uh what i'm uh we're in our payload there so we just have this validation and then we can have also some else statement here so we can validate this we will try to use now the yabi guys so this one is a error here okay and then i will try the yabi that now is the crate user is existing there so at the moment we don't have the db connection so we will provide it with the database connection guys so i think we will come back here first uh we will uh directly insert now the insert statement guys so we will create that one first okay so because we're in our uh one of our parameters here is the database so maybe you can remove that one but it's no point we just straight away create our database here so but uh of course we need to hash the password first guys and sort uh your plain text password so like um this one is a built-in with the golden guys but i also created one package guys the uh they call it the sakto guys uh under the it republic there so if we don't have this yet so i already pre-installed you know in my laptop here so uh hash and salt this one you can use this one guys the sakto is here i already displayed somewhere oh yeah this one guys so i really prepared so this one in my this is my uh own package as well guys so uh everything here i uh what they call this uh all the necessary checking like the email address valid email address or password housing hashing password dashing in our here if you want to check this and you can visit to my iti republic then the sac2 package there uh these are all the hash function that i'm calling guys so to generate this one using the b here and then all these things these are all the standard package with the golden design this one is our helper uh every time we want to use this uh all the common validation uh will be here just like the username to capture our time so we we that's why it's it's a good thing guys that we have our uh it's like it's like a modular kind of thing like if you build this you can reuse this uh your entire project or new project uh you don't want to rip keep repeating and you need to copy this function again to your individual golem project or new golang project uh that one we don't want to repeat ourselves this is uh the goal for uh golang packages guys or any kind of programming languages guys so like you have some like uh common functions that we need to use or the common utilities that we need to use so in my case i created this sakto sakto means in my own language and they call it what they call this is a correct one something like uh some kind of a validation guy so these are all the common validations like is email here i already predefined all these things guys so we just want to validate this one is a is a tested one and then a url also if a url is uh is correct or not all these things then the ip address time parse and many more here guys you need to parse a float to string all these things yes uh every time i encounter new uh common uh functions i will update here in our sakto package here in uh it republic so okay so if you if you are not yet installing this you can probably use these cases this one is open source also so you can uh install like this go get and then it republican the suck tool package then there is some sample here in my common one in this case is the password thing so this one is a very common as well or what they call um any kind of uh email validation all these things okay so we will use that so in this case i'm using the hash and the salt okay so that every time you don't want to you uh use or call all those uh codes so every time in one line only it's good guys so in this case it requires a byte uh of course the this one is a b crypt kind of thing and then we need to key in the user's password and the plain text password from the user side i mean when they submit the user registration forms okay so return zero if any errors there and then just return back to the user okay so we have done this now we can insert the new user's information here guys so we can just insert now we will use the golang's built-in mysql db driver guys so right now we can reuse our con here and then we use the prepare statement uh in this case uh double quote and then we can do the insert into in this case i will uh yeah be user because this yab user guys remember that in our model there this is the yabi user here so we can use this constant here to use that ensure that you have this table name guys called yabi user okay in your mysql database so in this case um probably we can do the insert now okay so insert statement here so we will do like our username uh this is our field name guys from our mysql table okay so email we need this and then the first name and then what else i think we need to put this down okay hold on guys it's uh quite long okay first name and then okay i think hold on guys i need to concatenate all these things and then bring it down okay so we can now bring it over okay maybe later but okay we just uh i don't know all this thing has middle name later but just sometimes uh confusing suffix is super user and then is admin and then they join and then is active okay so we will have this and then okay guys uh to capture that time i predefined i have free fill up value for you all all these things guys so this is what we have done this is for the insert statement using this prepare statement from this uh golang mysql database mysql driver guys so we will do the necessary like the insert into all the necessary fields here and then with this uh question mark is all the values inside so ensure that uh whatever you have here uh how many fields that you are providing here like in these cases i think it's a thirteen one two three four five six seven eight nine ten eleven so eleven uh fields here that we need to do the insert statement so uh ensure that your question mark here this is uh for our arguments uh parameters guys to fill in all the information from the users so uh after this any error then you just return with the error guess don't proceed with the rest here so if no errors from this statement here then we can proceed with this insert statement okay so uh all this username uh from the uh user's side we will we will we will insert okay so to our mysql table and then this uh what we call we need to get the last inserted id id guy so i also created this new field here called a new function called get last inserted id so in this case we will use the query row from of course from the uh mysql uh driver and then we will just select with the id there so ensure that we have the id here guys this is the id here this is our primary id with the auto increment number and auto increment value so we will get the latest id there and then we just uh what they call the dist limit one with the scan uh feature this is the query row guys uh it will only fetch uh one row okay so this one is a very fast also so no need to worry about the uh what they call this um the slowness or something this one is from the golang mysql uh driver itself so this one will always uh fetch uh this uh some information here these are all the you can fetch with one row guys okay so these are all the background here all the arguments if you want to go with deeper when you reach this query row function so okay uh we need to pass drag a bit guys and then right now here in our create user we can now specify uh something our mysql database guys okay so i think we are dragging too much of time guys so we will continue uh about this uh probably in our next discussion guys we will continue this and then we can probably insert that database a new row there okay okay guys welcome back to the channel okay uh we will continue and guys uh i don't want to drag too much of time is too much of time already we can spend so probably in our next discussion uh sure we can insert the table there so keep watching on this uh this series guys because this one is uh uh not quite straightforward but uh once we have done this uh project the yabi project uh definitely is very useful in our golang's projects every time we have a golang web project of course there is a user's authentication system that and then we can just reuse this and then to speed up our workload so we can focus on our requirement only so we we all have done this and then the user's registration so no need for us to um no need for us to redo every time we have done this project then we just or if you have existing there then you just copy paste or something but in this case uh our end goal for this is to publish this on our github there and then we can just reuse every time in our project we just use a go get and then we can supply these uh required information no worries guys uh we will uh this one is um it's a bit huge topic as well because this one we will go in detail every time we need to validate all this one is a critical also because we need to validate all these users so this one is uh it can fit because we have design designed this uh yabi authentication system as some point some kind of a generic user's authentication so you can reuse okay so reuse back all these information to your system okay so thank you so much guys for watching please consider subscribing to my channel so that you can have more videos like these and to also to to to honor our uh my time here of course please consider subscribing so that it's a win-win solution guys so thank you so much for watching uh may god bless us all thank you and [Music] bye-bye you
Info
Channel: Maharlikans Code
Views: 106
Rating: undefined out of 5
Keywords: Life As Software Developer, go, golang web development series 23, golang web development course, web development with go, golang tutorial, learn to create web applications using golang, building scalable web apps in golang, golang good for web development, golang http user authentication, golang http authentication, golang user registration, golang authentication microservice, golang session authentication, golang webassembly, golang mysql, golang mysql tutorial, golang sql
Id: Dn6On9D5u38
Channel Id: undefined
Length: 41min 23sec (2483 seconds)
Published: Mon Dec 21 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.