Skip to content
This repository has been archived by the owner on Apr 12, 2023. It is now read-only.

Devise integration? #5

Open
fluffyx opened this issue Feb 3, 2020 · 2 comments
Open

Devise integration? #5

fluffyx opened this issue Feb 3, 2020 · 2 comments
Labels
question 🤔 I'd like feedback on something

Comments

@fluffyx
Copy link

fluffyx commented Feb 3, 2020

Any plans to integrate this fantastic piece of work with Devise?

Gems intended for that (omniauth-slack) are pretty terrible.

@tommyschaefer tommyschaefer added the question 🤔 I'd like feedback on something label Feb 5, 2020
@tommyschaefer
Copy link
Member

Thanks so much for checking the gem out! 😄 I didn't have a Devise integration in mind originally, but I will definitely look into it and get back to you!

@fluffyx
Copy link
Author

fluffyx commented Feb 5, 2020

You're awesome for filling this need, and even more awesome for responding so kindly! I'm not a professional programmer and I've never done a Github pull request.

I don't know if this will help, here's how I modified my Rails project so Devise works with your gem.

The "right" way to do it in a gem is probably following the omniauth- gem framework, but alas, I had to get it working in less than an hour and your code helped me do that. So once again, THANK YOU SO MUCH!!

./db/migrate/add_omniauth_to_users.rb

    class AddOmniauthToUsers < ActiveRecord::Migration[6.0]
      def change
        add_column :users, :provider, :string
        add_column :users, :uid, :string
        add_column :users, :name, :string
        add_column :users, :image, :text
      end
    end

./app/models/user.rb

    class User < ApplicationRecord
      def self.from_slack(auth)
        i = auth.identity
        
        user = User.where(provider: :slack, uid: i.unique_id).first_or_create do |user|
          user.password = Devise.friendly_token[0, 20]
        end
        
        user.email = i.email
        user.name = i.name   # assuming the user model has a name
        user.image = i.avatar # assuming the user model has an image
        # If you are using confirmable and the provider(s) you use validate emails, 
        # uncomment the line below to skip the confirmation emails.
        # user.skip_confirmation!
        
        user.save if user.changed?
        
        user
      end
    end

./app/controllers/users/sessions_controller.rb

    class Users::SessionsController < Devise::SessionsController
      # before_action :configure_sign_in_params, only: [:create]
      include SlackSignIn::Authorization
      
      # GET /users/sign_in
      # This should DEFINITELY be somewhere else, but it's where I put it. I used /users/sign_in as the redirect path in Slack
      def new
        if slack_authorization.successful?
          user = User.from_slack(slack_authorization)
          sign_in user
          redirect_to '/'
        else
          super 
        end
      end
    end

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
question 🤔 I'd like feedback on something
Projects
None yet
Development

No branches or pull requests

2 participants