├── src ├── assets │ ├── .gitkeep │ ├── btc.jpg │ └── avatar.svg ├── app │ ├── app.component.sass │ ├── super-secret │ │ ├── super-secret.component.sass │ │ ├── super-secret.component.html │ │ └── super-secret.component.ts │ ├── user-login │ │ ├── user-login.component.sass │ │ ├── user-login.component.ts │ │ └── user-login.component.html │ ├── subscriber-page │ │ ├── subscriber-page.component.sass │ │ ├── subscriber-page.component.html │ │ └── subscriber-page.component.ts │ ├── app.component.html │ ├── core │ │ ├── user.ts │ │ ├── core.module.ts │ │ ├── admin.guard.ts │ │ ├── can-read.guard.ts │ │ └── auth.service.ts │ ├── app.component.ts │ ├── app-routing.module.ts │ └── app.module.ts ├── environments │ ├── environment.prod.ts │ └── environment.ts ├── favicon.ico ├── typings.d.ts ├── tsconfig.app.json ├── tsconfig.spec.json ├── main.ts ├── index.html ├── styles.sass ├── test.ts └── polyfills.ts ├── e2e ├── app.po.ts ├── tsconfig.e2e.json └── app.e2e-spec.ts ├── .editorconfig ├── README.md ├── tsconfig.json ├── protractor.conf.js ├── .gitignore ├── karma.conf.js ├── package.json ├── .angular-cli.json └── tslint.json /src/assets/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/app/app.component.sass: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/app/super-secret/super-secret.component.sass: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/app/user-login/user-login.component.sass: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/app/subscriber-page/subscriber-page.component.sass: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/environments/environment.prod.ts: -------------------------------------------------------------------------------- 1 | export const environment = { 2 | production: true 3 | }; 4 | -------------------------------------------------------------------------------- /src/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AngularFirebase/75-role-based-auth-firestore/HEAD/src/favicon.ico -------------------------------------------------------------------------------- /src/assets/btc.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AngularFirebase/75-role-based-auth-firestore/HEAD/src/assets/btc.jpg -------------------------------------------------------------------------------- /src/app/app.component.html: -------------------------------------------------------------------------------- 1 |
2 | 3 | 4 |
-------------------------------------------------------------------------------- /src/typings.d.ts: -------------------------------------------------------------------------------- 1 | /* SystemJS module definition */ 2 | declare var module: NodeModule; 3 | interface NodeModule { 4 | id: string; 5 | } 6 | -------------------------------------------------------------------------------- /src/app/super-secret/super-secret.component.html: -------------------------------------------------------------------------------- 1 |

Admin Page Bitcoins

2 | 3 |

Keep all of your bitcoins here

