jwt token verify code example

Example 1: how to check if string is valid jwt

function validateResourceName(string) {
 var pattern = /(\w)*\\(?!\\)(\w)*\\(?!\\)(\w)*(?!\\)/g;
  if (!pattern.test(string)) {
    alert("not a match");
  } else {
    alert("match");
  }

}
if(JWS_REGEX.test('abc')){
console.log('valid token')
} else {
    console.log('not valid');
}

Example 2: jwt encode

jwt.encode( { 'client_id':'value', 'expires_in':'datetime'}, SECRET_KEY, algorithm='HS256' )

OBS:
Convert datetime to string because in the backend is a json encode system 
and it will generate a TypeError
ex: TypeError: Object of type datetime is not JSON serializable

Example 3: jwt decode

jwt.decode( token, SECRET_KEY, algorithm='HS256' )

Tags:

Php Example