Skip to content

Instantly share code, notes, and snippets.

@ripsware
Created September 15, 2016 10:26
Show Gist options
  • Save ripsware/e3e167035f3cbc5a3543342e823df8a7 to your computer and use it in GitHub Desktop.
Save ripsware/e3e167035f3cbc5a3543342e823df8a7 to your computer and use it in GitHub Desktop.
Registrasi User di firebase menggunakan fungsi "createUserWithEmailAndPassword"
// Requirement Firebase JS SDK
(function(){
'use strict';
angular.module('ContohRegisterFirebase', [
'ionic',
'FirebaseService' // include dari file firebase-service.js
])
.controller('RegisterPageCtrl', ['$scope', '$firebase', '$ionicPopup',
function($scope, $firebase, $ionicPopup){
$scope.models = {
newUser: {
email: 'tester@domain.com',
password: 'P@ssw0rd'
}
};
$scope.register = function(newUserInfo){
$firebase.createUser(newUserInfo,
function(userInfo){
$ionicPopup.alert({
title: 'Registration Success',
template: 'Successfully created user account for ' + userInfo.email
}).then(function(){
newUserInfo.email = null;
newUserInfo.password = null;
});
}, function(error){
$ionicPopup.alert({
title: 'Registration Failed',
template: error.message
});
}
);
}
// Panggil fungsi registrasi di controller ini, bisa di taro di dalam submit event
$scope.register($scope.models.newUser);
}
])
;
})();
(function(){
var config = {
apiKey: "[FIREBASE_API_KEY]", // Firebase API Key
authDomain: "[AUTH_DOMAIN]", // Auth domain
databaseURL: "[FIREBASE_URL]", // Firebase URL
storageBucket: "[STORAGE_BUCKET]" // Storage bucket
};
var firebase = null;
angular.module('FirebaseService', [])
.factory('$firebase', [function(){
return {
get instance (){
if (!fireBase) {
fireBase = firebase.initializeApp(config);
}
return fireBase;
},
createUser: function(userInfo, successCallback, errorCallback){
this.instance.auth().createUserWithEmailAndPassword(userInfo.email, userInfo.password)
.then(function(userData){
if(successCallback && successCallback instanceof Function){
successCallback.apply(this, [userInfo]);
}
}, function(error){
if(errorCallback && errorCallback instanceof Function){
var msg = "";
switch (err.code) {
case 'auth/email-already-in-use':
msg = 'The new user account cannot be created because the email is already in use.';
break;
case 'auth/invalid-email':
msg = 'The specified email is not a valid email.';
break;
case 'auth/weak-password':
msg = 'The password is not strong enough';
break;
default:
msg = 'Error creating user';
}
errorCallback.apply(this, [{message: msg, errorCode: err.code}]);
}
})
;
}
};
}])
;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment