README.md in omniauth-feishu-0.1.0 vs README.md in omniauth-feishu-0.1.2

- old
+ new

@@ -1,10 +1,11 @@ -# Omniauth::Feishu +# OmniAuth Feishu -Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/omniauth/feishu`. To experiment with that code, run `bin/console` for an interactive prompt. +Strategy to authenticate with Feishu via OAuth2 in OmniAuth. -TODO: Delete this and the text above, and describe your gem +[![Gem Version](https://badge.fury.io/rb/omniauth-feishu.svg)](http://badge.fury.io/rb/omniauth-feishu) +[![Build Status](https://travis-ci.org/renny-ren/omniauth-feishu.svg?branch=master)](https://travis-ci.org/renny-ren/omniauth-feishu) ## Installation Add this line to your application's Gemfile: @@ -18,19 +19,69 @@ Or install it yourself as: $ gem install omniauth-feishu +## Before You Begin + +You should have already created your app in feishu platform, if not, go to https://open.feishu.cn/app/ to create one. + +Take note of your App Id and App Secret because that is what your web application will use to authenticate against the Feishu API. +Make sure to set a redirect URL or else you may get authentication error. + ## Usage -TODO: Write usage instructions here +Here's an example for adding the middleware to a Rails app in `config/initializers/omniauth.rb`: -## Development +```ruby +Rails.application.config.middleware.use OmniAuth::Builder do + provider :feishu, 'App ID', 'App Secret' +end +``` -After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment. +## Devise Usage +Adapted from [Devise OmniAuth Instructions](https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview) -To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org). +```ruby +# app/models/user.rb +class User < ApplicationRecord + #... + devise :omniauthable, omniauth_providers: %i[feishu] + #... +end +# config/initializers/devise.rb +config.omniauth :feishu, 'App ID', 'App Secret' + +# Below controller assumes callback route configuration following +# in config/routes.rb +Devise.setup do |config| + # ... + devise_for :users, controllers: { omniauth_callbacks: 'users/omniauth_callbacks' } +end + +# app/controllers/users/omniauth_callbacks_controller.rb +class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController + def feishu + @user = User.from_omniauth(request.env["omniauth.auth"]) + if @user.persisted? + sign_in_and_redirect @user, event: :authentication + else + session["devise.feishu"] = request.env["omniauth.auth"] + redirect_to new_user_registration_url + end + end + + def failure + redirect_to root_path + end +end +``` + + ## Contributing -Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/omniauth-feishu. +Bug reports and pull requests are welcome on GitHub at https://github.com/renny-ren/omniauth-feishu. +## License + +The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).