|
1 |
| -# KJH Middleware |
| 1 | +# Angular Middleware |
2 | 2 |
|
3 |
| -Laravel-like middleware for Angular |
| 3 | +> Laravel-like middleware for ngRoute & ui.router |
4 | 4 |
|
5 |
| -I need to write new documentation |
| 5 | + |
| 6 | + |
| 7 | +## Installation |
| 8 | + |
| 9 | +1. Get it on your computer |
| 10 | + * Bower `bower install --save angular-middleware` |
| 11 | + * NPM `npm install --save angular-middleware` |
| 12 | + * GitHub `git clone https://github.com/oldtimeguitarguy/angular-middleware` |
| 13 | + |
| 14 | +2. Include `angular-middleware.min.js` in your app, whichever way you choose |
| 15 | + |
| 16 | +3. Include the module that works for you: |
| 17 | + * `ui.router.middleware` |
| 18 | + * `ngRoute.middleware` |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | +## Configuration & Examples |
| 23 | + |
| 24 | +```javascript |
| 25 | +// An app with ui.router... |
| 26 | +var app = angular.module('app', [ |
| 27 | + 'ui.router', |
| 28 | + 'ui.router.middleware' |
| 29 | +]); |
| 30 | + |
| 31 | +// An app with ngRoute... |
| 32 | +var app = angular.module('app', [ |
| 33 | + 'ngRoute', |
| 34 | + 'ngRoute.middleware' |
| 35 | +]); |
| 36 | + |
| 37 | +///////////////////////////////////////////////////////// |
| 38 | +// Either way you go, the rest is essentially the same // |
| 39 | +///////////////////////////////////////////////////////// |
| 40 | + |
| 41 | +/** |
| 42 | + * First, you need to map your middleware functions. |
| 43 | + * This can be done cleanly with separate files |
| 44 | + * for each middleware function. You can do that |
| 45 | + * a number of different ways. I'll just show you |
| 46 | + * the basics. |
| 47 | + */ |
| 48 | +app.config(['$middlewareProvider', |
| 49 | +function($middlewareProvider)] { |
| 50 | + |
| 51 | + // If you want middleware, |
| 52 | + // then you need to map some middleware |
| 53 | + // functions to names that you can |
| 54 | + // reference in your routes |
| 55 | + $middlewareProvider.map({ |
| 56 | + |
| 57 | + /** Don't allow anyone through */ |
| 58 | + 'nobody': function nobodyMiddleware() { |
| 59 | + // |
| 60 | + }, |
| 61 | + |
| 62 | + /** Let everyone through */ |
| 63 | + 'everyone': function everyoneMiddleware() { |
| 64 | + // In order to resolve the middleware, |
| 65 | + // you MUST call this.next() |
| 66 | + this.next(); |
| 67 | + }, |
| 68 | + |
| 69 | + /** Redirect everyone */ |
| 70 | + 'redirect-all': function redirectAllMiddleware() { |
| 71 | + // If you are using ui.router, |
| 72 | + // then you must choose a state name |
| 73 | + this.redirectTo('another-state-name'); |
| 74 | + |
| 75 | + // If you are using ngRoute, |
| 76 | + // then you must actually put in |
| 77 | + // the new url that you would use in |
| 78 | + // $location.path() |
| 79 | + this.redirectTo('/another-path'); |
| 80 | + }, |
| 81 | + |
| 82 | + /** Continue, but log the parameters */ |
| 83 | + 'log': ['$log', function logMiddleware($log) { |
| 84 | + // Notice that we used dependency injection to get $log. |
| 85 | + // You have access to the route parameters with this.params |
| 86 | + $log.debug(this.params); |
| 87 | + |
| 88 | + // Keep on truckin' |
| 89 | + this.next(); |
| 90 | + }], |
| 91 | + |
| 92 | + /** It will wait for async requests too! */ |
| 93 | + 'async-auth': ['$http', function asyncAuth($http) { |
| 94 | + // We'll need access to "this" in a deeper context |
| 95 | + var self = this; |
| 96 | + |
| 97 | + // Grab something from the server |
| 98 | + $http.get('/verify') |
| 99 | + |
| 100 | + // The server has responded! |
| 101 | + .then(function success(res) { |
| 102 | + if ( res.isVerified ) { |
| 103 | + return self.next(); |
| 104 | + } |
| 105 | + |
| 106 | + self.redirectTo('another-state-or-path'); |
| 107 | + }, |
| 108 | + |
| 109 | + function fail(err) { |
| 110 | + self.redirectTo('another-state-or-path'); |
| 111 | + }); |
| 112 | + }] |
| 113 | + |
| 114 | + }); |
| 115 | + |
| 116 | +}); |
| 117 | + |
| 118 | +/** |
| 119 | + * Now you're ready to use your middleware! |
| 120 | + * All you have to do is put them in your routes. |
| 121 | + * Each middleware is processed in the order you list them. |
| 122 | + * |
| 123 | + * The principle is the same for ui.router and ngRoute. |
| 124 | + * I'll show you both just to cover all the bases. |
| 125 | + */ |
| 126 | + |
| 127 | + /** ui.router */ |
| 128 | + app.config(['$stateProvider', function($stateProvider) { |
| 129 | + $stateProvider |
| 130 | + |
| 131 | + // You can have just one middleware, |
| 132 | + // represented by a string |
| 133 | + .state('my-state-name', { |
| 134 | + ... |
| 135 | + middleware: 'a-single-middleware' |
| 136 | + }) |
| 137 | + |
| 138 | + // You can have multiple middleware |
| 139 | + // separated by pipes. aka. | |
| 140 | + .state('another-state-name', { |
| 141 | + ... |
| 142 | + middleware: 'one-middleware|another-middleware' |
| 143 | + }) |
| 144 | + |
| 145 | + // You can have multiple middleware as an array |
| 146 | + .state('a-third-state-name', { |
| 147 | + ... |
| 148 | + middleware: ['one-middleware', 'another-middleware', 'another-nother-middleware'] |
| 149 | + }) |
| 150 | + }]); |
| 151 | + |
| 152 | + /** ngRoute */ |
| 153 | + app.config(['$routeProvider', function($routeProvider) { |
| 154 | + $routeProvider |
| 155 | + |
| 156 | + // You can have just one middleware, |
| 157 | + // represented by a string |
| 158 | + .when('/my-path', { |
| 159 | + ... |
| 160 | + middleware: 'a-single-middleware' |
| 161 | + }) |
| 162 | + |
| 163 | + // You can have multiple middleware |
| 164 | + // separated by pipes. aka. | |
| 165 | + .when('/my-other-path', { |
| 166 | + ... |
| 167 | + middleware: 'one-middleware|another-middleware' |
| 168 | + }) |
| 169 | + |
| 170 | + // You can have multiple middleware as an array |
| 171 | + .when('/my-third-path', { |
| 172 | + ... |
| 173 | + middleware: ['one-middleware', 'another-middleware', 'another-nother-middleware'] |
| 174 | + }) |
| 175 | + }]); |
| 176 | + |
| 177 | +``` |
| 178 | + |
| 179 | + |
| 180 | +## $middlewareProvider |
| 181 | + |
| 182 | +* `$middlewareProvider.map(<object>)` This is how you define and name your middleware. |
| 183 | + |
| 184 | +* `$middlewareProvider.bypassAll(<boolean>)` This gives you a way to easily bypass all middleware... as if it didn't exist! |
| 185 | + |
| 186 | +* `$middlewareProvider.global(<string|array>)` If you want to apply some middleware to **all** routes, you can easily do it here. The same rules apply to setting up middleware on routes, ie. you can use a string, a string with pipes, or an array of middleware names. **NOTE:** Anything defined here will be called **before** the route middleware is called. |
| 187 | + |
| 188 | + |
| 189 | +## Things you can do inside your middleware functions |
| 190 | +> If you don't know what I'm talking about, look at the example above |
| 191 | +
|
| 192 | +* `this.next()` **must be called** to resolve the middleware and either go to the next middleware or resolve the route |
| 193 | + |
| 194 | +* `this.redirectTo(<string>)` can be called to immediately redirect to a given path _(ngRoute)_ or state name _(ui.router)_ |
| 195 | + |
| 196 | +* `this.params` is an object that contains the current route parameters |
0 commit comments