How to manually generate a JWT with Rails Knock

I used knock, which is a gem that creates jwt, to create an authentication function between ios and Rails. I want to be logged in at the same time as the successful creation of a new user, but the initial flow of new user registration is

① Create a new user with user / create. → Returns user information.

(2) Put the user information in the request body, send a post request to user_token, receive jwt, and log in.

I had to hit the API twice. I want to do this once by making the user / create response jwt.

I did the following.

python


def create
  user = User.new(user_params)
  if user.save
     token = Knock::AuthToken.new payload: { sub: user.id }
     render status: :created, json: token
  else
     render status: :unprocessable_entity, json: { messages: user.errors.messages }
  end
end

You only have to do this once!

Recommended Posts

How to manually generate a JWT with Rails Knock
[How to insert a video in haml with Rails]
How to get started with creating a Rails app
How to get along with Rails
How to automatically generate ER diagram when migrating with Rails6
[Rails] How to use rails console with docker
How to insert a video in Rails
How to rename a model with foreign key constraints in Rails
[rails] How to create a partial template
How to build Rails 6 environment with Docker
[Rails] How to log in with a name by adding a devise name column
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
Rails: How to write a rake task nicely
[Rails] How to write when making a subquery
[Rails] rails new to create a database with PostgreSQL
[Rails] How to create a graph using lazy_high_charts
How to implement a like feature in Rails
How to easily create a pull-down in Rails
How to generate a primary key using @GeneratedValue
[Rails] How to create a Twitter share button
[Rails] How to easily implement numbers with pull-down
How to build API with GraphQL and Rails
[Ruby] How to generate a random alphabet string
[Rails] How to build an environment with Docker
How to make a follow function in Rails
How to automatically generate a constructor in Eclipse
How to write Rails
How to uninstall Rails
How to store data simultaneously in a model associated with a nested form (Rails 6.0.0)
How to make batch processing with Rails + Heroku configuration
How to make a factory with a model with polymorphic association
[Rails] How to search by multiple values ​​with LIKE
[Rails] How to create a signed URL for CloudFront
How to push an app developed with Rails to Github
How to make an almost static page with rails
[Rails] How to deal with URL changes after render
How to write a date comparison search in Rails
How to query Array in jsonb with Rails + postgres
[Rails 6] How to set a background image in Rails [CSS]
[Rails] How to load JavaScript in a specific view
[Docker + Rails] How to deal with Rails server startup failure
[Ruby/Rails] How to generate a password in a regular expression
[Java] How to start a new line with StringBuilder
[Rails] How to install a decorator using gem draper
[Rails] [Docker] Copy and paste is OK! How to build a Rails development environment with Docker
How to deal with errors in Rails s could not find a JavaScript runtime.
[rails] How to post images
[Rails] How to use enum
[Rails] How to install devise
[Rails] How to use enum
How to leave a comment
How to read rails routes
How to use rails join
How to number (number) with html.erb
How to update with activerecord-import
How to terminate rails server
How to write Rails validation
How to write Rails seed
[Rails] How to use validation
[Rails] How to disable turbolinks