Open Laboratory for Technocrats

Grab the developer role, learn concepts & prepare with senior software engineers to get solutions for problems in a software job. Coding and Programming Solutions crafted for you.

JWT - How to log out when using JSON web token?

 

JWT 
Full form of JWT is JSON web token

As per autho0.com

JSON Web Token (JWT) is an open standard (RFC 7519) that defines a compact and self-contained way for securely transmitting information between parties as a JSON object. This information can be verified and trusted because it is digitally signed. JWTs can be signed using a secret (with HMAC algorithm) or a public/private key pair using RSA.
Coming to the original question: How to log out when using JSON web token?
The simple answer is We Can't

Now let's see why we can't do it in the case of JWT.

JWT is stateless
Flow


This means JWT tokens are not stored in the database or sessions. So if a token is generated it can be used till it gets expired and how it does we can specify the time to expire at creation time.

So next is how should we plan this to expiry the token?

We need to plan in the following manner:

  • 1. Set a reasonable expiration time for tokens
  • 2. Delete the stored token from the client-side on logout
  • 3. Store no longer active tokens that still have some time to live

    No logout on JWT


PHP’s UCWords & UCFirst Functions In JavaScript

UCWords & UCFirst Functions In PHP Replication in JS

JavaScript is also having various functionalities regarding string manipulation, But the PHP developers who are working new to it faces a lot of need of the same functionality as they do it in PHP functions.

Today lets discuss the two very common functions ucfirst function in php and ucwords function in php that every developer who are using PHP uses.

ucwords() :

Returns a string with the first character of each word in string capitalized, if that character is alphabetic.

Now let's see how we can implement this same functionality in core javascript, without using any framework.

String.prototype.jsUCWords = function () {
    return this.replace(/\w+/g, function(a){
        return a.charAt(0).toUpperCase() + a.slice(1).toLowerCase()
        })
    }
Example and Usage:

var stringVariable = "This is my first js test";
stringVariable.jsUCWords();
 // Output: This Is My First Js Test


Similarly now lets have a look into next function called ucfirst():

Returns a string with the first character of str capitalized, if that character is alphabetic. 
Now lets see how we can implement this same functionality in core javascript,without using any framework.
String.prototype.jsUCFirst = function () {    
    return this.charAt(0).toUpperCase() + this.slice(1).toLowerCase()  
}
 Example and Usage:

var stringVariable = "this is my first js test";
stringVariable.jsUCFirst();
 // Output: This is my first js test

These are two very common and easy to use function in PHP which we have implemented using plain javascript.

Happy Coding :)


Parent vs Closest - JavaScript

When we usually work with javascript then we use the searching with the help of closest or parent or parents.

Lets discuss something about them as:

closest() : 
* Begins with the current element
* Traverses up the DOM tree and if found the first selector stops there
parent() : 
* Begins with the current element
* Traverses up to just parent in DOM tree and if found the selector stops there
parents() : 
* Begins with the current element
* Traverses up to complete DOM tree and found all selectors and returns all the set

Here are some speed results from jsperf



Top #3 Articles

Most Read