angularjs - Angular & Firebase $_SESSION like php? -
im trying membership process angular , firebase.
is there solution, instead of keeping user information in local storage , session storage?
i not want keep local storage etc. sessionstorage. think because not safe.of course,i not keep sensitive information.i need more information on subject.
current login , signup coding.. think right way ? "sorry bad english.."
app.controller('signup',function($scope, $timeout,$sessionstorage, $window, sessions){ $scope.success = false; $scope.signup = function(){ var ref = new firebase('https://<myfirebase>.firebaseio.com/'); ref.createuser({ email : $scope.loginemail, password : $scope.loginpass }, function(error, userdata) { if (error) { console.log("error creating user:", error); $scope.error = true; $scope.errortext = error; alert(error); } else { console.log("successfully created user account uid:", userdata.uid); $scope.success = true; $timeout(function(){ $scope.success = false; },4000); } }); } $scope.login = function(){ var ref = new firebase("https://<myfirebase>.firebaseio.com/"); ref.authwithpassword({ email : $scope.loginemail, password : $scope.loginpass }, function(error, authdata) { if (error) { console.log("login failed!", error); } else { console.log("authenticated payload:", authdata); $window.sessionstorage.userid = authdata.uid; $window.sessionstorage.login = true; } }); } $scope.online = $window.sessionstorage.getitem('login'); });
you need create services store user data,
authservice.js
(function(){ 'use strict'; angular .module('app.auth') /** * authservice going handle of our auth functions, don't need write them inside controllers. */ .factory('authservice', authservice); function authservice($firebaseauth, $firebaseobject, $firebasearray, $state, $firebaseref){ var authuser = $firebaseauth($firebaseref.default); return { /* function receives email, password, name , creates new user after user created stores user details in db. */ signupemail: function(newemail, newpassword, newfullname){ /** * here we're using angular-fire $createuser create new user, passing email, password , * full name. * * after we're creating record in db in "userprofile" node, remember, * creating user doesn't show him/her in db, need create record ourselves. * * , catching errors might happen :p */ authuser.$createuser({ email: newemail, password: newpassword, fullname: newfullname, }).then(function(authdata){ authuser.$authwithpassword({ "email": newemail, "password": newpassword }).then (function(authdata){ $firebaseref.default.child("userprofile").child(authdata.uid).set({ name: newfullname, email: newemail, }); $state.go('menu.inicio'); }); }).catch(function(error){ switch (error.code) { case "email_taken": alert("bro, someone's using email!"); break; case "invalid_email": alert("dude, not email address!"); break; default: alert("error creating user:", error); } }); }, /** * here login our user in, user angular-fire $authwithpassword assing email , password. * after send user our dashboard. */ loginuser: function(email, password){ authuser.$authwithpassword({ "email": email, "password": password }).then (function(authdata){ $state.go('menu.inicio'); }).catch(function(error){ console.log(error); }); }, logoutuser: function(){ authuser.$unauth(); $state.go('login'); }, userprofiledata: function(userid){ var userprofileref = $firebaseref.default.child('userprofile').child(userid); return $firebaseobject(userprofileref); } } } })();
call authcontroller.js:
(function(){ 'use strict'; angular .module('app.auth') .controller('loginctrl', loginctrl); /** * create our controller , inject authservice can connect firebase. */ loginctrl.$inject = ['$scope', '$state', 'authservice']; function loginctrl($scope, $state, authservice){ // create variable called 'data', asign empty object , bind scope, handle form data. $scope.data = {}; /** * our function pretty simple, username , password form, , send our auth service, that's it. * auth service take care of else you! * @return {[type]} [description] */ $scope.loginemail = function(loginform){ if (loginform.$valid) { var email = $scope.data.email; var password = $scope.data.password; authservice.loginuser(email, password); }; } } })();
and configure routes , app modules: mainmodule.js
(function(){ 'use strict'; angular .module('app', [ /* place core , shared modules */ 'app.core', /* place features modules, auth. */ 'app.auth', ]); })();
coremodule.js
(function(){ 'use strict'; angular .module('app.core', [ 'firebase', ]); angular .module('app.core') .run(['$rootscope', '$state', function( $rootscope, $state) { /*cath stateerror un-authenticated users */ $rootscope.$on("$statechangeerror", function(event, tostate, toparams, fromstate, fromparams, error){ if (error === "auth_required") { $state.go('login'); }; }); }]) .config(function($firebaserefprovider) { $firebaserefprovider.registerurl('https://<url>.firebaseio.com/'); }); })();
finally when call route declare resolve statement:
routes.js
(function(){ angular .module('app.core') .config(['$stateprovider', '$urlrouterprovider', function($stateprovider, $urlrouterprovider) { $stateprovider .state('login', { url: '/login', templateurl: 'app/auth/login/login.html', controller: 'loginctrl', }) .state('mytablewithinfo', { url: '/mwinfo', resolve: { user: function($firebaseauthservice) { return $firebaseauthservice.$requireauth(); } }, views: { 'my-view-name': { templateurl: 'app/core/templates/mypage.html', controller: 'myctrl', } } }) ; $urlrouterprovider.otherwise('login'); }]); })();
and when create service call onauth "$_session" info
ref.onauth(function(authdata) { if (authdata) { // user signed in! uid = authdata.uid; } });
Comments
Post a Comment