Skip to content
The simplest way to add passwordless authentication to your application
Ruby HTML
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
assets/images
lib
views
.gitignore
Gemfile
Gemfile.lock
LICENSE
README.md
kno.gemspec
main.rb

README.md

Kno Ruby

Gem Version

Go Passwordless with trykno.com

Use the Kno service for passwordless authentication, it handles sending emails so you don't have to. It also allows users to set up device-based authentication so they don't have to wait for any email (after the first).

Installation

Add kno as a dependency in your Gemfile:

gem 'kno'

Then, install it using bundler:

$ bundle install

Usage

This library integrates Kno into any Rack-based application, including Rails, Hanami and Sinatra.

Rails integration

Configure middleware

Add Kno::Session to the middleware stack, by configuring it in /my_app/config/application.rb

module MyApp
  class Application < Rails::Application
    # ...
    config.load_defaults 6.0

    config.middleware.use Kno::Session, sign_in_redirect: "/"
  end
end

Make sure to add Kno::Session after the default middleware as it requires the session middleware to be applied.

Add sign in/out button

Use the helpers that Kno added to the request to show the correct sign in, or sign out, button.

<%= request.env['kno'].session_button().html_safe %>

Check the user is authenticated

Controllers can check if a user is authenticated by looking up their persona_id.

persona_id = request.env['kno'].persona_id

With Kno, users are uniquely identified by their persona_id. If the request is unauthenticated, then the value will be nil.

Local development

Authentication is now setup for local development. Run locally and click the sign in button and you should see a sign in modal.

Local development screenshot

Enter your real email address. Kno runs a service for local development that sends a limited number of emails.

Get production keys

To use Kno in production you will need site and API tokens for your application.

Create an account at trykno.com and follow the guidance to create your first space. This will direct you to create a site_token and api_token. Add these to your environment and edit the middleware configuration.

config.middleware.use Kno::Session,
  sign_in_redirect: "/"
  site_token: ENV["KNO_SITE_TOKEN"],
  api_token: ENV["KNO_API_TOKEN"]

NOTE: The tokens do not have to be stored in the environment. However the api token MUST be kept secure and should not be committed to your applications source code.

Contributing

Contributions are very welcome. Please do open an issue or pull request or reach out to us at team@trykno.com

Docker

If you do not have node installed, you can run locally in Docker with the following command.

docker run \
  -it \
  --rm \
  -w="/opt/app" \
  -v="$(pwd):/opt/app" \
  --env="PORT=3000" \
  -p="3000:3000" \
  --network="host" \
  ruby:2.7.0 bash

NOTE: You will need to bundle install every time you start a container with this command

You can’t perform that action at this time.