From 5a36f7b799fa452b1affabda481876be943e95a4 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Thu, 14 Feb 2019 14:23:47 -0600 Subject: [PATCH] Add Generate A Signed JWT Token as a ruby til --- README.md | 3 ++- ruby/generate-a-signed-jwt-token.md | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 ruby/generate-a-signed-jwt-token.md diff --git a/README.md b/README.md index b988c7f..bd9d994 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/). For a steady stream of TILs from a variety of rocketeers, checkout [til.hashrocket.com](https://til.hashrocket.com/). -_763 TILs and counting..._ +_764 TILs and counting..._ --- @@ -601,6 +601,7 @@ _763 TILs and counting..._ - [FactoryGirl Sequences](ruby/factory-girl-sequences.md) - [Fail](ruby/fail.md) - [Finding The Source of Ruby Methods](ruby/finding-the-source-of-ruby-methods.md) +- [Generate A Signed JWT Token](ruby/generate-a-signed-jwt-token.md) - [Generate Ruby Version And Gemset Files With RVM](ruby/generate-ruby-version-and-gemset-files-with-rvm.md) - [Identify Outdated Gems](ruby/identify-outdated-gems.md) - [If You Detect None](ruby/if-you-detect-none.md) diff --git a/ruby/generate-a-signed-jwt-token.md b/ruby/generate-a-signed-jwt-token.md new file mode 100644 index 0000000..a801ddd --- /dev/null +++ b/ruby/generate-a-signed-jwt-token.md @@ -0,0 +1,19 @@ +# Generate A Signed JWT Token + +The [`jwt`](https://github.com/jwt/ruby-jwt) gem is a Ruby library for +encoding and decoding JWT tokens. You can create a signed JWT with the +`#encode` method by specifying a secret and a hash algorithm. + +```ruby +payload = { id: 1, email: 'user@example.com' } +secret = Rails.application.credentials.secret_key_base + +token = JWT.encode(payload, secret, 'HS256') +``` + +This will create a JWT token that contains some JWT headers, application +data, and an encrypted secret that signs the data. This can be passed to and +from your client app as a way of identifying and authenticating a user. + +See the [`jwt-ruby` docs](https://github.com/jwt/ruby-jwt) or +[jwt.io](https://jwt.io/) for more details.