Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular JS API authentication

Good afternoon,

I have a sort of general conceptual question I hope you guys can help me sort out. It's primarily about AngularJS but is also relevant to all the MV* JS frameworks. How do you deal with traditionally server side features in the world of AngularJS, etc?

Consider an example - authentication. I have a Web API business layer project (with it's own url) which takes JSON and talks to the DB and spits out JSON back, standard stuff. it has a filter that checks for username and password coming in the headers and doesn't let you to the method if they don't match username and password API expects.

Before AngularJS my client app would be an MVC (non web api) project with very light controllers - the whole point of the controller would be to receive data from jQuery ajax methods in the view, add the authentication info from web.config, pass this whole thing on to the API url, and spit the resulting (usually partial) view back to jQuery.

It seems in the world of AngularJS I don't have to do that - I can just have Angular's $http method go directly for the API and then let Angular do it's thing. That way there is no, for the lack of better term, "server-side" to the client side app at all. But I don't see how things like authentication can be accomplished at all.

Am I misunderstanding client side frameworks? Do client side apps still need a "server side" component that talks to the actual API? In that case what's the allure/benefit of Angular? Using a controller to spit partial views to jQuery seems like a good methodology all in itself.

As you can probably tell I am new to the client side frameworks :) I appreciate your help!

like image 838
americanslon Avatar asked Oct 03 '22 08:10

americanslon


1 Answers

You're right you don't need anything more than API (Web API in your case) as angular handles all the logic client-side (controllers, html templates etc).

Also check out https://github.com/witoldsz/angular-http-auth and http://www.espeo.pl/2012/02/26/authentication-in-angularjs-application to see how to implement authentication in angularjs in best way.

like image 122
Tadeusz Wójcik Avatar answered Oct 08 '22 01:10

Tadeusz Wójcik