项目作者: gatleon

项目描述 :
add authentication to your application - in 1 minute or less.
高级语言: Ruby
项目地址: git://github.com/gatleon/gatleon-authform-rails.git
创建时间: 2020-04-30T05:32:33Z
项目社区:https://github.com/gatleon/gatleon-authform-rails

开源协议:MIT License

下载


gatleon-authform-rails

add authentication to your application - in 1 minute or less.

installation

add this line to your application’s Gemfile:

  1. gem "gatleon-authform-rails"

and then execute:

  1. $ bundle install

open rails credentials:

  1. $ EDITOR=vim rails credentials:edit

set authform credentials:

  1. authform:
  2. public_key: "Available at https://authform.gatleon.com"
  3. secret_key: "Available at https://authform.gatleon.com"

add a profile controller:

  1. class ProfileController < ActionController::Base
  2. include Gatleon::Authform::Rails::Concern.new(Rails.application.credentials.dig(:authform))
  3. before_action :require_login, only: [:index]
  4. def index
  5. erb = <<~ERB
  6. <h1>Profile</h1>
  7. <p style="color: green;">You are signed in. (<a href="/profile/signoff">sign off</a>)</p>
  8. <p><%= current_user._id %> <%= current_user._email %></p>
  9. ERB
  10. render inline: erb
  11. end
  12. def signin
  13. erb = <<~ERB
  14. <p style="color: red;"><%= flash[:error] %></p>
  15. <h1>Sign In</h1>
  16. <form action="<%= signon_url %>" method="POST">
  17. <input type="hidden" name="successPath" value="/profile">
  18. <input type="email" name="email">
  19. <button type="submit">Sign In</button>
  20. </form>
  21. ERB
  22. render inline: erb
  23. end
  24. def signoff
  25. current_user.signoff!
  26. redirect_to(profile_signin_path) and return
  27. end
  28. private
  29. def require_login
  30. unless current_user
  31. flash[:error] = "Sign in, please."
  32. redirect_to(profile_signin_path) and return
  33. end
  34. end
  35. end

add profile routes to routes.rb:

  1. Rails.application.routes.draw do
  2. get "/profile", to: "profile#index", as: :profile
  3. get "/profile/signin", to: "profile#signin", as: :profile_signin
  4. get "/profile/signoff", to: "profile#signoff", as: :profile_signoff
  5. end

that’s it!

license

the gem is available as open source under the terms of the MIT License.