4 | 5 | -------------------------------------------------------------------------------- /src/app/core/user.ts: -------------------------------------------------------------------------------- 1 | 2 | export interface Roles { 3 | subscriber?: boolean; 4 | editor?: boolean; 5 | admin?: boolean; 6 | } 7 | 8 | export interface User { 9 | uid: string; 10 | email: string; 11 | roles: Roles; 12 | } 13 | -------------------------------------------------------------------------------- /e2e/app.po.ts: -------------------------------------------------------------------------------- 1 | import { browser, by, element } from 'protractor'; 2 | 3 | export class AppPage { 4 | navigateTo() { 5 | return browser.get('/'); 6 | } 7 | 8 | getParagraphText() { 9 | return element(by.css('app-root h1')).getText(); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/tsconfig.app.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "../out-tsc/app", 5 | "baseUrl": "./", 6 | "module": "es2015", 7 | "types": [] 8 | }, 9 | "exclude": [ 10 | "test.ts", 11 | "**/*.spec.ts" 12 | ] 13 | } 14 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # Editor configuration, see http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | charset = utf-8 6 | indent_style = space 7 | indent_size = 2 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | 11 | [*.md] 12 | max_line_length = off 13 | trim_trailing_whitespace = false 14 | -------------------------------------------------------------------------------- /e2e/tsconfig.e2e.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "../out-tsc/e2e", 5 | "baseUrl": "./", 6 | "module": "commonjs", 7 | "target": "es5", 8 | "types": [ 9 | "jasmine", 10 | "jasminewd2", 11 | "node" 12 | ] 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Episode 76 - Role-Based Access Control Firestore 2 | 3 | Watch the [screencast](https://angularfirebase.com/lessons/role-based-authorization-with-firestore-nosql-and-angular-5) 4 | 5 | ## Usage 6 | 7 | - `git clone` 8 | - create the `src/enviornments/environment.ts` file and add your firebase config to it 9 | - `npm install` 10 | - `ng serve` 11 | -------------------------------------------------------------------------------- /e2e/app.e2e-spec.ts: -------------------------------------------------------------------------------- 1 | import { AppPage } from './app.po'; 2 | 3 | describe('base App', () => { 4 | let page: AppPage; 5 | 6 | beforeEach(() => { 7 | page = new AppPage(); 8 | }); 9 | 10 | it('should display welcome message', () => { 11 | page.navigateTo(); 12 | expect(page.getParagraphText()).toEqual('Welcome to app!'); 13 | }); 14 | }); 15 | -------------------------------------------------------------------------------- /src/app/super-secret/super-secret.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit } from '@angular/core'; 2 | 3 | @Component({ 4 | selector: 'super-secret', 5 | templateUrl: './super-secret.component.html', 6 | styleUrls: ['./super-secret.component.sass'] 7 | }) 8 | export class SuperSecretComponent implements OnInit { 9 | 10 | constructor() { } 11 | 12 | ngOnInit() { 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/tsconfig.spec.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "../out-tsc/spec", 5 | "baseUrl": "./", 6 | "module": "commonjs", 7 | "target": "es5", 8 | "types": [ 9 | "jasmine", 10 | "node" 11 | ] 12 | }, 13 | "files": [ 14 | "test.ts" 15 | ], 16 | "include": [ 17 | "**/*.spec.ts", 18 | "**/*.d.ts" 19 | ] 20 | } 21 | -------------------------------------------------------------------------------- /src/main.ts: -------------------------------------------------------------------------------- 1 | import { enableProdMode } from '@angular/core'; 2 | import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; 3 | 4 | import { AppModule } from './app/app.module'; 5 | import { environment } from './environments/environment'; 6 | 7 | if (environment.production) { 8 | enableProdMode(); 9 | } 10 | 11 | platformBrowserDynamic().bootstrapModule(AppModule) 12 | .catch(err => console.log(err)); 13 | -------------------------------------------------------------------------------- /src/environments/environment.ts: -------------------------------------------------------------------------------- 1 | // The file contents for the current environment will overwrite these during build. 2 | // The build system defaults to the dev environment which uses `environment.ts`, but if you do 3 | // `ng build --env=prod` then `environment.prod.ts` will be used instead. 4 | // The list of which env maps to which file can be found in `.angular-cli.json`. 5 | 6 | export const environment = { 7 | production: false 8 | }; 9 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compileOnSave": false, 3 | "compilerOptions": { 4 | "outDir": "./dist/out-tsc", 5 | "sourceMap": true, 6 | "declaration": false, 7 | "moduleResolution": "node", 8 | "emitDecoratorMetadata": true, 9 | "experimentalDecorators": true, 10 | "target": "es5", 11 | "typeRoots": [ 12 | "node_modules/@types" 13 | ], 14 | "lib": [ 15 | "es2017", 16 | "dom" 17 | ] 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/app/core/core.module.ts: -------------------------------------------------------------------------------- 1 | import { NgModule } from '@angular/core'; 2 | import { CommonModule } from '@angular/common'; 3 | import { AuthService } from './auth.service'; 4 | import { AdminGuard } from './admin.guard'; 5 | import { CanReadGuard } from './can-read.guard'; 6 | 7 | @NgModule({ 8 | imports: [ 9 | CommonModule 10 | ], 11 | declarations: [], 12 | providers: [AuthService, AdminGuard, CanReadGuard] 13 | }) 14 | export class CoreModule { } 15 | -------------------------------------------------------------------------------- /src/app/user-login/user-login.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit } from '@angular/core'; 2 | import { AuthService } from '../core/auth.service'; 3 | 4 | @Component({ 5 | selector: 'user-login', 6 | templateUrl: './user-login.component.html', 7 | styleUrls: ['./user-login.component.sass'] 8 | }) 9 | export class UserLoginComponent implements OnInit { 10 | 11 | canEdit; 12 | 13 | constructor(public auth: AuthService) { } 14 | 15 | ngOnInit() { 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/app/subscriber-page/subscriber-page.component.html: -------------------------------------------------------------------------------- 1 |
2 |

{{ post.title }}

3 |

{{ post.content }}

