abhinav.x.tiwari
Joined 6/11/2017
abhinav.x.tiwari said over 7 years ago on Rails API - Authentication with JWT :

There is no error, this"if condition" always returns false, so i am not able to load the components:

 <% if user_signed_in? %>
                                    <li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">Account
                                        <b class="caret"></b></a>
                                        <ul class="dropdown-menu">
                                            <li>
                                                <div class="navbar-content">
                                                    <div class="row">
                                                        <div class="col-md-7">
                                                            <span>User Name</span>
                                                            
                                                                <%= current_user.email %>
                                                            <div class="divider">
                                                            </div>
                                                            <a href="#" class="btn btn-primary btn-sm active">View Profile</a>
                                                            <div class="divider">
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="navbar-footer">
                                                    <div class="navbar-footer-content">
                                                        <div class="row">
                                                            <div class="col-md-6">
                                                                <a href="#" class="btn btn-default btn-sm">Change Password</a>
                                                            </div>
                                                            <div class="col-md-6">
                                                                <%= link_to "Log out", destroy_user_session_path, :method=>'delete', :class => 'class="btn btn-default btn-sm pull-right' %>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </li>
                                        </ul>
                                    </li>
                                    <% else %>
                                    <li class="dropdown"><%= link_to "Login", new_user_session_path %>
                                    <% end %>

abhinav.x.tiwari said over 7 years ago on Rails API - Authentication with JWT :

Thanks Kobaltz, i figured it out that i was using my application controller not the one which is present under api folder.

But there is a different problem now, i am trying to get information from students controller, and it doesn't work because of this line in the controller:

before_action :authenticate_user!

(P.S. I am using devise) 


Do you suggest?


abhinav.x.tiwari said over 7 years ago on Rails API - Authentication with JWT :

True, i implemented the same way:

This is my error(Json response, i think this is from devise):

{
  "error": "You need to sign in or sign up before continuing."
}

This is my user model:

class User < ApplicationRecord
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
         # remove required: false later
  alias_method :authenticate, :valid_password?
  def self.from_token_payload(payload)
        self.find payload["sub"]
  end  
end

This is my application_controller from api module:

module Api
  class ApplicationController < ActionController::API
    include Knock::Authenticable
    undef_method :current_user
  end
end





abhinav.x.tiwari said over 7 years ago on Rails API - Authentication with JWT :

1 thing, https://github.com/driftingruby/051-rails-api-authentication-with-jwt/blob/master/app/controllers/user_token_controller.rb

your controller doesn't include before_action :authenticate_user!

I have this line in my applicationcontroller which is causing issue:

class ApplicationController < ActionController::Base

  protect_from_forgery with: :exception

  before_action :authenticate_user!

 end


But i need this line for my web form to be authenticated!





abhinav.x.tiwari said over 7 years ago on Rails API - Authentication with JWT :

This exposes my application without authentication. for e.g. i am able to see /students page which i was earlier not able to view without authentication.