angularfire icon indicating copy to clipboard operation
angularfire copied to clipboard

5.2.1 AngularFireAuthGuard causes TypeError: source.lift is not a function

Open katowulf opened this issue 7 years ago • 39 comments

Performing basic docs for AngularFireAuthGuard produces an error related to source.lift.

Version info

sputnik:testauthguard katowulf$ npm list --depth 0
[email protected] /Users/katowulf/projects/testauthguard
├── @angular-devkit/[email protected]
├── @angular/[email protected]
├── @angular/[email protected]
├── @angular/[email protected]
├── @angular/[email protected]
├── @angular/[email protected]
├── @angular/[email protected]
├── @angular/[email protected]
├── @angular/[email protected]
├── @angular/[email protected]
├── @angular/[email protected]
├── @angular/[email protected]
├── @angular/[email protected]
├── @types/[email protected]
├── @types/[email protected]
├── @types/[email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
└── [email protected]

sputnik:testauthguard katowulf$ ng version

Angular CLI: 7.1.4
Node: 8.9.3
OS: darwin x64
Angular: 7.1.4
... animations, cli, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.11.4
@angular-devkit/build-angular     0.11.4
@angular-devkit/build-optimizer   0.11.4
@angular-devkit/build-webpack     0.11.4
@angular-devkit/core              7.1.4
@angular-devkit/schematics        7.1.4
@angular/fire                     5.2.1
@ngtools/webpack                  7.1.4
@schematics/angular               7.1.4
@schematics/update                0.11.4
rxjs                              6.3.3
typescript                        3.1.6
webpack                           4.23.1

package.json

{
  "name": "testauthguard",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~7.1.0",
    "@angular/common": "~7.1.0",
    "@angular/compiler": "~7.1.0",
    "@angular/core": "~7.1.0",
    "@angular/fire": "^5.2.1",
    "@angular/forms": "~7.1.0",
    "@angular/platform-browser": "~7.1.0",
    "@angular/platform-browser-dynamic": "~7.1.0",
    "@angular/router": "~7.1.0",
    "core-js": "^2.5.4",
    "firebase": "^6.1.0",
    "rxjs": "~6.3.3",
    "tslib": "^1.9.0",
    "zone.js": "~0.8.26"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.11.0",
    "@angular/cli": "~7.1.0",
    "@angular/compiler-cli": "~7.1.0",
    "@angular/language-service": "~7.1.0",
    "@types/node": "~8.9.4",
    "@types/jasmine": "~2.8.8",
    "@types/jasminewd2": "~2.0.3",
    "codelyzer": "~4.5.0",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~3.1.1",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.1",
    "karma-jasmine": "~1.1.2",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.4.0",
    "ts-node": "~7.0.0",
    "tslint": "~5.11.0",
    "typescript": "~3.1.6"
  }
}

How to reproduce these conditions

Failing test unit, Plunkr, or JSFiddle demonstrating the problem

testauthguard.zip Gist of app.module.ts and app-routing.module.ts here.

Steps to set up and reproduce

  1. Download and decompress zip file
  2. npm install
  3. npm start

How I created the contents of the zip:

ng new testauthguard
cd testauthguard
npm install @angular/fire firebase --save
ng generate component test
ng generate component login

Then I modified auth-routing.module.ts as indicated in the docs for AuthGuard.

Sample data and security rules

n/a

Debug output

** Errors in the JavaScript console **

ERROR Error: Uncaught (in promise): TypeError: source.lift is not a function
TypeError: source.lift is not a function
    at mapOperation (map.js:9)
    at pipe.js:18
    at Array.reduce (<anonymous>)
    at piped (pipe.js:18)
    at AngularFireAuthGuard.push../node_modules/@angular/fire/auth-guard/auth-guard.js.AngularFireAuthGuard.canActivate (auth-guard.js:23)
    at router.js:3097
    at Observable._subscribe (defer.js:9)
    at Observable.push../node_modules/rxjs/_esm5/internal/Observable.js.Observable._trySubscribe (Observable.js:43)
    at Observable.push../node_modules/rxjs/_esm5/internal/Observable.js.Observable.subscribe (Observable.js:29)
    at TakeOperator.push../node_modules/rxjs/_esm5/internal/operators/take.js.TakeOperator.call (take.js:24)
    at resolvePromise (zone.js:831)
    at resolvePromise (zone.js:788)
    at zone.js:892
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:423)
    at Object.onInvokeTask (core.js:16147)
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:422)
    at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (zone.js:195)
    at drainMicroTaskQueue (zone.js:601)
