dimanche 1 novembre 2015

Rails app - login works on local server, but not on internet

I've been using the Hartl tutorial as a template for a similar-but-different rails app and have finally ploughed through Chapter 8 (logging in and out). I can now create users, login etc on my local server, but can't do the same on the external site. When I try to login an existing user, the site just reloads an empty login page.

When I create a new user on the external server, a 'new user' homepage appears with the details that were submitted moments before. However, it's not possible to log in again later. Those details also don't seem to be saved into the user database on SQLite Browser when they're entered on the external server, so I don't think anything is actually being saved at all.

The Heroku logs don't seem to pick up any problem. Below is an excerpt, for when I try to log in a user already saved in the table (I've starred out my brother's email address). As above, what happens is simply a reloading of the login page.

Below that, I've put the relevant view, model and controller info. Please let me know if you need any more and I'll gladly put it up. What's clear is that the local server is able to connect to my users table - my external server is not doing so. Any advice on resolving this - or pointing me in the direction of a similar question - will be much appreciated! I'm hoping this is v simple, but I can't see a similar question anywhere. I really am pretty new to this but v keen for help - thanks in advance.

Heroku Logs

  2015-11-01T17:13:00.157216+00:00 heroku[router]: at=info method=GET path="/playerlogin" host=http://ift.tt/1Q3PgeG request_id=5dce7bf2-d9a6-45c4-9f05-409e6e074192 fwd="88.105.85.62" dyno=web.1 connect=0ms service=568ms status=200 bytes=3602
2015-11-01T17:13:00.152200+00:00 app[web.1]:   Rendered layouts/_header.html.erb (3.7ms)
2015-11-01T17:13:00.144200+00:00 app[web.1]:   Rendered sessions/new.html.erb within layouts/application (448.2ms)
2015-11-01T17:13:00.147941+00:00 app[web.1]:   Rendered layouts/_shim.html.erb (0.4ms)
2015-11-01T17:13:00.153986+00:00 app[web.1]:   Rendered layouts/_footer.html.erb (0.7ms)
2015-11-01T17:13:00.153989+00:00 app[web.1]: Completed 200 OK in 469ms (Views: 459.4ms | ActiveRecord: 0.0ms)
2015-11-01T17:13:10.365075+00:00 app[web.1]: Started POST "/playerlogin" for 88.105.85.62 at 2015-11-01 17:13:10 +0000
2015-11-01T17:13:10.370046+00:00 app[web.1]: Processing by SessionsController#create as HTML
2015-11-01T17:13:10.370097+00:00 app[web.1]:   Parameters: {"utf8"=>"✓", "authenticity_token"=>"G+0dqEQYLbJqnrFxZioNX/9SodHYq15Dk6hV8UKh0oTXiQ77FuvQtABlrQKs0zkkdMRAUt6SpzV3EUNefClGCg==", "session"=>{"email"=>"***@gmail.com", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Log in"}
2015-11-01T17:13:10.403492+00:00 app[web.1]:   User Load (2.4ms)  SELECT  "users".* FROM "users" WHERE "users"."email" = $1 LIMIT 1  [["email", "***@gmail.com"]]
2015-11-01T17:13:10.922751+00:00 heroku[router]: at=info method=POST path="/playerlogin" host=http://ift.tt/1Q3PgeG request_id=7157d677-8039-4d17-a158-4a67ca4da9a0 fwd="88.105.85.62" dyno=web.1 connect=0ms service=566ms status=200 bytes=3603
2015-11-01T17:13:10.917919+00:00 app[web.1]:   Rendered sessions/new.html.erb within layouts/application (512.7ms)
2015-11-01T17:13:10.919288+00:00 app[web.1]:   Rendered layouts/_header.html.erb (0.4ms)
2015-11-01T17:13:10.920107+00:00 app[web.1]: Completed 200 OK in 550ms (Views: 515.7ms | ActiveRecord: 20.9ms)
2015-11-01T17:13:10.918711+00:00 app[web.1]:   Rendered layouts/_shim.html.erb (0.1ms)
2015-11-01T17:13:10.919663+00:00 app[web.1]:   Rendered layouts/_footer.html.erb (0.2ms)
Derek-Breslins-MacBook:predictor_world derekbreslin$ 

The view page - views/sessions/new.html.erb

<% provide(:title, "Log in") %>
<h1>Log in</h1>

<div class="row">
  <div class="col-md-6 col-md-offset-3">
    <%= form_for(:session, url: playerlogin_path) do |f| %>

      <%= f.label :email %>
      <%= f.email_field :email, class: 'form-control' %>

      <%= f.label :password %>
      <%= f.password_field :password, class: 'form-control' %>

      <%= f.label :remember_me, class: "checkbox inline" do %>
        <%= f.check_box :remember_me %>
        <span>Remember me on this computer</span>
      <% end %>

      <%= f.submit "Log in", class: "btn btn-primary" %>
    <% end %>

    <p>New user? <%= link_to "Sign up now!", createteam_path %></p>
  </div>
</div>

Sessions Controller - controllers/sessions_controller.rb

class SessionsController < ApplicationController
  def new
  end

  def create
    user = User.find_by(email: params[:session][:email].downcase)
    if user && user.authenticate(params[:session][:password])
      log_in user
      params[:session][:remember_me] == '1' ? remember(user) : forget(user)
      remember user
      redirect_to user
    else
      flash.now[:danger] = 'Invalid email/password combination'       
      render 'new'
    end
  end

  def destroy
    log_out if logged_in?
    redirect_to root_url
  end
end

Users Model - app/models/user.rb

class User < ActiveRecord::Base
    attr_accessor :remember_token
    before_save { self.email = email.downcase }
    before_save { self.teamname = teamname.titleize }
    validates :name, presence: true, length: {maximum: 25 }
    VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
    validates :email, presence: true, length: {maximum: 255 },
                      format: { with: VALID_EMAIL_REGEX },
                      uniqueness: { case_sensitive: false }
    validates :teamname, presence: true, length: {maximum: 25 },
                      uniqueness: { case_sensitive: false }
    has_secure_password
    validates :password, presence: true, length: { minimum: 6 }
    validates :league, presence: true, length: {maximum: 25 }

      # Returns the hash digest of the given string.
  def User.digest(string)
    cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
                                                  BCrypt::Engine.cost
    BCrypt::Password.create(string, cost: cost)
  end

  #Returns a random token
  def User.new_token
    SecureRandom.urlsafe_base64
  end

    # Remembers a user in the database for use in persistent sessions.
  def remember
    self.remember_token = User.new_token
    update_attribute(:remember_digest, User.digest(remember_token))
  end

    # Returns true if the given token matches the digest.
  def authenticated?(remember_token)
    return false if remember_digest.nil?
    BCrypt::Password.new(remember_digest).is_password?(remember_token)
  end

    # Forgets a user.
  def forget
    update_attribute(:remember_digest, nil)
  end
end

Aucun commentaire:

Enregistrer un commentaire