firewire-angular-auth
v1.0.9
Published
firewire | a modular ecosystem for building firebase apps in express.
Downloads
14
Maintainers
Readme
Use this module with https://www.npmjs.com/package/firewire-angular
Install
Install into your public node_modules folder (if this doesn't make sense, check out https://www.npmjs.com/package/firewire).
$ npm install firewire-angular-auth --save
Firebase
For this module to work - you will need to enable your particular auth method in firebase.
What you get
- login & i forgot my password views
- login by
- email / password
- anonymous
- third party provider - facebook / google / github (note that these methods are not all tested)
- logout
- password reset
- password change
- email change
Coming soon - create / delete users
Add 'login' & 'i forgot my password' views
Within your firewire-angular-auth module you should have 2 jade files. Move these to your views folder.
Now you can set up the following firewire route in index.js
router.get('/user/:page', function(req, res, next) {
firewire.wire(req,res,firewire.load,[]);
});
Now when you go to localhost:3000/user/login you'll be greeted with a login screen.
Redirecting to your page of choice when logged in
Within node_modules/angular-fire-auth lives controller.js
Just change the route here to redirect to your page of choice.
if(authdata){window.location.assign("/admin/pages");}
Authenticating within your own app
Add the file as a dependancy in your jade template
script(src="/node_modules/firewire-angular-auth/index.js")
Add the auth module as a dependancy in your angular app
You should also have firebase and firewireModule as dependancies.
var app = angular.module('app', [
"firebase",
"firewireModule",
"firewireAuthModule"
]);
Auth module is now set up :)
Doing something on auth / unauth
app.controller('myController',['$rootScope', function ($rootScope) {
$rootScope.auth.$onAuth(function(authdata){
// Where should we be redirected on auth?
if(authdata){window.location.assign("/some/where");}
else if(!authdata){window.location.assign("/user/login");}
});
}]);
Methods
firewire-angular-auth exposes $rootScope.auth
which contains the firebase auth data and $rootScope.fwUser
(which connects to the fwUser service).$rootScope.auth
lets you check auth state and perform actions based on that state.$rootScope.fwUser
lets you perform all your user actions and get your users data.
fwUser.act()
fwUser.act(action, provider, id, password, changeParameter)
action : 'login', 'logout', 'changePassword', 'changeEmail', 'passwordReset'
provider : 'password', 'thirdParty','anon'
provider is needed to tell firebase which type of auth we are using eg. email / password, third party, anonymous, ...
id
id is email when using provider : 'password'. It is either 'google', 'facebook', or 'github' if using provider : 'thirdParty'
password - duh
changeParameter
Used when changing an email or password. This is either the new email or password.
Examples
All of these can be done straight from your UI. If doing them within a controller (etc.) remember to inject $rootScope and call $rootScope.fwUser.act()
login with email / password
fwUser.act('login', 'password', email, password);
Anonymous login (Do this in your js)
Remember to inject $rootScope into your controller, service, etc...
$rootScope.fwUser('login', 'anom');
login thirdParty
fwUser.act('login', 'thirdParty', 'google');
logout
fwUser.act('logout');
password reset
fwUser.act('passwordReset');
change password
fwUser.act('changePassword','password', email, oldPassword, newPassword);
change email
fwUser.act('changeEmail','password', oldEmail, Password, newEmail);
fwUser.get() vs user
If you need to get a user's data you can do either:
app.controller('myController',['$rootScope', function ($rootScope) {
$rootScope.auth.$onAuth(function(authdata){
if(authdata){$rootScope.fwUser.get();}
});
}]);
Or just user $rootScope.user
which already does the above.
Either will dip into firebase/users/{id} and return the data object from that node.
Auth states and error messages within your UI
$rootScope.auth
contains some useful utilities that you can use straight from the UI.
$rootScope.auth.loginStatus
can either be 'loggedIn', 'loggedOut', or 'trying'
Trying is useful for when a user can clicked the login button, and is waiting for a response.
$rootScope.auth.alert
- Will = 'authAlert' if there is an issue with auth.
$rootScope.auth.alertMsg
- Can be plugged in to display the error message.
Example:
.alert.alert-success(ng-show="auth.loginStatus ==='trying'") logging in...
.alert.alert-danger(ng-show="auth.alert ==='autherror'") {{auth.alertMsg}}
This will show a "logging in" message while the authStatus is neither loggedIn or loggedOut.
Furthermore an error message will be displayed if there is an error.