Skip to content

Commit 3e6e964

Browse files
committed
(docs) auth
1 parent 6e41707 commit 3e6e964

File tree

1 file changed

+128
-0
lines changed
  • docs/angular-meteor/client/content/api-reference/1.3.2

1 file changed

+128
-0
lines changed

docs/angular-meteor/client/content/api-reference/1.3.2/api.auth.html

+128
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,134 @@
1313
src: angular-meteor-auth/modules/auth.js
1414
</a>
1515

16+
`$auth` service is a part of `angular` package (also included in `angular-with-blaze` package), and it located inside `angular-meteor.auth` AngularJS module.
17+
18+
This service provides the functionality for authentication for your app.
19+
20+
`$auth` wraps Meteor's API for ease of use with AngularJS applications, and also provides the ability to access Meteor's Accounts package from the UI.
21+
22+
In order to use this service, you will need to add it's module as a dependency to your AngularJS app:
23+
24+
angular.module('myApp', [
25+
'angular-meteor',
26+
'angular-meteor.auth'
27+
]);
28+
29+
## API Reference
30+
31+
### waitForUser()
32+
33+
`waitForUser` is a method of `$auth` which waits for any user to log in.
34+
35+
It a wrapper for `Meteor.loggingIn` method, but it's return value is a promise which can be only resolved, when any user is done with the login process.
36+
37+
It is useful when you want to run part of your code only after the user logs in.
38+
39+
#### Usage example
40+
41+
angular.module('myApp').directive('myComponent', function() {
42+
return {
43+
link: function($auth) {
44+
$auth.waitForUser().then(() => {
45+
// Login is done!
46+
});
47+
}
48+
};
49+
});
50+
51+
52+
### requireUser()
53+
54+
`requireUser` provides a useful functionality for performing a one-time login check, with a promise.
55+
56+
It's very useful for `angular-ui-router`'s [resolve](https://github.com/angular-ui/ui-router/wiki#resolve) functionality.
57+
58+
It's return value is a promise, which resolves when the login process is done, and the `Meteor.user()` is valid and not `null`.
59+
60+
#### Usage example with angular-ui-router:
61+
62+
$stateProvider
63+
.state({
64+
template: '<my-component></my-component>',
65+
resolve: {
66+
user: ($auth) => {
67+
return $auth.requireUser();
68+
}
69+
}
70+
});
71+
72+
### requireValidUser(validationMethod)
73+
74+
`requireValidUser` uses `requireUser` method, but adding another custom check that provided by the user.
75+
76+
It's return value is a promise, which resolves if the user is logged in and the custom validation method returns `true`.
77+
78+
If the user validation message returns anything other than `true`, the promise will rejected and the promise failure argument will be the return value of the validation message.
79+
80+
#### Usage example with angular-ui-router:
81+
82+
In this example, we will allow only logged in users, with `firstName` equals to `Uri`.
83+
84+
$stateProvider
85+
.state({
86+
template: '<my-component></my-component>',
87+
resolve: {
88+
user: ($auth) => {
89+
return $auth.requireValidUser((user) => {);
90+
if (user.firstName === 'Uri') {
91+
return true;
92+
}
93+
else {
94+
return 'You are not Uri!';
95+
}
96+
}
97+
}
98+
}
99+
});
100+
101+
`$auth` service also provides two useful properties that are useful in views:
102+
103+
### currentUser
104+
105+
`$auth.currentUser` is a property of the `$auth` service, which contain the current logged in user in every time.
106+
107+
It automatically changes when the user logs in or logs out, and it can be used from the view or the AngularJS controller/components.
108+
109+
It's very useful for hiding / showing parts of the view depending on the login status.
110+
111+
#### Usage example inside a view
112+
113+
<div ng-show="$auth.currentUser">I am visible only for logged in users!</div>
114+
<div ng-hide="$auth.currentUser">I am visible only for guests!</div>
115+
116+
#### Usage example inside a component
117+
118+
angular.module('myApp').directive('myComponent', function() {
119+
return {
120+
link: function($auth) {
121+
$auth.waitForUser().then(() => {
122+
let currentUser = $auth.currentUser;
123+
});
124+
}
125+
};
126+
});
127+
128+
### loggingIn
129+
130+
`$auth.loggingIn` is a property of the `$auth` service, which contain the current login status.
131+
132+
It's a wrapper for [Meteor.loggingIn](http://docs.meteor.com/#/full/meteor_loggingin) method.
133+
134+
It's value is `true` if a login method (such as `Meteor.loginWithPassword`, `Meteor.loginWithFacebook`, or `Accounts.createUser`) is currently in progress.
135+
136+
> You can use this property in your view in order to display a spinner or a "Logging you in..." message!
137+
138+
#### Usage example inside a view
139+
140+
<div ng-show="$auth.loggingIn">Logging in...</div>
141+
<div ng-show="$auth.currentUser">I am visible only for logged in users!</div>
142+
<div ng-hide="$auth.currentUser">I am visible only for guests!</div>
143+
16144
{{/markdown}}
17145
</do-nothing>
18146

0 commit comments

Comments
 (0)