javascript - Interceptors for managing restricted pages in AngularJS -
I started with AngularJS a few days ago and I'm having problems with my interceptor which 401 Prevents situations.
When it returns 401, then it transmits the message of "loginRequired" in such a way and a redirect is triggered on that event.
The problem is that if I try to access the restricted page, while not being logged in, I see the page flash for a moment before being redirected to the login page. I'm still starting in asynchronous stuff, promises etc. Will someone tell me what I am doing?
Here's my interceptor. As you can see that it's really simple, but I slowed it down to clear it on my side and I tried to understand things before developing it I am doing
Interceptor var services = angular Modules ('Services', []); Services.factory ('myInterceptor', ['$ q', '$ rootScope', function ($ q, $ rootScope) {var myInterceptor = {'responseError': function (rejection) {$ rootScope. $ Broadcast ('event: LoginRequired '); Return $ q.reject (rejection);}}; return myInterceptor;}]);
Injection of my interceptor
myApp.config (['$ httpProvider', function ($ httpProvider) {$ HttpProvider .interceptors.push ('myInterceptor');}]); Route for the restricted page
. When ('' / restricted page ', {templateUrl:' partial / restricted page html ', administrator:' restricted page control '}). Restricted Page Controller
Controller. Controller ('Restricted Page Controller', Function ($ scope) {// Some Warning ("I think there should not be");}; $ / root> $ Rootscope event viewer
$ Paths $. ($ Location.path () == "/ free asset"); // $ '(' Event: loginRequired ', function), // if we are not on the free access page only; // Other login page $ location.path ('/ home' );}); My problem is obviously the way I handle the interceptor and $ q. I found another way to make an interceptor, but this is not the way to use the official document, so I think it can be an old way and it is not clear by putting it into a factory in my opinion. He simply puts this code after defining the routes in the configuration function of his module. But this code works and I do not get page flash
I found another way on the gitub
var interceptor = ['$ RootScope ',' $ q ',' $ log ', function (area, $ q, $ log) {function success (feedback) {return response; } Function error (response) {var status = response.status; If (condition == 401) {var deferred = $ q.defer (); Var req = {config: response.config, deferred: deferred}; . Scope $ Transmission ('Event: LoginRequired'); Refund refund. } // Return the $ q.reject (response); } Return function (promise) {return promise.then (success, error); }; }]; $ HttpProvider.responseInceptceptors.push (interceptor); But my goal is not to "do this work" and I hate the mantra. If it is not broken then do not fix it. "I have to understand that my code What's the problem with thanks!
Instead of transmitting 'event: loginRequired' from your interceptor, try changing the path to the location path Your interceptor can get broadcast 401 and replace the location and cause the screen to 'flash'. , '$ Rootsecope', '$ location', function ($ q, $ rootsecope, $ location) {var myInterceptor = {'responseError': function (disapproval) {if (response.status === 401 & $ Location .path ()! == '/ freeAccess') {// Other login page $ location.path ('/ home') Replace ();} // Return the $ q.reject (response); }}; My interceptor;}]);
When the user is authorized, when your app module runs for the first time, you can also make an HTTP request:
myApp.config ([ '$ HttpProvider', function ($ httpProvider), $ httpProvider.interceptors.push ('myInterceptor');}]) .ron (function ($ http) {// If it returns 401, then your interceptor $ http Get triggered ('some endpoint-to-determined-at');});
Comments
Post a Comment