Group Group Group Group Group Group Group Group Group

Abort.401: User not authenticated. (GuardMiddleware.swift:43)

Hi,

This could be a simple case of I’ve made a mess of things and created a bug, but it’s driving me crazy that I can’t find where this bug exists.

When I attempt to run the tests in Chapter 19 the testAcronymsCanBeRetrievedFromAPI() is successful, but testAcronymCanBeSavedWithAPI() fails because testAcronymsCategories() fails at XCTAssertFails(categories[0].id, category.id) due to Fatal error: Index out of range. and in the Debug area I see:

[ INFO ] Reverting all migrations on ‘psql’ database (RevertCommand.swift:50)
[ INFO ] Reverting migration ‘AdminUser’ (MigrationContainer.swift:94)
[ INFO ] Reverting migration ‘Token’ (MigrationContainer.swift:94)
[ INFO ] Reverting migration ‘AcronymCategoryPivot’ (MigrationContainer.swift:94)
[ INFO ] Reverting migration ‘Category’ (MigrationContainer.swift:94)
[ INFO ] Reverting migration ‘Acronym’ (MigrationContainer.swift:94)
[ INFO ] Reverting migration ‘User’ (MigrationContainer.swift:94)
[ INFO ] Succesfully reverted all migrations (RevertCommand.swift:54)
[ INFO ] Migrating ‘psql’ database (FluentProvider.swift:28)
[ INFO ] Preparing migration ‘User’ (MigrationContainer.swift:50)
[ INFO ] Preparing migration ‘Acronym’ (MigrationContainer.swift:50)
[ INFO ] Preparing migration ‘Category’ (MigrationContainer.swift:50)
[ INFO ] Preparing migration ‘AcronymCategoryPivot’ (MigrationContainer.swift:50)
[ INFO ] Preparing migration ‘Token’ (MigrationContainer.swift:50)
[ INFO ] Preparing migration ‘AdminUser’ (MigrationContainer.swift:50)
[ INFO ] Migrations complete (FluentProvider.swift:32)
[ ERROR ] Abort.401: User not authenticated. (GuardMiddleware.swift:43)
[ ERROR ] Abort.401: User not authenticated. (GuardMiddleware.swift:43)

After reading through all my .swift files and sheer desperation I moved over the Sources and Tests folders from the Chapter 19 example final folder to replace my own, but still the error occurs.

I’ve removed the postgres-test and added it again, but still the same problem arises.

If anyone could suggest where I’ve gone wrong, I’d be very grateful. :slight_smile:

@0xtim Can you please help with this when you get a chance? Thank you - much appreciated! :]

@andy4202 :thinking: they seem to be working fine for me! Can you confirm that you’re calling the login function in your test to get a token? And that you’re sending that token in your request?

@0xtim
I’ve followed Chapter 18: API Authentication, Part 1 and on page 287 it explains how to receive the token back, which I do:

POST
U/n: admin
Password: password

RESPONSE BODY:

{
“id”: “F1B37C96-263B-496E-BC92-5EE0E4067019”,
“token”: “EZmjNa7CzVQgk9itMtU2Pg==”,
“userID”: “CBB2DCB4-0C6F-451A-BB53-272CEAD59305”
}

But when I get to page 290 and use Authorization Bearer EZmjNa7CzVQgk9itMtU2Pg==
With http://localhost:8080/api/acronyms
short - IKR
long - I Know Right
And Basic Authorization removed.
I get:

{
“error”: true,
“reason”: “User not authenticated.”
}

Since I was convinced that the problem was in my code I replaced all my files (Package.swift and everything in the folders Sources and Tests) with those in the final project example of the chapter, but the problems are the same. Can you confirm for me where the login function should be called?

@andy4202 how are you setting up the request? (Are you using RESTed for instance). Screenshots would help to diagnose the issue. One thing that I’ve just thought of is are you removing the BASIC Auth credentials first?

Hello,

I have the same issue when create new category using Rest app through
"localhost:8080/api/categories/
I have revert my database and remove the Basic Auth credentials before request.
But still get ’ User not authenticated’

Much appreciated if you could advise.

Hello again,

Now solved after carefully follow the tutorial.^^
Because I missed to add Header “Authorization” and its value "Bearer " as the tutorial mentioned.

@thammaporn Thank you for sharing the solution - much appreciated!

@shogunkaramazov

It’s my pleasure and Thanks for the great vapor forum .
I like it.^^

1 Like

Just had the same problem and your “One thing that I’ve just thought of is are you removing the BASIC Auth credentials first?” was worth its weight in gold :-). I know, it’s also written in the book, even in bold, but sometimes I’m too eager to try things out :smiley:

1 Like