AngularFire with token authentication on Node backend server. This branch uses the mongoose node module with a mongo database to show an authorization example.
- Run these commands to add the project locally:
$ git clone https://github.com/LukeSchlangen/nodeFire
$ cd nodeFire
$ git checkout mongo-authorization
$ npm install
-
Create a free Firebase account at https://firebase.google.com
-
Create a project from your Firebase account console
-
Add a connection to firebase on your front end
-
Click the “Add Firebase to your web app” icon
-
Copy the contents WITHOUT SCRIPT TAGS or the CDN from the resulting popup into
public/config.js
. It should look like this:
```javascript
// Initialize Firebase
var config = {
apiKey: "XXXXXXXXXXXXXXXXXXXXXX",
authDomain: "XXXXXXXXXXXX.firebaseapp.com",
databaseURL: "https://XXXXXXXXXXXX.firebaseio.com",
storageBucket: "XXXXXXXXXXXX.appspot.com",
messagingSenderId: "XXXXXXXXXX"
};
firebase.initializeApp(config);
```
- Add a firebase service account to you node project
- Navigate to the Service Accounts tab in your project's settings page.
- Select your Firebase project.
- Copy your
databaseURL
from theAdmin SDK configuration snippet
. The line you need will look like this:databaseURL: "https://XXXXXXXXX.firebaseio.com"
. - Navigate to the
server/modules/decoder.js
file in the node project and replace the databaseURL. Only replace that line. It is inside of theadmin.initializeApp
:
```javascript
admin.initializeApp({
credential: admin.credential.cert("./server/firebase-service-account.json"),
databaseURL: "https://XXXXXXXXX.firebaseio.com" // replace this line with your URL
});
```
-
Return to firebase Service Accounts. Navigate to your project again if you have left it.
-
Click the Generate New Private Key button at the bottom of the Firebase Admin SDK section of the Service Accounts tab.
-
Rename the new JSON file to
firebase-service-account.json
and save it in theserver
folder of your application. -
Return to Firebase console. Configure Google as an authentication provider for your Firebase project.
-
In the Firebase console for your project (you may already be there from the previous step), click "Authentication" in left panel
-
Click "Set Up Sign-In Method" button
-
Select "Google"
-
Click the "edit" icon
-
Toggle Google to
on
-
Connect the application to your database
-
Create a new mongo database or select one you already use. In
server/modules/database-config.js
, change the connection string, currentlyvar connectionString = 'postgres://localhost:5432/sigma';
, to match the location of your database. -
Copy the queries from the
database.js
file and run them in robomongo or the mongo terminal to create the necessary collections for this project. On thedb.users.insert
, be sure to add your own name and give yourself a clearance_level from 1 to 5. This will determine what data you can see:
```javascript
db.users.insert(
[
{ email: '[email protected]', clearanceLevel: 5 },
{ email: '[email protected]', clearanceLevel: 4 }, // Your Google Email added here
{ email: '[email protected]', clearanceLevel: 2 }, // Your Other Google Email added here
{ email: '[email protected]', clearanceLevel: 3 }
]
);
```
Because you have set up google OAuth, you will need to log in with a google account (an email with @gmail.com
will work great). If you have a second google account, that will make it easy to see the differences for people with differing clearance levels.
- Run
npm start
to run your application onlocalhost:5000
- The repository is open to contribution from all interested developers. Kindly send us Pull Requests with explanation as to what changes you have done.
- Also, you can write to us by opening an Issue and also solve a current issue if possible.
- The software is registered under the MIT License