Create Firebase User for authentication
- Create a firebase user by going to your firebase console and in the sidenav under Develop click on the
Authenticationoption. - In Authentication go to the SIGN-IN METHOD tab and enable Email/Password.
- Then click on USERS tab and click ADD USER button. Enter an email and password.
- You can authenticate the user on the client side with this html and login function.
<section ng-if="!vm.currentUser" class="md-form">
<md-input-container layout-align="end center">
<label>Email</label>
<input ng-model="vm.newUser.email" type="email">
</md-input-container>
<md-input-container layout-align="end center">
<label>Password</label>
<input ng-model="vm.newUser.password" type="password">
</md-input-container>
<md-button class="md-raised md-primary" ng-click="vm.login(vm.newUser.email, vm.newUser.password)">Log In</md-button>
</section>
function login(email, password) {
firebase.auth().signInWithEmailAndPassword(email, password)
.catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
console.log(errorCode + ' ' + errorMessage);
// ...
})
.then(function(user) {
self.currentUser = user;
self.resetForm();
$mdDialog.hide();
})
}
-
The key here is
firebase.auth().signInWithEmailAndPassword(email, password)This specifically talks to firebase. Now there is a currentUser object that can be used to show/hide add/edit/delete data features in the client. -
To get the firebase keyword to work some code needed to be updated to initialize firebase in my app. In your firebase console under Authentication there is a WEB SETUP button where to find the special keys to your firebase db. It should look like this.
var config = {
apiKey: API_KEY,
authDomain: AUTH_DOM,
databaseURL: FIREBASE_URL,
storageBucket: STRG_BUCKET,
messagingSenderId: MSG_SND_ID
};
firebase.initializeApp(config);
- I updated my firebase library by adding these scripts to index.html.
<script src="https://cdn.firebase.com/js/client/2.3.2/firebase.js"></script>
<script src="https://www.gstatic.com/firebasejs/3.6.7/firebase.js"></script>
References
Authenticate with Firebase using Password-Based Accounts