defaultErrorLogger @ core.js:14597
push../node_modules/@angular/core/fesm5/core.js.ErrorHandler.handleError @ core.js:14645
next @ core.js:16628
schedulerFn @ core.js:12609
push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.__tryOrUnsub @ Subscriber.js:196
push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.next @ Subscriber.js:134
push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber._next @ Subscriber.js:77
push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next @ Subscriber.js:54
push../node_modules/rxjs/_esm5/internal/Subject.js.Subject.next @ Subject.js:47
push../node_modules/@angular/core/fesm5/core.js.EventEmitter.emit @ core.js:12593
(anonymous) @ core.js:16178
push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke @ zone.js:391
push../node_modules/zone.js/dist/zone.js.Zone.run @ zone.js:150
push../node_modules/@angular/core/fesm5/core.js.NgZone.runOutsideAngular @ core.js:16115
onHandleError @ core.js:16178
push../node_modules/zone.js/dist/zone.js.ZoneDelegate.handleError @ zone.js:395
push../node_modules/zone.js/dist/zone.js.Zone.runGuarded @ zone.js:164
_loop_1 @ zone.js:694
api.microtaskDrainDone @ zone.js:703
drainMicroTaskQueue @ zone.js:608
Promise.then (async)
scheduleMicroTask @ zone.js:584
push../node_modules/zone.js/dist/zone.js.ZoneDelegate.scheduleTask @ zone.js:413
push../node_modules/zone.js/dist/zone.js.Zone.scheduleTask @ zone.js:238
push../node_modules/zone.js/dist/zone.js.Zone.scheduleMicroTask @ zone.js:258
scheduleResolveOrReject @ zone.js:879
ZoneAwarePromise.then @ zone.js:1012
push../node_modules/@angular/core/fesm5/core.js.PlatformRef.bootstrapModule @ core.js:16660
./src/main.ts @ main.ts:11
__webpack_require__ @ bootstrap:78
0 @ main.ts:12
__webpack_require__ @ bootstrap:78
checkDeferredModules @ bootstrap:45
webpackJsonpCallback @ bootstrap:32
(anonymous) @ main.js:1

** Output from firebase.database().enableLogging(true); **

n/a

** Screenshots **

Screen Shot 2019-06-02 at 1 21 13 PM

Expected behavior

Adding Auth guard should not generate an error for such a simple repro directly copying the docs.

Actual behavior

Addition of Auth guard causes error. Commenting it out removes the error.

katowulf avatar Jun 02 '19 20:06 katowulf

Hmmm, I'll check this out, thanks for the repro Kato.

jamesdaniels avatar Jun 03 '19 10:06 jamesdaniels

If I use ... canActivate (redirectUnauthorizedToLogin) will work perfectly. But it is only working on localhost. When the build is done --prod does not work The route guardian is allowing access to the routes

lucaswillemen avatar Jun 03 '19 19:06 lucaswillemen

ng serve localhost - work ng build localhost - work ng build firebase - work ng build --prod localhost - fail ng build --prod firebase - fail

lucaswillemen avatar Jun 03 '19 19:06 lucaswillemen

i got the same error msg when using angularfireauthguard even when using ... canActivate (redirectUnauthorizedToLogin)

justAnormalUser00 avatar Jun 07 '19 09:06 justAnormalUser00

I have the same issue either with canActivate or authGuardPipe.

oespirilla avatar Jun 08 '19 13:06 oespirilla

Having the same issue

tariva avatar Jun 09 '19 17:06 tariva

I have the same issue

htmike avatar Jun 10 '19 17:06 htmike

Also having the same issue

lmiguel-96 avatar Jun 10 '19 17:06 lmiguel-96

У меня такая же ошибка при добавлении data: { authGuardPipe: redirectUnauthorizedToLogin }

babtsoualiaksandr avatar Jun 11 '19 13:06 babtsoualiaksandr

...canActivate(redirectUnauthorizedToLogin) используя это всё решает

babtsoualiaksandr avatar Jun 11 '19 14:06 babtsoualiaksandr

