So your working on a web app and you realize you want to allow them have accounts and login and out. To accomplish this, you can use a number of different things, or just use what ever your web framework (ASP.NET MVC, Express, Rails, and etc) of choose does.
Or you can use this thing called “Token Authentication”. Now what it is, is exactly what it sounds like, its just a token you pass between your client (browser) and server to validate your request.
You might be thinking well that sounds okay but what stops someone from capturing your token and pretending to be you. Or what would stops someone from making a fake token ?
JWT (JSON Web Token) stops all those things. It is self contained and can hold a variety of different information. Its structure can be broken down into three parts:
- Header (can be decoded by anyone)
- Contains what algorithm was used to encrypt it
- Payload (can be decoded by anyone)
- The info (user name, id, expiry date and etc) stored inside the token
- Got to be careful with what type of info you put in here
So that is why:
- A person cannot capture your token and infinity use it , since we can specify a expiry date.
- A person cannot make a fake one, since we hash the token with a secret that only lives on the server
A few cool side features of using a JWT is that the server does not need to validate the user by interacting with the data store. Which may be a big deal if you want to have a high performance application. The other cool feature is that since you have reduced your authentication method to a JWT, it gives you a lot more flexibility in what ( maybe a mobile app ) can interact with your API.
Have you every want to modify your car ? Well if you have then you already know the basics of what a extension is. Since you modify something that someone else has made (maybe you also made it too, but thats okay).
OR have you ever been in a situation where, you are working on something. And you thought “maybe I should write a new class to extend this other class, that way I add to the functionality”. You could do that, “could” being the keyword there. OR you could just use Extentions 🙂
Extensions let you add new functionality to existing classes, structs, enumerations, or protocols. All you have to do is extended them.
Your going about your normal programming life. Everything is fine, your using functions and classes and variables, and etc. And you’ll be thinking “I got this programming thing pretty much in the bag”
But then you see “it” what “it” is your not sure. Its something coming out the side of your function … OMG is looks like another function. A function against a function ???
We all know what a function is, it is just piece of code that we can use in different places. It some times takes in varables and spits them out. This is all “normal”, however what if I told you that you could return a function from a function ? Or even add a function as a input value ? Pretty crazy right ?
Some of you out there might be thinking, “why would you need this ? stop making things soo complicated !” Well if you want your applications to run asynchronously, your gonna wanna use closures when ever you do something that takes alot of time.
So the use case would go something like this:You wanna log someone in, and execute something else as soon as it finishs, without freezing up the UI (in a iOS app).
Instead of doing it the standard way of writing the code that needs to be executed right after the method call. You can just pass the login function, another function to execute once done.
The in depth code will come in part two.
Hope you found the theory behind it easy to understand 😅
Warning this only works on Mac OS.
Now before you start thinking your going to make the next WordPress, let me just say we are only going to be making the MVP of a blogging site, that’s right a MVP. What is a MVP you may ask ? It stands for Minimum Viable Product, this thing we are about to build is just a web app that has CRUD functionality. What is CRUD you ask ? CRUD is Create Read Update Destroy.
Don’t be afraid of the terminal, the terminal is your friend for life 🙂 you just got to treat it right.
Up and running:
- Install Home Brew, is manages the software you need to manage all the software, your going to use to make software. Just follow the instructions on here: https://brew.sh/
- Install Bundler, it manages the specific Ruby software libraries for your application. Type the following command “gem install bundler”.
- Install Nokogiri, it gives Ruby the ability to understand HTML and CSS among other things. Type the following command “gem install nokogiri”. This is going to take awhile, chill out and take a break you have been working too hard 🙂
- Install Rails, its the thing that you build on top of, to make your web application. Ruby the language has libraries for doing different things. Rails is just a really big library for making web based applications. As you may have guess just type in “gem install rails”.
Making a Rails App:
- CD into a folder you would like to be in and run the following command “rails new <name of your app here> -T “
- Now if you look into the folder you just ran this command in, you will see a folder with the name of your app. Open this with your favorite text editor, VSCode is always a good choice.
- CD into the newly created folder, and make your database. You can do this by running “rails db:create” and then running “rails db:migrate”. What this is doing is creating a database, and then populating it with boiler plate info.
- Now run “rails s” and go to the URL is gives you. You should see your app, YAY you made your first rails app 🙂 If the URL isn’t working try using http:127.0.0.1/8080 or http:127.0.0.1/3000.
Actually making the Blog:
- Run “rails g scaffold Blog title:string body:text”. What this command will do is create a Blog object, that has a title and a body and can be stored in our database. Rails knows this since “g” is for generate, “scaffold” is for create everything for me, and the rest is specify what type of things you want there to be, in the object you are creating.
- Now you got to let the database know about this blog object you made. To do so simply run “rails db:migrate”
- Finally the moment you have been waiting for, run your app with “rails s” and ta-da you go your self a working rails app 🙂
As you an Angular component has a life cycle, much life us humans. In the beginning we are nothing, we do a bunch in the middle and then we die 🙂 However if you want to call a certain service in your “ngOnInit” to dump some values into a var that appears in your html, your going to have a bad time. Or at least angular is by throwing a lot of errors in your console, I mean it will still work.
To get rid of these errors all you have to do it the following add a “?” at the end of the end of the var that’s in the html, and ta-da errors gone. This works since the “?” tells Angular to chill out there will be a value you there “eventually”. Hmm… but why eventually you may ask ? Its because two functions before “ngOnInit” get called , the very first one being the constructor of the class and the second being the “ngOnChanges” method. So for the execution of the first two methods its asking “WTF where is this thing in the html in the .ts file ?” which makes throw errors.
When you inherit from a class you have full access to it’s variables and methods. However what do you do when you would like to use one its methods, and then add to it ? Well you would try to override the method with your definition. This can be accomplished using the “Virtual” keyword on the function you want to override on the parent class. And then using the “Override” keyword on the function in the child class. After which you would then use the “Super” keyword to call the implementation of method found in the parent class.
A real world application of this is, when you want to override and add to the “BeginPlay” method.
Here we see that the TankPlayerController inherits from the APlayerController that inherits from the Actor class. The Actor class holds the initial implementation of the “BeginPlay” method.
Java Script is not different then other languages ! It’s unique and special in its own way.
- For loops
- While loops
- Do – Then loops
- If – Else Statements
- Switch Statements
- Classes and Objects
- Dynamically Typed (most languages have this)
However it has some very weird and interesting differences:
- It has no truth, it is a truthy language
- You can stuff a function into a variable
- There are more ways then one to do the same thing