LOADING...

Preview

Pen ID
Unlock Campus Themeforest adv

 

Code



  
  
  



  
    

User

Log in Create User Log out Remove User

Message: {{ message }}

Error: {{ error }}

CSS
//https://material.angularjs.org/HEAD/demo/button

//https://www.firebase.com/docs/web/libraries/angular/api.html#angularfire-users-and-authentication-unauth

//https://www.firebase.com/docs/web/libraries/angular/guide/user-auth.html
JS
var app = angular.module("sampleApp", ["firebase", "ngMaterial"])
  .factory("Auth", AuthFactory)
  .controller("SampleCtrl", SampleCtrl)
  .config(function($mdThemingProvider) {
    $mdThemingProvider.theme('docs-dark').dark();
  });

// let's create a re-usable factory that generates the $firebaseAuth instance
AuthFactory.$inject = ["$firebaseAuth"];
function AuthFactory($firebaseAuth) {
  var ref = new Firebase("https://docs-sandbox.firebaseio.com");
  return $firebaseAuth(ref);
}

// and use it in our controller
SampleCtrl.$inject = ["$scope", "$log", "Auth"];
function SampleCtrl($scope, $log, Auth) {
  $scope.message = null;
  $scope.error = null;
  $scope.isLoggedIn = false;
  $scope.removeUser = removeUser;
  $scope.createUser = createUser;
  $scope.login = login;
  $scope.logout = logout;
  var offAuth = Auth.$onAuth(onAuth);
  
  init();

  function init() {
    $scope.message = null;
    $scope.error = null;
  }
  
  function onAuth(authData){
    $log.info("ONAUTH", authData)
  }

  function createUser() {
    init();
    Auth.$createUser({
      email: $scope.user.email,
      password: $scope.user.password
    }).then(function(userData) {
      $scope.message = "User created with uid: " + userData.uid;
    }).catch(_catchError);
  };

  function removeUser() {
    init();
    Auth.$removeUser({
      email: $scope.user.email,
      password: $scope.user.password
    }).then(function() {
      $scope.message = "User removed";
    }).catch(_catchError);
  };

  function login() {
    init();
    Auth.$authWithPassword({
      email: $scope.user.email,
      password: $scope.user.password
    }).then(function(authData) {
      $scope.isLoggedIn = true;
      $scope.message = "Authenticated successfully with payload:";
      console.log($scope.message, authData);
    }).catch(_catchError);
  }
  
  function logout() {
    Auth.$unauth();
    $scope.isLoggedIn = false;
    offAuth();
  }
  
  function _catchError(error){
    $scope.error = error.message;
    console.log(error);
  }
}
Host Instantly Drag and Drop Website Builder

 

Term
Wed, 11/29/2017 - 11:24

Related Codes

Pen ID
Pen ID
Pen ID