Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User Authentiation Using Backbonejs and SLIM Framework

I am trying to develop an application using Backbonejs and SLIM framework. Now I would like to develop User Login/Log out functionality. In this case I would like to keep SESSION alive in server while user navigating from one page to another. Previously I worked one other PHP framework and core PHP and successfully implement User Authentication. SLIM framework works as API. Actually my problem is "How to know the current SESSION is alive using HTTP request to an API from Backbonejs??"

It looks messy to me. I read several stackoverflow post like below but could not come to a solution.

How to do authentication with a REST API right? (Browser + Native clients)

If REST applications are supposed to be stateless, how do you manage sessions?

How to use PHP sessions with REST client application ?

I hope anyone of you implement User Authentication using Backbonejs and SLIM Framework as API.If he/she share his/her experience with some sample code, that will be a BIG help me.

Thanks

like image 617
PHP PHP Avatar asked May 13 '26 10:05

PHP PHP


1 Answers

I am not sure with Slim API. But With Restful web services there is no session concept.

Instead, You can have a http header token that carries around your auth token and this token could be persisted in table(User table for instance). A check can be made to ensure if the token is valid each time a request hits the server. Nullify the token on signout.

Post Authentication,from the success callback for signin operation you can trigger a a custom event that has been bound to load the success page.

EG:

Intialise at the application level.

var controller = _.extend({}, Backbone.Events);
controller.on("myapp:dashboard",function() {
  //your dashboard data fetch logic.
  //create instance of the view and pass fetched data.
});

in login success handler(callback) make the following call:

Delegates to the custom controller.
controller.trigger("myapp:dashboard");

Changes the url to the correct location.
myAppRouter.navigate("tourl",{trigger:false});

Reference: http://lostechies.com/derickbailey/2012/04/03/revisiting-the-backbone-event-aggregator-lessons-learned/

like image 65
Balaji Avatar answered May 16 '26 08:05

Balaji



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!