Skip to content

debmhteach/CS3300-Devise-User-Authentication

Repository files navigation

Rails - Devise User Authentication

Here you will set up a user login.

Go to your Portfolio project docker environment and cd to your portfolio project directory.

Step 1 - Installing and Configuring Devise

  1. Add the devise gem to your Gemfile after gem "rails"
    gem "rails" , "~> 6.1.7"
    gem "devise"
  2. Refresh/ install all the gems in your project
    bundle install
  3. Set up devise in your project by running the generator command to create all the necessary logic and views associated with devise
    bundle exec rails g devise:install
  4. Open config/initializers/devise.rb and add the following line of code. Make sure that the code is within the devise setup loop. Your code should look something like the following. The line of code should be added somewhere around line 19.
Devise.setup do |config|
    # ... comments...

    config.navigational_formats = ['*/*', :html, :turbo_stream]

    # ... comments...
end
  1. Run the following generator command to generate the devise user model
    bundle exec rails g devise user
  2. Confirm that the following code exists within your config/routes.rb file above the code for root "home#index if it does not exist, make sure to add it.
     devise_for :users
     root "projects#index"
     resources :projects
  1. To view all the available routes, we can run the following command and get an output in the command line of all possible routes that we can provide to the user
    bundle exec rails routes

  2. Make all the database migrations and place the user's table in the database
    bundle exec rake db:migrate

  3. Generate the views associated with devise by running the following command. rails generate devise:views users

  4. In /config/initializer/devise.rb, Uncomment the following around line 255 # config.scoped_views = false change false to true config.scoped_views = true

  5. Start rails and go to http://localhost:3000/users/sign_in to see the login page

  6. Open app/controllers/projects_controller.rb and update the following

class ProjectsController < ApplicationController
  before_action :set_project, only: %i[ show edit update destroy ]
  before_action :authenticate_user!, only: %i[ new edit create update destroy]
  1. Open app/views/layouts/application.html.erb and add the following code inside the HTML body tags.
# Your code should look something like this
...
  <body>
  <p class="navbar-text float-right">
  <% if user_signed_in? %>
    Logged in as <strong><%= current_user.email %></strong>.
    <%= link_to 'Edit profile', edit_user_registration_path, :class => 'navbar-link' %> |
    <%= link_to "Logout", destroy_user_session_path, method: :delete, :class => 'navbar-link'  %>
  <% else %>
    <%= link_to "Sign up", new_user_registration_path, :class => 'navbar-link'  %> |
    <%= link_to "Login", new_user_session_path, :class => 'navbar-link'  %>
  <% end %>
  </p>
  <p class="notice"><%= notice %></p> 
  <p class="alert"><%= alert %></p> 
    <%= yield %>
  </body>
  1. View the changes that you made by restarting your server and navigating to http://localhost:3000/ and test that you can't create new, edit update or destroy

  2. Open the app/views/projects/index.html.erb file and update to only show edit destroy new buttons buttons if logged in. Here is the code to determine if logged in

        <% if user_signed_in? %>

        <% end %>  
  1. Test the changes that you made for not displaying the buttons

CircleCI Setup

Continuous Integration Continuous Delivery Pipeline

Step 1 - Configuring circleCI in the project

  1. In your application's directory, create a new directory with name .circleci (including the period at the beginning of the name)
    mkdir .circleci
  2. Create a new file inside the directory titled config.yml
    touch .circleci/config.yml
  3. Copy and paste the following contentment from below into the config.yml file
# Use the latest 2.1 version of CircleCI pipeline process engine.
# See: https://circleci.com/docs/2.0/configuration-reference
version: 2.1
jobs:
  build:
    docker:
      - image: 'tghastings/code-esaas'
    steps:
      - checkout
      - run:
          name: rspec
          command: sed -i 's/ruby "[0-9].[0-9].[0-9]"/ruby "3.0.2"/' Gemfile && bundle install && bundle exec rake db:migrate RAILS_ENV=test && bundle exec rspec
# Invoke jobs via workflows
# See: https://circleci.com/docs/2.0/configuration-reference/#workflows
workflows:
  sample: # This is the name of the workflow, feel free to change it to better match your workflow.
    # Inside the workflow, you define the jobs you want to run.
    jobs:
      - build
  1. You will need to push your code to Github

Step 3 - Setting up a CircleCI account

  1. Go to https://circleci.com and Sign-in with GitHub
    1. Accept the Authorization of Circle CI (you'll be re-directed to GitHub and then back to CircleCI)
  2. Select the repository for your portfolio project (this should be listed in CircleCI's interface)
    alt-text
    1. You may see the CI/CD pipeline in action and start to run your tests (it should look something like the image below)
      alt-textx
  3. Push your code with unit tests to GitHub
  4. Verify that you can see your tests passing on https://app.circleci.com/pipelines/github/<GITHUB_USERNAME>/<PROJECT_NAME>
    1. alt-image
  5. Make changes to the code in your project.rb file so that the tests will fail Do this by commenting out validate_presence_of
class Project < ApplicationRecord
    # require title and description
    #validates_presence_of :title, :description
end
  1. Push your modified code back to GitHub and navigate back to your project in CircleCI https://app.circleci.com/pipelines/github/<GITHUB_USERNAME>/<PROJECT_NAME>
  2. See that your tests failed alt-text
  3. Click on the small triangle left of the red FAILED icon.
  4. Click on the blue build link.
  5. Expand the rspec box to view the test output. alt-text
  6. Revert the changes you just made in the project.rb file and uncomment the validates_presence_of line
class Project < ApplicationRecord
     # require title and description
    validates_presence_of :title, :description
end
  1. Push your code back to GitHub and re-check CircleCI to ensure that all the tests pass again

Note: If your tests are not all passing and CircleCI does not show "Successful" this is OK

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published