Go tutorial part 8 (Web app part 6)
Key Takeaways
This video tutorial demonstrates how to implement basic user authentication in a web application using Go, SQLite 3, and SQL, including user data storage, verification, and cookie-based login functionality.
Full Transcript
hi guys welcome to go tutorial part 8 my name is tensor from the sensor programming blog and today we are going to implement a database as well as a form of user authentication into our application so this application this user module that we've created thus far let's go over what we've created so we're going to run it real quick so here's our application we have a login form we have a signup form we can sign in and when we sign in we get to this little dummy page and it shows our little user name up here as well and this is being passed through a cookie which is nice so of course the biggest problem with this application as it currently is is that when you sign up you do not actually register a user because there's no actual database to store our user base in also when you sign in you can literally put in anything and any password and it will log you in as you can see so we're going to change that today so the first thing we want to do is we want to implement a database and we're going to do this by using SQLite 3 which we used before and the reason we're going to do this is for consistency sake now if we're using SQLite 3 for our other database when we do merge these two applications into one application we will not have to make multiple databases so let's make our imports so our first import is the SQLite database or the database back slash SQL and this is our second import which is our SQLite driver now we're doing all this in our data go file just to make things more consistent obviously the data file should have the database in it so the first thing we want to do is we want to be able to save the data that we get from our login form so if we go to where we login you can see here that we have all of our form values here of all the five different fields for our user aside from our unique user ID so we need to make a database that accounts for all of this so let's create a function called save data and inside of it we are just going to pass a user instance which means a pointer to user and this function is not going to return anything so we're going to create a database variable inside of our save data function called DB and this is going to open our database we're going to input our driver name which is SQLite 3 and then we're going to input the name of the database we want to create which is going to be users SQLite 3 next we want to write defer DB close now what this actually does is this will close the database after this function completes running it's not necessary to do this if you guys remember how we implemented our database before we did not use any deferred or even closed the database and usually it's just nice practice to close the database or even if you're just reading from files you want to write defer close at some point it's just general idiomatic go to do this now we need to do a DB exec and we need to put in the SQL that we're going to run so here's our SQL we are going to create a table if the table doesn't already exist and this table is going to be called users it's going to have five fields all of which are going to be of text so notice we're not putting in our unique user ID yet and this is because we haven't generated it we will do this in a later tutorial for now we're just going to create our database with five different fields a first name the last name username email and a password all of them are type of text so now we need to create our TX variable by typing in DB doc begin then we need to prepare our database so we need to actually put in the SQL that we're going to use to prepare it to enter in the values so here is our SQL we're just going to insert into our users table the first name last name username email and password and then we pass a bunch of question marks as our values because we are going to read them in so to read in our values we pass in st MT dot and then we pass each of the values in so okay so now we're reading in all our values and I lied before we actually want to return an error from this actual function so we've opened the database we've created our table and then we have created or we've inserted our variables into the table finally we need to commit these changes to our database by calling TX commit and then we are going to return the error which is er R so this is our saved data function and we can actually go into our main function here and we can call save data on our user here so if we just type in save data and we put in you this should now work this will now create a database with these as the username password etc etc we're going to remove this set session here this is creating a cookie using this user but we don't need it anymore now that we're actually saving everything into a database so now that we've removed that we need to now look at our login function so if we look at our login function as it is currently we read in the values from the login form here then we just make sure they're not empty then we deploy cookie with set session and then we change our redirect to example so now we need to create a function inside of our data that go file that will allow us to compare the username and the password that are being entered in here with what we have in our database so we're going to create a function called user exists' this is going to take in a type of user and it's going to return a boolean type and you'll see why we're returning a boolean later so because we did not declare our database as a global variable in here we need to read eclair it in here so let's declare it like we did above we also want to defer and close it finally we want to create a variable our two variables rather one called PS and one called the US and these will both be of type string now these variables are going to hold our password and username that we actually read from our database so let's query the database and we are going to do it very simply we're going to say select username and password from users where username equals and remember when we actually insert a variable into our into our SQL here we need to surround it with single quotes so we're going to put in a single quote and then we're going to type in you username to concatenate it and we're going to say end password equals and again we're going to make a single quote and type in password are you dot password rather and then we will concatenate this with a quotation mark string so there we go that's our query this will actually select both the username and the password and it will match it with both the username and the password that we can put it through here so to handle our error in this case we're just going to say if error does not equal now return false so if we get an error from this query it's going to return false as our boolean value here now we are going to iterate over the values that we selected from the database so we're gonna say queue next for queue next Q dot scan and we're going to scan in the variables that we get so it goes username and password so we're going to scan in our us and then PS and we need to do this pointers so us and PS and then finally we are going to actually match the two values with our value of user that we bring it so we want to say if u s equals username you dot username and PS equals u dot password return true otherwise return false and there we go so that's our function for verifying if a user exists in our database so let's actually implement this now so if we go into our login here we want this to be inside of our if name is not equal to an empty string and if pass is not equal to an empty string so we're going to say if user exists and we're going to input our user which we will create right here so we didn't put it like this we could create a variable so we could say okay a u equals an instance of user like this and then we could just pass in u here and this would actually be the better way to do things so we say if userexists and then we want to copy and paste this stuff inside of here so if our user exists we deploy a cookie and then we redirect to example otherwise we just redirect back to our index so this is a very simple implementation of authentication and having the cookie is of course extremely important and you'll see in the next tutorial why this is because we want to be able to check while we're in the inside of our example page here if that cookie exists and if that cookie exists then the user will be able to continue working past the authentication so that's actually how authentication normally works for example when you login to Facebook you put in your username and password and they only match you against the database when you put in your username and password usually what happens is facebook will issue you a cookie after you log in and this cookie will only last for a certain period of time and then after the cookie is deleted you'll have to log in again to verify that you are in fact you so that's basically what we've created here so let's run this and take a look at what actually is going on so to run it of course we need to run all three files at once so here's our login first let's try to login so we'll type in test and say password and if we hit submit it won't let us login and we're now getting an error back here so let's sign up so our username will be a capital tensor first name John last name doe email tensor @ tensor com our password will be password and we will submit that and we are getting an error here and that's because we didn't fix things to stop our signup from sending us to our example so we're not actually getting an error if I actually type in tensor here and then I type in password it should log us in but because we didn't have the cookie before we couldn't actually get to the example so let's change that the fix what just happened we need to change the redirect from our signup form so our signup form was redirecting to example we want it to redirect to our login screen so while we're at it also we're going to open up our SQL Lite browser as you can see here we have created a first name and a last name and a username so all the fields here exists now this is a problem of course we do not want to store the actual password inside of our database so we're going to have to create a function to encrypt our password this is of course something we're going to do in our next tutorial so let's rerun everything and make sure it works again okay so now here is our login page let's create a new user and we're gonna call it test user and the first name will just be tester and then user our email will just be test at test comm and our password will be test so let's submit this so now we actually go straight to the login form and we can actually type in test user and test and this should work so as you see here and now it says hello user test user and if we actually go say we go back to our login page without hitting the logout button if we were to type in example again this would send us back to our test user page so we are keeping the user who's logged in name using these cookies which is very cool so if we were to come back here without logging out and we type in tensor with a capital T and then we type in our password it would actually change to ten Sura and this is happening because our cookie is now changing so if we log out now though and we try to go to example it shouldn't work and there we go we have a empty page and we're going to deal with that later when we add a redirect if there is no cookie and one more thing before we finish so let's login again so 10 sir now let's check out the cookie that we actually created so here are our cookies for localhost and the cookie we've created was called session and as you can see here there's this long key here that is being encrypted and this has both of the username and the password inside of it and if we reload our SQLite browser we have two users now in here one test user and the other one 10 sir and it shows their passwords in plain text of course like I said before this is something that we want to get rid of and also another problem that we have with this current implementation is that we could have multiple users with the same name in in this actual database which could be a problem anyway guys I hope you enjoyed this tutorial please feel free to like and subscribe and if you have any questions of course feel free to comment if you dislike the tutorial go ahead and dislike us we will be back after the holidays we will be creating at least two or three tutorials per week for the videos and our written tutorials should start back up after the holidays are finished anyway guys I hope you guys have a good holiday
Original Description
In this tutorial, we implement some very basic user authentication.
Also check out our written Golang tutorial on our blog: http://tensor-programming.com/welcome-to-go/
Check out the Code on Github: https://github.com/tensor-programming/go-tutorial-8
Check out our Twitter: https://twitter.com/TensorProgram
Check out our Facebook: https://www.facebook.com/Tensor-Programming-1197847143611799/
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Tensor Programming · Tensor Programming · 16 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
▶
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
NodeJs, Text editors and IDEs
Tensor Programming
Vanilla JS todo App
Tensor Programming
Elm Tutorial part 1
Tensor Programming
Elm Lang Tutorial, Part 2
Tensor Programming
Elm Tutorial Part 3
Tensor Programming
Elm Tutorial Part 4 -- Analog Clock App
Tensor Programming
Elm Tutorial part 5 -- Snake Game
Tensor Programming
Elm Tutorial part 6 -- Calculator
Tensor Programming
Go Tutorial part 1 -- Hello World and Static File Server
Tensor Programming
Go Tutorial part 2 -- Web Crawler
Tensor Programming
Go Tutorial Part 3 (Web App part 1)
Tensor Programming
Go tutorial Part 4 (Web tutorial part 2) - Using templates
Tensor Programming
Go tutorial part 5 (web app part 3)
Tensor Programming
Go tutorial part 6 (webapp part 4)
Tensor Programming
Go tutorial part 7 (web app part 5)
Tensor Programming
Go tutorial part 8 (Web app part 6)
Tensor Programming
Go tutorial Part 9 (web tutorial part 7)
Tensor Programming
Go tutorial Part 10 (web app part 8)
Tensor Programming
Go tutorial Part 11 (Web app Part 9)
Tensor Programming
Go Tutorial Part 12 (Web app Part 10)
Tensor Programming
Go Tutorial Part 13 (Web app Part 11)
Tensor Programming
Looking at Elm 0.18
Tensor Programming
Go tutorial Part 14 (Web tutorial part 12)
Tensor Programming
Go tutorial Part 15 (Web tutorial part 13)
Tensor Programming
Go tutorial part 16 (web app part 14)
Tensor Programming
Elm Tutorial Part 7 (SPA part 1)
Tensor Programming
Elm Tutorial Part 8 (SPA Part 2)
Tensor Programming
Electron Elm Tutorial
Tensor Programming
Go tutorial part 17 (web app part 15)
Tensor Programming
Up and Coming Programming Languages and Technologies for 2017
Tensor Programming
elixir tutorial part 1
Tensor Programming
elixir tutorial part 2
Tensor Programming
Elixir tutorial Part 3 (GenServer and Supervisor)
Tensor Programming
Elixir Tutorial Part 4 (GenStage)
Tensor Programming
Elixir Tutorial Part 5 (Plug and Cowboy)
Tensor Programming
Phoenix Framework Tutorial Part 1 (elixir part 6)
Tensor Programming
Phoenix Framework Tutorial Part 2 (elixir part 7)
Tensor Programming
Phoenix Framework Tutorial Part 3 (elixir part 8)
Tensor Programming
A Intro to Clojure and Clojure Syntax
Tensor Programming
An Update about the channel
Tensor Programming
Intro to Rustlang (Setup and Primitives)
Tensor Programming
Intro to Rustlang (Strings, Tuples, Arrays, Slices and Pretty Printing)
Tensor Programming
Intro to Rustlang (Ownership and Borrowing)
Tensor Programming
Intro to Rustlang (Structs, Methods, Functions, Related Functions and the Display/Debug Traits)
Tensor Programming
Intro to Rustlang (Control Flow, Conditionals and Pattern Matching)
Tensor Programming
Intro to RustLang (Enums and Options)
Tensor Programming
Intro to Rustlang (Vectors, HashMaps, Casting, If-Let, While-Let, and the Result Enum)
Tensor Programming
Rustlang Project: Snake Game
Tensor Programming
Intro to Rustlang (Traits and Generic Types)
Tensor Programming
Intro to Rust-lang (Closures, the Box Pointer and Iterators)
Tensor Programming
Intro to Rust-lang (Modules and Lifetimes)
Tensor Programming
Intro to Rust-lang (Macros and Metaprogramming)
Tensor Programming
Intro to Rust-lang (Error Handling)
Tensor Programming
Intro to Rust-lang (Concurrency, Threads, Channels, Mutex and Arc)
Tensor Programming
Intro to Rust-lang (Tests, Attributes, Configuration and Conditional compilation)
Tensor Programming
Rustlang Project: Port Sniffer CLI
Tensor Programming
Rustlang Project: Chat Application
Tensor Programming
Rustlang Project: CLI Toy Blockchain
Tensor Programming
Intro to Rust-lang (Setting up a Development Environment)
Tensor Programming
Intro to Rust-lang (Building a Web API with Iron)
Tensor Programming
More on: LLM Foundations
View skill →Related Reads
📰
📰
📰
📰
Corvorum OS 1.0 - Sistema Operativo Tecnomántico
Dev.to · Technomantus Corvi
Why Materials Scientists Are Still Copy-Pasting Data from PDFs in 2026 (And Why AI Changes…
Medium · AI
How to Actually Cap AI Spend for Your Users: 3 Edge Cases Everyone Misses
Dev.to · CJ Cummings
Nano Banana 2 Lite with Kiro
Dev.to · xbill
🎓
Tutor Explanation
DeepCamp AI