sdelements / lets-chat

Self-hosted chat app for small teams
http://sdelements.github.io/lets-chat
MIT License
9.77k stars 1.58k forks source link

How to actually authenticate via ajax request? #693

Closed mrmnmly closed 8 years ago

mrmnmly commented 8 years ago

I've deployed a Let's Chat application for my own server.

However, instead of using currently built, original Let's Chat web application I would like to develop my own, using its API.

And according to Let's Chat wiki:

Revoke an API Token

In the top-left dropdown menu:

  • Select "Auth tokens"
  • Click "Revoke token"
  • Choose "Yes". This will delete any previously generated token.

Basic Authentication

Use the API token as the username when authenticating. The password can be set to anything, but it must not be blank (simply because most clients require it).

So far I've generated own token and tried to send GET request to retrieve all rooms that I have in the app, but I've got an error: 401 - Unauthorized - I've tried to send this request with { data: my_token, password: my_random_password } credentials but without success. So my main question is: how exactly I can authenticate with Let's Chat API using ajax request?

I couldn't find any API url / endpoint dedicated for such task - please help.

EDIT:

I've tried also setting headers but it still doesn't work:

  $.ajax({
        url: CHAT_URL + 'rooms',
        beforeSend: function(xhr){
            xhr.setRequestHeader('Authentication', 'bearer NTcz112222222222wYThiNzZhYjhmYjFjOWJkOTQ5ZDQ2YjhjNWUyMzkwNmMzYjhjMQ==');
        }
    }).done(function(resp){
        console.log('1');
        console.log(resp);
    }).done(function(resp){
        console.log('2');
        console.log(resp);
    });
hhaidar commented 8 years ago

Change your header to Authorization:

xhr.setRequestHeader('Authorization', 'bearer......
mrmnmly commented 8 years ago

It works! thanks!