Sha256: bfc550f37b67aa467ad370593660e0ee005d91e6a42e74cff9b57acd8ba0c5b4

Contents?: true

Size: 1.54 KB

Versions: 6

Compression:

Stored size: 1.54 KB

Contents

# authentication token

generate "migration", "add_authentication_token_to_users", "authentication_token:string:index"
rake "db:migrate"

inject_into_file "app/models/user.rb", after: "# authentication_token\n" do
<<-EOF
  before_save :ensure_authentication_token

  def ensure_authentication_token
    if authentication_token.blank?
      self.authentication_token = generate_authentication_token
    end
  end

  def generate_authentication_token
    loop do
      token = Devise.friendly_token
      break token unless User.where(authentication_token: token).first
    end
  end
EOF
end

inject_into_file "app/controllers/application_controller.rb", after: "# authentication_token\n" do
<<-EOF
  before_action :authenticate_user_from_token!

  def authenticate_user_from_token!
    auth_token = params[:auth_token].presence
    user       = auth_token && User.find_by_authentication_token(auth_token.to_s)

    if user
      # Notice we are passing store false, so the user is not
      # actually stored in the session and a token is needed
      # for every request. If you want the token to work as a
      # sign in token, you can simply remove store: false.
      sign_in user, store: false
    end
  end
EOF
end

inject_into_file "app/views/devise/registrations/edit.html.erb", before: "    <%= f.submit 'Update', :class => 'button right' %>" do
<<-EOF
    <fieldset>
      <div class="form-group">
        <%= f.label :authentication_token %>
        <%= f.text_field :authentication_token, class: 'form-control', disabled: true %>
      </div>
    </fieldset>
EOF
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
crosstie-0.0.7 lib/crosstie/templates/authentication_token.rb
crosstie-0.0.6 lib/crosstie/templates/authentication_token.rb
crosstie-0.0.5 lib/crosstie/templates/authentication_token.rb
crosstie-0.0.4 lib/crosstie/templates/authentication_token.rb
crosstie-0.0.3 lib/crosstie/templates/authentication_token.rb
crosstie-0.0.2 lib/crosstie/templates/authentication_token.rb