All Projects → mezbahalam → devise_cancan

mezbahalam / devise_cancan

Licence: other
Tutorial: How to use gem 'devise' and gem 'cancancan'

Programming Languages

ruby
36898 projects - #4 most used programming language
HTML
75241 projects
CSS
56736 projects
javascript
184084 projects - #8 most used programming language
coffeescript
4710 projects

Projects that are alternatives of or similar to devise cancan

Furatto Rails Start Kit
A rails app with Furatto, Devise, and Facebook Authentication perfect for hackathons!
Stars: ✭ 46 (+187.5%)
Mutual labels:  devise
Limestone
Boilerplate Rails 6 SaaS application with Webpack, Stimulus and Docker integration.
Stars: ✭ 191 (+1093.75%)
Mutual labels:  devise
acts as user
A gem which handles multiple types of users on a rails app
Stars: ✭ 24 (+50%)
Mutual labels:  devise
Graphql devise
GraphQL interface on top devise_token_auth
Stars: ✭ 100 (+525%)
Mutual labels:  devise
Doorkeeper Provider App
An example OAuth 2 provider application using the Doorkeeper gem, Rails and Devise
Stars: ✭ 146 (+812.5%)
Mutual labels:  devise
Devise invitable
An invitation strategy for devise
Stars: ✭ 2,491 (+15468.75%)
Mutual labels:  devise
Devise
Flexible authentication solution for Rails with Warden.
Stars: ✭ 22,088 (+137950%)
Mutual labels:  devise
rails 6 devise example
rails 6 with devise + bootstrap + github oauth
Stars: ✭ 65 (+306.25%)
Mutual labels:  devise
Passport
Provides authentication for phoenix application
Stars: ✭ 159 (+893.75%)
Mutual labels:  devise
react-devise-token-auth-sample
React on Rails using devise_token_auth for authentication
Stars: ✭ 25 (+56.25%)
Mutual labels:  devise
Simple token authentication
Simple (but safe) token authentication for Rails apps or API with Devise.
Stars: ✭ 1,474 (+9112.5%)
Mutual labels:  devise
Base App
An app to help jumpstart a new Rails 4 app. Features Ruby 2.0, PostgreSQL, jQuery, RSpec, Cucumber, user and admin system built with Devise, Facebook login.
Stars: ✭ 127 (+693.75%)
Mutual labels:  devise
devise-uncommon password
Devise extension to prevent users from using a common password.
Stars: ✭ 24 (+50%)
Mutual labels:  devise
Instuigram
🎓 Learning Ruby on Rails through building the Instagram Application.
Stars: ✭ 88 (+450%)
Mutual labels:  devise
shotgun
Ready to go Rails App with TailwindCSS, ViewComponent, Devise, and more!
Stars: ✭ 25 (+56.25%)
Mutual labels:  devise
Devise Jwt
JWT token authentication with devise and rails
Stars: ✭ 881 (+5406.25%)
Mutual labels:  devise
Rails Devise Graphql
A Rails 6 boilerplate to create your next Saas product. Preloaded with graphQL, devise, JWT, CanCanCan, RailsAdmin, Rubocop, Rspec, i18n and more.
Stars: ✭ 199 (+1143.75%)
Mutual labels:  devise
active admin role
Role based authorization with CanCanCan for Active Admin
Stars: ✭ 53 (+231.25%)
Mutual labels:  cancancan
Devise-Omniauth-Multiple-Providers
Devise Multiple Omniauth Providers
Stars: ✭ 34 (+112.5%)
Mutual labels:  devise
graphql authorize
Authorization helpers for ruby-graphql fields
Stars: ✭ 23 (+43.75%)
Mutual labels:  cancancan

How to use devise and cancancan gem instraction:

Get video tutorial:

IMAGE ALT TEXT HERE

First Step
create a new rails app

terminal

$ rails new devise_cancan

$ rails g scaffold status post:text

add gem to gemfile

\Gemfile

gem 'devise'
gem 'cancancan'
Second Step
setup devise

terminal

$ rails generate devise:install

\config\environments\development.rb

 config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }

\config\routes.rb

 root 'statuses#index'

terminal

$ rails generate devise User

$ rake db:migrate

$ rails generate devise:views

\app\controllers\statuses_controller.rb

 before_action :authenticate_user!

\app\views\layouts\application.html.erb

<% if user_signed_in? %>
    <%= link_to "sign out",destroy_user_session_path, method: :delete   %>
<% end %>
<br>
Third step
setup cancancan abilities:

terminal

$ rails g cancan:ability

$ rails generate migration add_roles_mask_to_users roles_mask:integer

$ rake db:migrate

\app\models\user.rb

ROLES = %i[admin author]

def roles=(roles)
  roles = [*roles].map { |r| r.to_sym }
  self.roles_mask = (roles & ROLES).map { |r| 2**ROLES.index(r) }.inject(0, :+)
end

def roles
  ROLES.reject do |r|
    ((roles_mask.to_i || 0) & 2**ROLES.index(r)).zero?
  end
end

def has_role?(role)
  roles.include?(role)
end

\app\controller\application_controller.rb

before_action :configure_permitted_parameters, if: :devise_controller?
protected

def configure_permitted_parameters
  devise_parameter_sanitizer.for(:sign_up)  { |u| u.permit(  :email,:password, :password_confirmation, roles: []) }
end

\app\views\devise\registrations\new.html.erb

<% for role in User::ROLES %>
 <%= check_box_tag "user[roles][#{role}]", role, @user.roles.include?(role), {:name => "user[roles][]"}%>
 <%= label_tag "user_roles_#{role}", role.to_s.humanize %><br />
<% end %>
<%= hidden_field_tag "user[roles][]", "" %>

\app\models\ability.rb

  user ||= User.new
  if user.has_role? :admin
    can :manage, :all
  elsif user.has_role? :author
    can :create, Status # author can create status
    can :update, Status # author can update status
    # can :destroy, Status # #uncomment this line, author can destroy status 
    can :read, :all
  else
    can :read, :all
  end

\app\controllers\statuses_controller.rb

 load_and_authorize_resource

Now Test It !!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].