Thursday, 27 June 2019

BlogApplicationProject




Mini Facebook App

1. How to add Bootstrap 4 to a Rails 5 app :-

1) Add bootstrap to your gem file :- gem ‘bootstrap’
2) type bundle install then hit the enter key
3) restart your server.
4) Import bootstrap styles in app/assests/stylesheets/application.scss:=> @import “bootstrap” make sure the file has .scss extension
5)
6) All we need to do here is remove all the lines in our newly renamed application.scss file that start with *=require:

7)
it’s always good practice to run bundle install and restart the Rails server after adding a gem to the Gemfile, so it’s a good time to do both of those things now.

8)
9) create a blog application with the help of rails guide

10) How To use User Authentication in Rails=> 

1) Open up your Gemfile and add this line
gem 'devise'
2) and run
bundle install
3) Configure Devise
Ensure you have defined default url options in your environments files. Open up config/environments/development.rb and add this line:
   config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
before the end keyword.
Open up app/views/layouts/application.html.erb and add:
<% if notice %>
  <p class="alert alert-success"><%= notice %></p>
<% end %>
<% if alert %>
  <p class="alert alert-danger"><%= alert %></p>
<% end %>
right above
   <%= yield %>
Open up app/views/ideas/show.html.erb and remove the line that says:
<p id="notice"><%= notice %></p>
Do the same for app/views/comments/show.html.erb. These lines are not necessary as we’ve put the notice in the app/views/layouts/application.html.erb file.
4) Setup the User model
We’ll use a bundled generator script to create the User model.
   rails g devise user
   rails db:migrate
 5) Add sign-up and login links
In order to do that, edit app/views/layouts/application.html.erb add:
<p class="navbar-text pull-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>
right after
<ul class="nav">
  <li class="active"><a href="/ideas">Ideas</a></li>
</ul>
6) Open up app/controllers/application_controller.rb and add:
  before_action :authenticate_user!
after protect_from_forgery with: :exception.
7) rails generate devise:views



1) Add to your Gemfile:
     gem 'omniauth-facebook' Then bundle install.
2) Add your provider to the App
        create a new file under config/initializers called omniauth.rb and paste the following code into the file(omniauth.rb)
         Rails.application.config.middleware.use OmniAuth::Builder do
         provider :facebook, '584175165442247', '0dbe06c52291a17b450eb7ccd7fd2373'
         # provider :facebook, '433592010474964', '547da79dec038a932f4c846986398f63'
           provider :linkedin, '81q662zzjusm67', 'xZyEHlGAaZI8Gm9q' end
3) Creating the login Page
      rails generate controller sessions new create failure
4) Creating the Authentication Model
     rails generate model Authentication provider:string uid:string user_id:integer token:string
     rake db:migrate
5) Add the following code to your app/models/user.rb file
                 has_many :comments, dependent: :destroy
                  validates :title, presence: true, length: {minimum: 5}
6) Next to your app/models/authentication.rb file, add
               belongs_to :user
               validates :provider, :uid, :presence=> true
7) Add a bit of logic to our sessions controller (app/controllers/sessions_controller.rb)
           class SessionsController < ApplicationController
               skip_before_action :authenticate_user!
               def create
                          user = Authentication.from_omniauth(request.env["omniauth.auth"])
                 if user
                          flash[:notice] = "Authentication successful."
                          sign_in(user)
                          sign_in_and_redirect(user)
                else
                        flash[:notice] = "Authentication Failed."
                end
                end
         end
8) Next, to your app/models/authentication.rb file
          def self.from_omniauth(auth)
                  authenticate = where(provider: auth['provider'], :uid=>auth['uid']).first_or_initialize
                  register_user = User.find_by(email: auth.info.email)
                  if authenticate.user
                           return authenticate.user
                  elsif register_user
                          register_user.authentications.create(provider: auth['provider'], :uid=>auth['uid'])
                  return register_user
                  else
                       user = User.new(
                       email: auth.info.try(:email),
                       password: Devise.friendly_token.first(8)
                     )
                    if user.email.blank?
                             user.email=auth.extra.raw_info.id.to_s+"@gmail.com"
                   end
                   user.save!(:validate => false)
                          user.authentications.create(provider: auth['provider'], :uid=>auth['uid'],token: auth["credentials"] ["token"])
                          return user
                       end
                     end
9) In config/routes.rb
            match '/auth/:provider/callback', :to => 'sessions#create', via: [:get, :post]
            match '/auth/failure', :to => 'sessions#failure', via: [:get, :post]
10) In Views
            <%= link_to "facebook", "/auth/facebook", class: "btn btn-primary"%>
            <%= link_to "linkedin", "/auth/linkedin", class: "btn btn-primary"%>
11)
gem 'thin'
bundle install start server on https mode its specially used for facebook login
server start on https mode cmd => thin start –ssl

12) How to find provider: ‘your_app_id’, ‘your_app_secret’


1) Facebook => firstly you create a facebook app from this url(https://developer.facebook.com)
Go to the Setting/Basic option copy the app id and app secret and paste into the omniauth.rb
Linkedin => firstly you create a linkedin app from this url(https://developer.linkedin.com)
Go to the Auth menu and copy a Client id and client secret and paste into omaniauth.rb file.
Google => firstly you create a google app from this url(https://developer.google.com)
create a project (BlogApp)
→ next, go to the Oauth consent screen and give a application name next, support email and click to save button
→ go to Credentials menu and select a Oauth client id and checked a web application option
     and authorized redirect url and after this click create button.




  Common Git Commands   Take clone of a repository git clone <remote-repository-url> ex: git clone  https://github.com/agricor/RegTech...