ESA - failed to implement current user

I’m trying without success to get work Implicit Grant authentication and followed the steps described in Ember Simple Auth managing current user guide. After logging I’m still redirected to the route (’/’) path and is not authenticated instead of being redirected to /dashboard. More over, I see the dashboard for a couple of seconds and then I get the route / path again. The code source is here. The backe-end in Rails has users/me route called:

Started GET "/users/me" for at 2018-02-02 15:17:55 +0100
Processing by UsersController#me as application/vnd.api+json
  User Load (1.3ms)  SELECT  "users".* FROM "users" WHERE "users"."username" = $1 LIMIT $2  [["username", "Z28SCAMB"], ["LIMIT", 1]]
  User Load (0.4ms)  SELECT "users".* FROM "users"
[active_model_serializers] Rendered ActiveModel::Serializer::CollectionSerializer with ActiveModelSerializers::Adapter::JsonApi (0.86ms)
Completed 200 OK in 8ms (Views: 2.3ms | ActiveRecord: 1.7ms)

What am I doing wrong, please ? Should I encode the same token on the backend side as well and pass it back to Ember ? Thank you in advance.

Really hard to say where it’s failing without some active debugging. Have you tried putting a bunch of breakpoints in to see where things are going wrong?

One thing I noticed is that you don’t actually have an index route defined so when your app ends up at ‘/’ it’s unclear what’s happening. To fix that you could either define a routes/index.js route that redirects to ‘dashboard’, or you could just make dashboard have a path of / in your router.

If I change the dashboard route in router.js like that:

this.route('dashboard'); // before
this.route('dashboard', { path: '/' }); // after

the app fails to start and display this error:

Error: Assertion Failed: You attempted to define a `{{link-to "index"}}` but did not pass the parameters required for generating its dynamic segments. There is no route named index

I’ve already put some console.log into current-user.js service:

export default Service.extend({
  session: service('session'),
  store: service(),

  loadCurrentUser() {
    console.log("#loadCurrentUser:" + this.get('session.isAuthenticated'));
    if (this.get('session.isAuthenticated')) {
      return this.get('store').queryRecord('user', { me: true }).then((user) => {
        console.log("user: " + user);
        this.set('user', user);
    } else {
      return RSVP.resolve();

and get always false for this.get('session.isAuthenticated') value :frowning:

So the error happens because when you change the path of dashboard it’s replacing the index route, so now there is no index route, or at least not a route specifically named index (there would be a default created one if no route had the / path). However in your main-navigation component you’re trying to link to index:

{{#link-to 'index' classNames='menu-text'}}Home{{/link-to}}

So if you changed that link-to to ‘dashboard’ instead of ‘index’ it would fix the error.

As for debugging, I’d highly recommend using the javascript debugger instead of console.log for most bugs. It will give you more consistent results, and allow you to really dig into the code. I’d put breakpoints in your application route hooks, your current user service, your login route/component, and anywhere else where authentication related code will be triggered. Then you can step through the breakpoints to figure out what is happening and in what order. That will make it much easier to debug.

OK, fixed the issue with index link in navigation. I’ll try to debug in Ember inspector. Really weird, even the dummy app displays Logout button (so I’m authenticated) after being logged in, but still displays the content of

{{#unless session.isAuthenticated}}
  <div class="alert alert-info">
    You can {{#link-to 'login' classNames='alert-link'}}log in{{/link-to}} with login <code>letme</code> and password <code>in</code>.

in index.hbs template. In the very beginning, I take the actual code from there. Then I changed according to ESA current user Guide.

First discovered thing,- I pass twice into if (this.get('session.isAuthenticated')) in current-user#loadCurrentUser: after being authenticated it is evaluated to true, when I pass the 2d time it is evaluated to false. WHY ? A Real mistery :frowning:

Sounds like you’re getting authenticated but then something is invalidating your session right away. Maybe try commenting out the .catch on this line and seeing if there are any errors thrown?

1 Like

Thank you so much @dknutsen ! I figured out what the problem was after commenting out the catch block. In the back-end UsersController I checked the rpesence of me in params hash. That was the old implementation:

class UsersController < ApplicationController

  def me
    if params[:me].present?
      users = User.find(
      users = User.all
    render json: users, status: :ok

And I always returned an Array what generates the error on Ember side:

Error: Assertion Failed: Expected the primary data returned by the serializer for a `queryRecord` response to be a single object but instead it was an array

After fixing the controller to return a single record:

class UsersController < ApplicationController

  def me
    user = User.find(
    render json: user, status: :ok

it works like a charm :). Thank you for your help!

1 Like

Awesome glad you got it figured out!