was so excited to implement the new and simplified auth guard... and that excitement landed me here with no solution , hope it gets resolved soon

JP1016 avatar Jun 12 '19 19:06 JP1016

I'll be looking at this this week. Will aim to cut 5.2.2 with a fix for AOT ASAP.

jamesdaniels avatar Jun 12 '19 20:06 jamesdaniels

I worked in this way

const redirectUnauthorizedToLogin = () => redirectUnauthorizedTo(["login"]);

const routes: Routes = [
  {
    path: "home",
    children: [],
    canActivate: [AngularFireAuthGuard],
    data: { authGuardPipe: redirectUnauthorizedToLogin }
  }
]

lisp719 avatar Jun 19 '19 04:06 lisp719

Great lisp719 ! Your solution worked for me too! Thanks!!!!

victor2105 avatar Jun 19 '19 22:06 victor2105

I was also able to get this working using the canActivate helper.

const redirectUnauthorizedToLogin = redirectUnauthorizedTo(['login']);

const routes: Routes = [
  { path: '', redirectTo: 'home', pathMatch: 'full' },
  { path: 'home', loadChildren: './home/home.module#HomePageModule', ...canActivate(redirectUnauthorizedToLogin) },
  { path: 'login', loadChildren: './login/login.module#LoginPageModule' },
];

EDIT: This actually failed to work when running in production mode. I seemed to have the same issue noted here: https://github.com/angular/angularfire2/issues/2114

For now the solution provided by @lisp719 works the best for me, even in production mode.

devinshoemaker avatar Jul 02 '19 12:07 devinshoemaker

These work locally and prod/SSR 👍

const isUser = () => pipe(map(user => {
  return !!user ? !!user : ['/'];
}));

const isAdmin = () => pipe(customClaims, map(claims => {
  return claims.admin === true ? claims.admin : ['/'];
}));

const routes: Routes = [
  { path: '', component: HomeComponent },
  { path: 'login', component: LoginComponent },
  {
    path: 'admin',
    component: AdminComponent,
    canActivate: [AngularFireAuthGuard],
    data: { authGuardPipe: isAdmin }
  },
  {
    path: 'profile',
    component: ProfileComponent,
    canActivate: [AngularFireAuthGuard],
    data: { authGuardPipe: isUser }
  },
  { path: '**', component: ErrorComponent }
];

jrodl3r avatar Jul 06 '19 18:07 jrodl3r

I worked in this way

const redirectUnauthorizedToLogin = () => redirectUnauthorizedTo(["login"]);

const routes: Routes = [
  {
    path: "home",
    children: [],
    canActivate: [AngularFireAuthGuard],
    data: { authGuardPipe: redirectUnauthorizedToLogin }
  }
]

Great, thanks for your help!

fabiothz avatar Jul 28 '19 14:07 fabiothz

Im trying to use the AF AuthGuard and I'm trying to protect a route from not logged in users and not admin users. To redirect the users when not logged in I use const redirectUnauthorizedToLogin = () => redirectUnauthorizedTo(['login']); but how do I choose where to redirect the users when not admin?

ix-xerri avatar Aug 10 '19 08:08 ix-xerri

What about redirectLoggedInTo. How can I implement this?

n3nikita avatar Aug 10 '19 16:08 n3nikita

I had same issue and is not resolved by solution proposed, my routes are:

const redirectUnauthorizedToLogin = redirectUnauthorizedTo(['access']);

const appRoutes: Routes = [
    {
        path: 'access',
        loadChildren: () => import('./main/authentication/auth.module').then(m => m.AuthModule),
    },
    {
        path: '',
        loadChildren: () => import('./main/pages/pages.module').then( m => m.PagesModule),
        ...canActivate(redirectUnauthorizedToLogin)
    },
    {
        path        : '**',
        redirectTo: 'access'
    }
];

AuthModule and PagesModule had GuardRedirects also. Everything worked perfectly while serving, but when build with prod configuration guards did not trigger, neither this way or this other: canActivate: [AngularFireAuthGuard], data: { authGuardPipe: redirectUnauthorizedToLogin}

As the only difference between scenarios is build options, i tryed by test/error removing options. Removing this two finally worked: "aot": true, "buildOptimizer": true,

jcuervas avatar Aug 29 '19 13:08 jcuervas