|
158 | 158 | </div> |
159 | 159 | <div role="tabpanel" class="tab-pane" id="auth1-0-0"> |
160 | 160 | {{#markdown}} |
| 161 | + |
161 | 162 | # $auth 1.0.0 |
162 | 163 |
|
163 | | -<a class="src-code" href="https://github.com/ozsay/angular-meteor-auth/blob/master/angular-meteor-auth.js" target="_blank"> |
164 | | - src: angular-meteor-auth.js |
| 164 | +<a class="src-code" href="https://github.com/Urigo/angular-meteor-auth/blob/master/angular-meteor-auth.js" target="_blank"> |
| 165 | + src: angular-meteor-auth/angular-meteor-auth.js |
165 | 166 | </a> |
166 | 167 |
|
167 | | -`$auth` is a part of `angular-meteor-auth` package, and it located inside `angular-meteor.auth` AngularJS module. |
168 | | - |
169 | | -This module adds the functionality of meteor auth to your app for an easy access within angular. |
170 | | - |
171 | | -It will automatically extend your reactive contexts with the auth functionality. |
| 168 | +An extension which comes in a seperate package called `angular-meteor-auth` which provides us with authentication related methods and properties. Note that `accounts-base`package needs to be installed in order for this module to work, otherwise an error will be thrown. |
172 | 169 |
|
173 | 170 | ## Installation |
174 | 171 |
|
175 | 172 | You will need to add the package to your project and add it's module as a dependency to your AngularJS app: |
176 | 173 |
|
177 | | - meteor add angular-meteor-auth |
| 174 | +```js |
| 175 | +meteor add angular-meteor-auth |
178 | 176 |
|
179 | | - angular.module('myApp', [ |
180 | | - 'angular-meteor', |
181 | | - 'angular-meteor.auth' |
182 | | - ]); |
| 177 | +angular.module('myApp', [ |
| 178 | +'angular-meteor', |
| 179 | +'angular-meteor.auth' |
| 180 | +]); |
| 181 | +``` |
183 | 182 |
|
184 | | -## API Reference |
| 183 | +## Properties |
185 | 184 |
|
186 | | -### $awaitUser |
| 185 | +The following properties will be added to the `$scope` / `view model` during initialization and will be updated reactively: |
187 | 186 |
|
188 | | -Waits for a user to login. |
| 187 | +- currentUser - The current user logged in. Reactivated by [Accounts.user](http://docs.meteor.com/#/full/meteor_user). |
| 188 | +- currentUserId - The current user's id. Reactivated by [Accounts.userId](http://docs.meteor.com/#/full/meteor_userid). |
| 189 | +- isLoggingIn - Inticates whenever we try to log in. Reactivated by [Accounts.loggingIn](http://docs.meteor.com/#/full/meteor_loggingin). |
189 | 190 |
|
190 | | -This method is useful when you want to run part of your code only after the user logs in. |
| 191 | +#### Example |
191 | 192 |
|
192 | | -This method returns a promise which fulfills when the user logged in successfully or rejects when the user failed to login. |
| 193 | +```html |
| 194 | +<div ng-show="ctx.isLoggingIn">Logging in...</div> |
| 195 | +<div ng-show="ctx.currentUser">I am visible only for logged in users!</div> |
| 196 | +<div ng-hide="ctx.currentUser">I am visible only for guests!</div> |
| 197 | +``` |
193 | 198 |
|
194 | | -In addition you send an user validation function that will run against the logged in user and rejects if the user is not |
195 | | -validated. |
| 199 | +## Methods |
196 | 200 |
|
197 | | -#### Arguments |
198 | | - |
199 | | -<table class="variables-matrix input-arguments"> |
200 | | - <thead> |
201 | | - <tr> |
202 | | - <th>Param</th> |
203 | | - <th>Type</th> |
204 | | - <th>Details</th> |
205 | | - <th>Required</th> |
206 | | - </tr> |
207 | | - </thead> |
208 | | - <tbody> |
209 | | - <tr> |
210 | | - <td><strong>validation</strong></td> |
211 | | - <td><a href="" class="label type-hint type-hint-function">Function</a></td> |
212 | | - <td><p>An user validation function</p></td> |
213 | | - <td>No</td> |
214 | | - </tr> |
215 | | - </tbody> |
216 | | -</table> |
217 | | - |
218 | | -#### Usage examples |
219 | | - |
220 | | -Waiting for a regular user |
221 | | - |
222 | | - myModule.controller('MyCtrl', ['$scope', '$reactive', function($scope, $reactive) { |
223 | | - $reactive(this).attach($scope); |
224 | | - |
225 | | - this.$awaitUser().then((user) => { |
226 | | - console.log('The user has logged in successfully!', user); |
227 | | - }, (err) => { |
228 | | - console.log('There was an error in login process', err); |
229 | | - }); |
230 | | - }]); |
231 | | - |
232 | | -Waiting for an admin |
233 | | - |
234 | | - myModule.controller('MyCtrl', ['$scope', '$reactive', function($scope, $reactive) { |
235 | | - $reactive(this).attach($scope); |
236 | | - |
237 | | - this.$awaitUser((user) => { |
238 | | - return user.role === "Admin"; |
239 | | - }).then((admin) => { |
240 | | - console.log('An admin has logged in successfully!', admin); |
241 | | - }, (err) => { |
242 | | - if (err === "AUTH_REQUIRED") |
243 | | - console.log('There was an error in login process'); |
244 | | - else if (err === "FORBIDDEN") |
245 | | - console.log('A user has logged in who isn\'t an admin'); |
246 | | - }); |
247 | | - }]); |
248 | | - |
249 | | -> Please note, that `$awaitUser` is intended to be invoked within the login process and will not work properly before |
250 | | - it has begun. |
251 | | - |
252 | | -### currentUser |
| 201 | +### $awaitUser |
253 | 202 |
|
254 | | -Keeps the current user object (`Meteor.user()`) |
| 203 | +Waits for the user to finish the login process. Our waiting can be stopped manually or automatically once our `$scope` has been destroyed. For syntactic sugar, you may also call `awaitUser` in a service called `$auth` which is an alias for `$rootScope.$awaitUser`. |
255 | 204 |
|
256 | | -### currentUserId |
| 205 | +------ |
257 | 206 |
|
258 | | -Keeps the current user id (`Meteor.userId()`) |
| 207 | +#### Arguments |
259 | 208 |
|
260 | | -### isLoggingIn |
| 209 | +<table class="variables-matrix input-arguments"> |
| 210 | + <thead> |
| 211 | + <tr> |
| 212 | + <th>Name</th> |
| 213 | + <th>Type</th> |
| 214 | + <th>Details</th> |
| 215 | + <th>Required</th> |
| 216 | + </tr> |
| 217 | + </thead> |
| 218 | + <tbody> |
| 219 | + <tr> |
| 220 | + <td>validationFn</td> |
| 221 | + <td> |
| 222 | + <a href="" class="label type-hint type-hint-function">Function</a> |
| 223 | + </td> |
| 224 | + <td>A validation function which will be invoked with the user once the login process has been succedded. If the returned value is `true` then the returned promised will be resolved. Else, if the returned value is 'false' the returned promise will be rejected with 'FORBIDDEN'. Else, if the returned value is a string or an error they will be rejected by the promise as is.</td> |
| 225 | + <td>No</td> |
| 226 | + </tr> |
| 227 | + </tbody> |
| 228 | +</table> |
261 | 229 |
|
262 | | -Keeps the current user object (`Meteor.loggingIn()`) |
| 230 | +#### Return value |
263 | 231 |
|
| 232 | +Returns a promise which will be fulfilled accordingly. If the login process has been failed the promise will be rejected with 'AUTH_REQUIRED'. Else, the validation function will be invoked with the logged in user, and if the user is valid the promise will be resolved with the logged in user. Also, a 'stop' method is defined on the promise in case we would like to stop our waiting. |
264 | 233 |
|
265 | | -These are all reactive properties which are added to the contexts. |
| 234 | +------ |
266 | 235 |
|
267 | | -### Usage example inside a view |
| 236 | +#### Examples |
268 | 237 |
|
269 | | - <div ng-show="ctx.isLoggingIn">Logging in...</div> |
270 | | - <div ng-show="ctx.currentUser">I am visible only for logged in users!</div> |
271 | | - <div ng-hide="ctx.currentUser">I am visible only for guests!</div> |
| 238 | +Running a piece of code only once logged in: |
272 | 239 |
|
273 | | -{{/markdown}} |
| 240 | +```js |
| 241 | +angular.module('myApp').directive('myComponent', function() { |
| 242 | + return { |
| 243 | + link: function($scope) { |
| 244 | + $scope.awaitUser().then(() => { |
| 245 | + let currentUser = $scope.currentUser; |
| 246 | + }); |
| 247 | + } |
| 248 | + }; |
| 249 | +}); |
| 250 | +``` |
| 251 | + |
| 252 | +Usage with $auth service: |
| 253 | + |
| 254 | +```js |
| 255 | +$stateProvider |
| 256 | + .state({ |
| 257 | + template: '<my-component></my-component>', |
| 258 | + resolve: { |
| 259 | + user: ($auth) => { |
| 260 | + return $auth.awaitUser(); |
| 261 | + } |
| 262 | + } |
| 263 | + }); |
| 264 | +``` |
| 265 | + |
| 266 | +Usage with validation method: |
| 267 | + |
| 268 | +```js |
| 269 | +$stateProvider |
| 270 | + .state({ |
| 271 | + template: '<my-component></my-component>', |
| 272 | + resolve: { |
| 273 | + user: ($auth) => { |
| 274 | + return $auth.awaitUser((user) => { |
| 275 | + if (user.firstName === 'Uri') { |
| 276 | + return true; |
| 277 | + } |
| 278 | + else { |
| 279 | + return 'You are not Uri!'; |
| 280 | + } |
| 281 | + } |
| 282 | + } |
| 283 | + } |
| 284 | + }); |
| 285 | +``` |
| 286 | + |
| 287 | +Waiting for a regular user: |
| 288 | + |
| 289 | +```js |
| 290 | +myModule.controller('MyCtrl', ['$scope', '$reactive', function($scope, $reactive) { |
| 291 | + $reactive(this).attach($scope); |
| 292 | + |
| 293 | + this.$awaitUser().then((user) => { |
| 294 | + console.log('The user has logged in successfully!', user); |
| 295 | + }, (err) => { |
| 296 | + console.log('There was an error in login process', err); |
| 297 | + }); |
| 298 | +}]); |
| 299 | +``` |
| 300 | + |
| 301 | +Waiting for an admin: |
| 302 | + |
| 303 | +```js |
| 304 | +myModule.controller('MyCtrl', ['$scope', '$reactive', function($scope, $reactive) { |
| 305 | + $reactive(this).attach($scope); |
| 306 | + |
| 307 | + this.$awaitUser((user) => { |
| 308 | + return user.role === "Admin"; |
| 309 | + }).then((admin) => { |
| 310 | + console.log('An admin has logged in successfully!', admin); |
| 311 | + }, (err) => { |
| 312 | + if (err === "AUTH_REQUIRED") |
| 313 | + console.log('There was an error in login process'); |
| 314 | + else if (err === "FORBIDDEN") |
| 315 | + console.log('A user has logged in who isn\'t an admin'); |
| 316 | + }); |
| 317 | +}]); |
| 318 | +``` |
| 319 | + |
| 320 | + {{/markdown}} |
274 | 321 | </div> |
275 | 322 | </div> |
276 | 323 | </div> |
|
0 commit comments