4 | 5 | 11 | 12 | 18 | 19 |
-------------------------------------------------------------------------------- /src/app/app.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit } from '@angular/core'; 2 | import { AngularFirestore, AngularFirestoreCollection } from 'angularfire2/firestore'; 3 | import { AngularFireAuth } from 'angularfire2/auth'; 4 | import { Observable } from 'rxjs/Observable'; 5 | 6 | 7 | @Component({ 8 | selector: 'app-root', 9 | templateUrl: './app.component.html', 10 | styleUrls: ['./app.component.sass'] 11 | }) 12 | export class AppComponent implements OnInit { 13 | 14 | 15 | constructor() { } 16 | 17 | ngOnInit() { } 18 | 19 | 20 | } -------------------------------------------------------------------------------- /src/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Base 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /src/styles.sass: -------------------------------------------------------------------------------- 1 | /* You can add global styles to this file, and also import other style files */ 2 | .btn-social 3 | color: #fff 4 | i 5 | margin-right: 5px 6 | &:hover 7 | color: #fff 8 | box-shadow: 0 14px 26px -12px rgba(0, 0, 0, 0.2), 0 4px 23px 0px rgba(0, 0, 0, 0.12), 0 8px 10px -5px rgba(0, 0, 0, 0.1) 9 | .btn-block 10 | margin-bottom: 10px 11 | 12 | .btn-google 13 | background-color: #dd4b39 14 | 15 | .content 16 | display: flex 17 | align-content: center 18 | justify-content: center 19 | flex-direction: column; 20 | padding: 10vh 20vw; -------------------------------------------------------------------------------- /src/app/app-routing.module.ts: -------------------------------------------------------------------------------- 1 | import { NgModule } from '@angular/core'; 2 | import { Routes, RouterModule } from '@angular/router'; 3 | 4 | import { SuperSecretComponent } from './super-secret/super-secret.component'; 5 | import { SubscriberPageComponent } from './subscriber-page/subscriber-page.component'; 6 | 7 | import { AdminGuard } from './core/admin.guard'; 8 | import { CanReadGuard } from './core/can-read.guard'; 9 | 10 | 11 | const routes: Routes = [ 12 | { path: 'content', component: SubscriberPageComponent, canActivate: [CanReadGuard] }, 13 | { path: 'secret', component: SuperSecretComponent, canActivate: [AdminGuard] } 14 | ]; 15 | 16 | @NgModule({ 17 | imports: [RouterModule.forRoot(routes)], 18 | exports: [RouterModule] 19 | }) 20 | export class AppRoutingModule { } 21 | -------------------------------------------------------------------------------- /protractor.conf.js: -------------------------------------------------------------------------------- 1 | // Protractor configuration file, see link for more information 2 | // https://github.com/angular/protractor/blob/master/lib/config.ts 3 | 4 | const { SpecReporter } = require('jasmine-spec-reporter'); 5 | 6 | exports.config = { 7 | allScriptsTimeout: 11000, 8 | specs: [ 9 | './e2e/**/*.e2e-spec.ts' 10 | ], 11 | capabilities: { 12 | 'browserName': 'chrome' 13 | }, 14 | directConnect: true, 15 | baseUrl: 'http://localhost:4200/', 16 | framework: 'jasmine', 17 | jasmineNodeOpts: { 18 | showColors: true, 19 | defaultTimeoutInterval: 30000, 20 | print: function() {} 21 | }, 22 | onPrepare() { 23 | require('ts-node').register({ 24 | project: 'e2e/tsconfig.e2e.json' 25 | }); 26 | jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } })); 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See http://help.github.com/ignore-files/ for more about ignoring files. 2 | 3 | # /src/environments/environment.prod.ts 4 | # /src/environments/environment.ts 5 | NOTES.md 6 | functions/node_modules 7 | 8 | /src/env.ts 9 | 10 | # compiled output 11 | /dist 12 | /tmp 13 | /out-tsc 14 | 15 | # dependencies 16 | /node_modules 17 | 18 | # IDEs and editors 19 | /.idea 20 | .project 21 | .classpath 22 | .c9/ 23 | *.launch 24 | .settings/ 25 | *.sublime-workspace 26 | 27 | # IDE - VSCode 28 | .vscode/* 29 | !.vscode/settings.json 30 | !.vscode/tasks.json 31 | !.vscode/launch.json 32 | !.vscode/extensions.json 33 | 34 | # misc 35 | /.sass-cache 36 | /connect.lock 37 | /coverage 38 | /libpeerconnection.log 39 | npm-debug.log 40 | testem.log 41 | /typings 42 | 43 | # e2e 44 | /e2e/*.js 45 | /e2e/*.map 46 | 47 | # System Files 48 | .DS_Store 49 | Thumbs.db 50 | -------------------------------------------------------------------------------- /src/app/core/admin.guard.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@angular/core'; 2 | import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router'; 3 | import { Observable } from 'rxjs/Observable'; 4 | import { AuthService } from './auth.service'; 5 | import { tap, map, take } from 'rxjs/operators'; 6 | 7 | @Injectable() 8 | export class AdminGuard implements CanActivate { 9 | 10 | constructor(private auth: AuthService) {} 11 | 12 | canActivate( 13 | next: ActivatedRouteSnapshot, 14 | state: RouterStateSnapshot): Observable { 15 | 16 | return this.auth.user$.pipe( 17 | take(1), 18 | map(user => user && user.roles.admin ? true : false), 19 | tap(isAdmin => { 20 | if (!isAdmin) { 21 | console.error('Access denied - Admins only') 22 | } 23 | }) 24 | ); 25 | 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/app/core/can-read.guard.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@angular/core'; 2 | import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router'; 3 | import { Observable } from 'rxjs/Observable'; 4 | import { AuthService } from './auth.service'; 5 | import { tap, map, take } from 'rxjs/operators'; 6 | 7 | @Injectable() 8 | export class CanReadGuard implements CanActivate { 9 | 10 | constructor(private auth: AuthService) {} 11 | 12 | canActivate( 13 | next: ActivatedRouteSnapshot, 14 | state: RouterStateSnapshot): Observable { 15 | 16 | return this.auth.user$.pipe( 17 | take(1), 18 | map(user => user && this.auth.canRead(user) ? true : false), 19 | tap(canView => { 20 | if (!canView) { 21 | console.error('Access denied. Must have permission to view content') 22 | } 23 | }) 24 | ); 25 | 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/app/subscriber-page/subscriber-page.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit } from '@angular/core'; 2 | import { AngularFirestore, AngularFirestoreCollection } from 'angularfire2/firestore'; 3 | import { AuthService } from '../core/auth.service'; 4 | 5 | @Component({ 6 | selector: 'subscriber-page', 7 | templateUrl: './subscriber-page.component.html', 8 | styleUrls: ['./subscriber-page.component.sass'] 9 | }) 10 | export class SubscriberPageComponent implements OnInit { 11 | 12 | postRef; 13 | post$; 14 | user; 15 | 16 | constructor(private afs: AngularFirestore, public auth: AuthService) { 17 | this.auth.user$.subscribe(user => this.user = user) 18 | } 19 | 20 | ngOnInit() { 21 | this.postRef = this.afs.doc('posts/myTestPost') 22 | this.post$ = this.postRef.valueChanges() 23 | } 24 | 25 | editPost() { 26 | this.postRef.update({ title: 'Edited Title!'}) 27 | } 28 | 29 | 30 | deletePost() { 31 | this.postRef.delete() 32 | } 33 | 34 | 35 | } 36 | -------------------------------------------------------------------------------- /karma.conf.js: -------------------------------------------------------------------------------- 1 | // Karma configuration file, see link for more information 2 | // https://karma-runner.github.io/1.0/config/configuration-file.html 3 | 4 | module.exports = function (config) { 5 | config.set({ 6 | basePath: '', 7 | frameworks: ['jasmine', '@angular/cli'], 8 | plugins: [ 9 | require('karma-jasmine'), 10 | require('karma-chrome-launcher'), 11 | require('karma-jasmine-html-reporter'), 12 | require('karma-coverage-istanbul-reporter'), 13 | require('@angular/cli/plugins/karma') 14 | ], 15 | client:{ 16 | clearContext: false // leave Jasmine Spec Runner output visible in browser 17 | }, 18 | coverageIstanbulReporter: { 19 | reports: [ 'html', 'lcovonly' ], 20 | fixWebpackSourcePaths: true 21 | }, 22 | angularCli: { 23 | environment: 'dev' 24 | }, 25 | reporters: ['progress', 'kjhtml'], 26 | port: 9876, 27 | colors: true, 28 | logLevel: config.LOG_INFO, 29 | autoWatch: true, 30 | browsers: ['Chrome'], 31 | singleRun: false 32 | }); 33 | }; 34 | -------------------------------------------------------------------------------- /src/test.ts: -------------------------------------------------------------------------------- 1 | // This file is required by karma.conf.js and loads recursively all the .spec and framework files 2 | 3 | import 'zone.js/dist/long-stack-trace-zone'; 4 | import 'zone.js/dist/proxy.js'; 5 | import 'zone.js/dist/sync-test'; 6 | import 'zone.js/dist/jasmine-patch'; 7 | import 'zone.js/dist/async-test'; 8 | import 'zone.js/dist/fake-async-test'; 9 | import { getTestBed } from '@angular/core/testing'; 10 | import { 11 | BrowserDynamicTestingModule, 12 | platformBrowserDynamicTesting 13 | } from '@angular/platform-browser-dynamic/testing'; 14 | 15 | // Unfortunately there's no typing for the `__karma__` variable. Just declare it as any. 16 | declare const __karma__: any; 17 | declare const require: any; 18 | 19 | // Prevent Karma from running prematurely. 20 | __karma__.loaded = function () {}; 21 | 22 | // First, initialize the Angular testing environment. 23 | getTestBed().initTestEnvironment( 24 | BrowserDynamicTestingModule, 25 | platformBrowserDynamicTesting() 26 | ); 27 | // Then we find all the tests. 28 | const context = require.context('./', true, /\.spec\.ts$/); 29 | // And load the modules. 30 | context.keys().map(context); 31 | // Finally, start Karma to run the tests. 32 | __karma__.start(); 33 | -------------------------------------------------------------------------------- /src/app/app.module.ts: -------------------------------------------------------------------------------- 1 | import { BrowserModule } from '@angular/platform-browser'; 2 | import { NgModule } from '@angular/core'; 3 | 4 | import { AppRoutingModule } from './app-routing.module'; 5 | import { AppComponent } from './app.component'; 6 | 7 | import { AngularFireModule } from 'angularfire2'; 8 | import { environment } from '../environments/environment'; 9 | 10 | import { AngularFirestoreModule } from 'angularfire2/firestore'; 11 | import { AngularFireAuthModule } from 'angularfire2/auth'; 12 | 13 | /// DELETE firebaseConfig 14 | /// Add your own firebase config to environment.ts 15 | /// Then use it to initialize angularfire2 AngularFireModule.initializeApp(environment.firebaseConfig), 16 | import { firebaseConfig } from '../env'; 17 | import { SuperSecretComponent } from './super-secret/super-secret.component'; 18 | import { UserLoginComponent } from './user-login/user-login.component'; 19 | 20 | import { CoreModule } from './core/core.module'; 21 | import { SubscriberPageComponent } from './subscriber-page/subscriber-page.component'; 22 | 23 | @NgModule({ 24 | declarations: [ 25 | AppComponent, 26 | SuperSecretComponent, 27 | UserLoginComponent, 28 | SubscriberPageComponent 29 | ], 30 | imports: [ 31 | BrowserModule, 32 | AppRoutingModule, 33 | CoreModule, 34 | AngularFireModule.initializeApp(firebaseConfig), 35 | AngularFirestoreModule, 36 | AngularFireAuthModule 37 | ], 38 | providers: [], 39 | bootstrap: [AppComponent] 40 | }) 41 | export class AppModule { } 42 | -------------------------------------------------------------------------------- /src/app/user-login/user-login.component.html: -------------------------------------------------------------------------------- 1 |
2 | 3 |
4 | 5 |
6 | 7 | 8 | Super Secret Page 9 | 10 | 11 | 12 | Subscriber Page 13 | 14 | 15 | 16 | 17 | 18 |

Howdy, GUEST

19 |

Login to get started...

20 | 21 | 24 |
25 | 26 | 27 | 28 |
29 |

Howdy, {{ user.displayName }}

30 | 31 |

UID: {{ user.uid }}

32 | 33 | 34 |
35 | 36 | Subscriber: 37 | 38 | {{ user.roles?.subscriber }} 39 |
40 | 41 | Editor: 42 | 43 | {{ user.roles?.editor }} 44 |
45 | 46 | Admin: 47 | 48 | {{ user.roles?.admin }} 49 |
50 | 51 | 52 | 53 |
54 | 55 | 56 |
57 | 58 |
-------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "base", 3 | "version": "0.0.0", 4 | "license": "MIT", 5 | "scripts": { 6 | "ng": "ng", 7 | "start": "ng serve", 8 | "build": "ng build", 9 | "test": "ng test", 10 | "lint": "ng lint", 11 | "e2e": "ng e2e" 12 | }, 13 | "private": true, 14 | "dependencies": { 15 | "@angular/animations": "^5.1.0", 16 | "@angular/common": "^5.1.0", 17 | "@angular/compiler": "^5.1.0", 18 | "@angular/core": "^5.1.0", 19 | "@angular/forms": "^5.1.0", 20 | "@angular/http": "^5.1.0", 21 | "@angular/platform-browser": "^5.1.0", 22 | "@angular/platform-browser-dynamic": "^5.1.0", 23 | "@angular/platform-server": "^5.1.0", 24 | "@angular/router": "^5.1.0", 25 | "@angular/service-worker": "^5.0.0", 26 | "angularfire2": "^5.0.0-rc.4", 27 | "core-js": "^2.4.1", 28 | "firebase": "^4.6.2", 29 | "rxjs": "^5.5.5", 30 | "zone.js": "^0.8.14" 31 | }, 32 | "devDependencies": { 33 | "@angular/cli": "^1.5.3", 34 | "@angular/compiler-cli": "^5.1.0", 35 | "@angular/language-service": "^4.2.4", 36 | "@types/jasmine": "~2.5.53", 37 | "@types/jasminewd2": "~2.0.2", 38 | "@types/node": "~6.0.60", 39 | "codelyzer": "~3.2.0", 40 | "jasmine-core": "~2.6.2", 41 | "jasmine-spec-reporter": "~4.1.0", 42 | "karma": "~1.7.0", 43 | "karma-chrome-launcher": "~2.1.1", 44 | "karma-cli": "~1.0.1", 45 | "karma-coverage-istanbul-reporter": "^1.2.1", 46 | "karma-jasmine": "~1.1.0", 47 | "karma-jasmine-html-reporter": "^0.2.2", 48 | "protractor": "~5.1.2", 49 | "ts-node": "~3.2.0", 50 | "tslint": "~5.7.0", 51 | "typescript": "^2.4.2" 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /.angular-cli.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "./node_modules/@angular/cli/lib/config/schema.json", 3 | "project": { 4 | "name": "base" 5 | }, 6 | "apps": [ 7 | { 8 | "root": "src", 9 | "outDir": "dist", 10 | "assets": [ 11 | "assets", 12 | "favicon.ico" 13 | ], 14 | "index": "index.html", 15 | "main": "main.ts", 16 | "polyfills": "polyfills.ts", 17 | "test": "test.ts", 18 | "tsconfig": "tsconfig.app.json", 19 | "testTsconfig": "tsconfig.spec.json", 20 | "prefix": "", 21 | "serviceWorker": false, 22 | "styles": [ 23 | "styles.sass" 24 | ], 25 | "scripts": [], 26 | "environmentSource": "environments/environment.ts", 27 | "environments": { 28 | "dev": "environments/environment.ts", 29 | "prod": "environments/environment.prod.ts" 30 | } 31 | } 32 | ], 33 | "e2e": { 34 | "protractor": { 35 | "config": "./protractor.conf.js" 36 | } 37 | }, 38 | "lint": [ 39 | { 40 | "project": "src/tsconfig.app.json", 41 | "exclude": "**/node_modules/**" 42 | }, 43 | { 44 | "project": "src/tsconfig.spec.json", 45 | "exclude": "**/node_modules/**" 46 | }, 47 | { 48 | "project": "e2e/tsconfig.e2e.json", 49 | "exclude": "**/node_modules/**" 50 | } 51 | ], 52 | "test": { 53 | "karma": { 54 | "config": "./karma.conf.js" 55 | } 56 | }, 57 | "defaults": { 58 | "styleExt": "sass", 59 | "class": { 60 | "spec": false 61 | }, 62 | "component": { 63 | "spec": false 64 | }, 65 | "directive": { 66 | "spec": false 67 | }, 68 | "guard": { 69 | "spec": false 70 | }, 71 | "module": { 72 | "spec": false 73 | }, 74 | "pipe": { 75 | "spec": false 76 | }, 77 | "service": { 78 | "spec": false 79 | } 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/app/core/auth.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@angular/core'; 2 | import { Router } from '@angular/router'; 3 | import * as firebase from 'firebase/app'; 4 | import { AngularFireAuth } from 'angularfire2/auth'; 5 | import { AngularFirestore, AngularFirestoreDocument } from 'angularfire2/firestore'; 6 | import { Observable } from 'rxjs/Observable'; 7 | import { switchMap } from 'rxjs/operators'; 8 | import { User } from './user'; 9 | 10 | @Injectable() 11 | export class AuthService { 12 | 13 | user$: Observable; 14 | 15 | constructor(private afAuth: AngularFireAuth, 16 | private afs: AngularFirestore, 17 | private router: Router) { 18 | //// Get auth data, then get firestore user document || null 19 | this.user$ = this.afAuth.authState 20 | .switchMap(user => { 21 | if (user) { 22 | return this.afs.doc(`users/${user.uid}`).valueChanges() 23 | } else { 24 | return Observable.of(null) 25 | } 26 | }) 27 | } 28 | 29 | 30 | ///// Login/Signup ////// 31 | 32 | googleLogin() { 33 | const provider = new firebase.auth.GoogleAuthProvider() 34 | return this.oAuthLogin(provider); 35 | } 36 | 37 | private oAuthLogin(provider) { 38 | return this.afAuth.auth.signInWithPopup(provider) 39 | .then((credential) => { 40 | this.updateUserData(credential.user) 41 | }) 42 | } 43 | 44 | signOut() { 45 | this.afAuth.auth.signOut() 46 | } 47 | 48 | private updateUserData(user) { 49 | // Sets user data to firestore on login 50 | const userRef: AngularFirestoreDocument = this.afs.doc(`users/${user.uid}`); 51 | const data: User = { 52 | uid: user.uid, 53 | email: user.email, 54 | roles: { 55 | subscriber: true 56 | } 57 | } 58 | return userRef.set(data, { merge: true }) 59 | } 60 | 61 | 62 | ///// Role-based Authorization ////// 63 | 64 | canRead(user: User): boolean { 65 | const allowed = ['admin', 'editor', 'subscriber'] 66 | return this.checkAuthorization(user, allowed) 67 | } 68 | 69 | canEdit(user: User): boolean { 70 | const allowed = ['admin', 'editor'] 71 | return this.checkAuthorization(user, allowed) 72 | } 73 | 74 | canDelete(user: User): boolean { 75 | const allowed = ['admin'] 76 | return this.checkAuthorization(user, allowed) 77 | } 78 | 79 | 80 | 81 | // determines if user has matching role 82 | private checkAuthorization(user: User, allowedRoles: string[]): boolean { 83 | if (!user) return false 84 | for (const role of allowedRoles) { 85 | if ( user.roles[role] ) { 86 | return true 87 | } 88 | } 89 | return false 90 | } 91 | 92 | 93 | } 94 | -------------------------------------------------------------------------------- /src/polyfills.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * This file includes polyfills needed by Angular and is loaded before the app. 3 | * You can add your own extra polyfills to this file. 4 | * 5 | * This file is divided into 2 sections: 6 | * 1. Browser polyfills. These are applied before loading ZoneJS and are sorted by browsers. 7 | * 2. Application imports. Files imported after ZoneJS that should be loaded before your main 8 | * file. 9 | * 10 | * The current setup is for so-called "evergreen" browsers; the last versions of browsers that 11 | * automatically update themselves. This includes Safari >= 10, Chrome >= 55 (including Opera), 12 | * Edge >= 13 on the desktop, and iOS 10 and Chrome on mobile. 13 | * 14 | * Learn more in https://angular.io/docs/ts/latest/guide/browser-support.html 15 | */ 16 | 17 | /*************************************************************************************************** 18 | * BROWSER POLYFILLS 19 | */ 20 | 21 | /** IE9, IE10 and IE11 requires all of the following polyfills. **/ 22 | // import 'core-js/es6/symbol'; 23 | // import 'core-js/es6/object'; 24 | // import 'core-js/es6/function'; 25 | // import 'core-js/es6/parse-int'; 26 | // import 'core-js/es6/parse-float'; 27 | // import 'core-js/es6/number'; 28 | // import 'core-js/es6/math'; 29 | // import 'core-js/es6/string'; 30 | // import 'core-js/es6/date'; 31 | // import 'core-js/es6/array'; 32 | // import 'core-js/es6/regexp'; 33 | // import 'core-js/es6/map'; 34 | // import 'core-js/es6/weak-map'; 35 | // import 'core-js/es6/set'; 36 | 37 | /** IE10 and IE11 requires the following for NgClass support on SVG elements */ 38 | // import 'classlist.js'; // Run `npm install --save classlist.js`. 39 | 40 | /** IE10 and IE11 requires the following for the Reflect API. */ 41 | // import 'core-js/es6/reflect'; 42 | 43 | 44 | /** Evergreen browsers require these. **/ 45 | // Used for reflect-metadata in JIT. If you use AOT (and only Angular decorators), you can remove. 46 | import 'core-js/es7/reflect'; 47 | 48 | 49 | /** 50 | * Required to support Web Animations `@angular/platform-browser/animations`. 51 | * Needed for: All but Chrome, Firefox and Opera. http://caniuse.com/#feat=web-animation 52 | **/ 53 | // import 'web-animations-js'; // Run `npm install --save web-animations-js`. 54 | 55 | 56 | 57 | /*************************************************************************************************** 58 | * Zone JS is required by Angular itself. 59 | */ 60 | import 'zone.js/dist/zone'; // Included with Angular CLI. 61 | 62 | 63 | 64 | /*************************************************************************************************** 65 | * APPLICATION IMPORTS 66 | */ 67 | 68 | /** 69 | * Date, currency, decimal and percent pipes. 70 | * Needed for: All but Chrome, Firefox, Edge, IE11 and Safari 10 71 | */ 72 | // import 'intl'; // Run `npm install --save intl`. 73 | /** 74 | * Need to import at least one locale-data with intl. 75 | */ 76 | // import 'intl/locale-data/jsonp/en'; 77 | -------------------------------------------------------------------------------- /tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "rulesDirectory": [ 3 | "node_modules/codelyzer" 4 | ], 5 | "rules": { 6 | "callable-types": true, 7 | "class-name": true, 8 | "comment-format": [ 9 | true, 10 | "check-space" 11 | ], 12 | "curly": false, 13 | "eofline": false, 14 | "forin": true, 15 | "import-blacklist": [true, "rxjs"], 16 | "import-spacing": true, 17 | "indent": [ 18 | true, 19 | "spaces" 20 | ], 21 | "interface-over-type-literal": true, 22 | "label-position": true, 23 | "max-line-length": [ 24 | true, 25 | 140 26 | ], 27 | "member-access": false, 28 | "member-ordering": [ 29 | true, 30 | "static-before-instance", 31 | "variables-before-functions" 32 | ], 33 | "no-arg": true, 34 | "no-bitwise": true, 35 | "no-console": [ 36 | true, 37 | "debug", 38 | "info", 39 | "time", 40 | "timeEnd", 41 | "trace" 42 | ], 43 | "no-construct": true, 44 | "no-debugger": true, 45 | "no-duplicate-variable": true, 46 | "no-empty": false, 47 | "no-empty-interface": true, 48 | "no-eval": true, 49 | "no-inferrable-types": [true, "ignore-params"], 50 | "no-shadowed-variable": true, 51 | "no-string-literal": false, 52 | "no-string-throw": true, 53 | "no-switch-case-fall-through": true, 54 | "no-trailing-whitespace": false, 55 | "no-unused-expression": true, 56 | "no-use-before-declare": true, 57 | "no-var-keyword": true, 58 | "object-literal-sort-keys": false, 59 | "one-line": [ 60 | true, 61 | "check-open-brace", 62 | "check-catch", 63 | "check-else", 64 | "check-whitespace" 65 | ], 66 | "prefer-const": true, 67 | "quotemark": [ 68 | true, 69 | "single" 70 | ], 71 | "radix": true, 72 | "semicolon": [ 73 | "always" 74 | ], 75 | "triple-equals": [ 76 | true, 77 | "allow-null-check" 78 | ], 79 | "typedef-whitespace": [ 80 | true, 81 | { 82 | "call-signature": "nospace", 83 | "index-signature": "nospace", 84 | "parameter": "nospace", 85 | "property-declaration": "nospace", 86 | "variable-declaration": "nospace" 87 | } 88 | ], 89 | "typeof-compare": true, 90 | "unified-signatures": true, 91 | "variable-name": false, 92 | "whitespace": [ 93 | true, 94 | "check-branch", 95 | "check-decl", 96 | "check-operator", 97 | "check-separator", 98 | "check-type" 99 | ], 100 | 101 | "directive-selector": [true, "attribute", "", "camelCase"], 102 | "component-selector": [true, "element", "", "kebab-case"], 103 | "use-input-property-decorator": true, 104 | "use-output-property-decorator": true, 105 | "use-host-property-decorator": true, 106 | "no-input-rename": true, 107 | "no-output-rename": true, 108 | "use-life-cycle-interface": true, 109 | "use-pipe-transform-interface": true, 110 | "component-class-suffix": true, 111 | "directive-class-suffix": true, 112 | "no-access-missing-member": true, 113 | "templates-use-public": true, 114 | "invoke-injectable": true 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /src/assets/avatar.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | 7 | 9 | 10 | 11 | 12 | 13 | 14 | 16 | 17 | 18 | 20 | 21 | 22 | 24 | 25 | 26 | 28 | 29 | 30 | 33 | 34 | 35 | 71 | 72 | 73 | 75 | 76 | 77 | 79 | 80 | 81 | 84 | 85 | 86 | 92 | 93 | 94 | 96 | 97 | 98 | 100 | 101 | 102 | 105 | 106 | 107 | 110 | 111 | 112 | 115 | 116 | 117 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | --------------------------------------------------------------------------------