Skip to content
This repository was archived by the owner on Oct 14, 2024. It is now read-only.

Using angularfire2 with this starter #127

Closed
beckmeindia opened this issue Jul 22, 2016 · 24 comments
Closed

Using angularfire2 with this starter #127

beckmeindia opened this issue Jul 22, 2016 · 24 comments

Comments

@beckmeindia
Copy link

Hey,
I am using Angular2 webpack with Firebase for developing my application. Hence I am using this starter to build the entire application.

I searched through documentation for using firebase or angularfire2 with this but could not find any. The present angularfire2 docs use angular-cli & don't work with this.

Could you please help?

Thanks,
Rahul

@manekinekko
Copy link

@beckmeindia what are you trying to accomplish? This starter is for Angular Universal not AngularFire.

@joaogarin
Copy link

@beckmeindia I think you might be in the wrong repo? https://github.com/angular/angularfire2
Documentation is on https://github.com/angular/angularfire2/blob/master/docs/1-install-and-setup.md

@beckmeindia
Copy link
Author

@joaogarin thank you. I was referring to the wrong docs.

@beckmeindia
Copy link
Author

beckmeindia commented Jul 26, 2016

Hey,
I am trying to integrate angularfire2 with this starter. Apparently I keep getting an error AngularFire not found, as it is running on the server side. The setup seems to be fine for webpack. I don't know how to bypass this problem of server-side and make it work with this starter.

Could any of you give ideas about a workaround for it? @manekinekko

I have attached the repository here: https://github.com/beckmeindia/universal-angularfire2

I have also tried to address this issue here: r-park/todo-angular-firebase#77 (comment)

Thanks,
Rahul

@manekinekko
Copy link

@beckmeindia I am gonna take a look at your issue tonight. I'll let you know.

@beckmeindia
Copy link
Author

Thank you so much Wassim
On 26 Jul 2016 19:34, "Wassim Chegham" [email protected] wrote:

@beckmeindia https://github.com/beckmeindia I am gonna take a look at
your issue tonight. I'll let you know.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#127 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AQo6KAxs2GjaL9_q-_UOLOyuINYrMhASks5qZhP9gaJpZM4JSrx_
.

@manekinekko
Copy link

manekinekko commented Jul 26, 2016

@beckmeindia I did some diggings. Here are my tldr: We simply need to add support for AngularFire2.

My analysis:

One of the issues is coming from the FirebaseSdkAuthBackend implementation which requests this._fbAuth = _fbApp.auth();. However, _fbApp.auth() doesn't seem to work (??); and then this.-fbAuth is NULL all the way down.

Here are the steps to reproduce this issue:

  1. instead of providing FIREBASE_PROVIDERS,
  2. provide these 2 deps:
COMMON_PROVIDERS,
{provide: AuthBackend, useClass: MockedAuthBackend}

And then provide a custom implementation with stubbed methods, something like:

class MockedAuthBackend {
   onAuth() {
     return null;
  }
}

Following the base parent class:

export declare abstract class AuthBackend {
    abstract authWithCustomToken(token: string): Promise<FirebaseAuthState>;
    abstract authAnonymously(options?: any): Promise<FirebaseAuthState>;
    abstract authWithPassword(credentials: EmailPasswordCredentials): Promise<FirebaseAuthState>;
    abstract authWithOAuthPopup(provider: AuthProviders, options?: any): Promise<firebase.auth.UserCredential>;
    abstract authWithOAuthRedirect(provider: AuthProviders, options?: any): Promise<void>;
    abstract authWithOAuthToken(credentialsObj: firebase.auth.AuthCredential, options?: any): Promise<FirebaseAuthState>;
    abstract onAuth(): Observable<FirebaseAuthState>;
    abstract getAuth(): FirebaseAuthState;
    abstract unauth(): void;
    abstract createUser(credentials: EmailPasswordCredentials): Promise<FirebaseAuthState>;
    abstract getRedirectResult(): Observable<firebase.auth.UserCredential>;
}

But then you will end up with other errors, like this one:

image

So basically, we need a universal AuthBackend implementation.

Getting @davideast in the loop ^^

@beckmeindia
Copy link
Author

beckmeindia commented Jul 27, 2016

@manekinekko Hey Wassem thanks a lot for your time and debugging. Will look forward to changes in the universal Authbackend by @davideast and try to implement what you have said & see if it somehow maybe works.

Also could I add you as a collaborator to the present repository so that we can commit new changes and maybe make it a universal-angularfire2-starter ?

Cheers,
Rahul

@manekinekko
Copy link

@beckmeindia sure

@beckmeindia beckmeindia reopened this Aug 1, 2016
@beckmeindia
Copy link
Author

hey @manekinekko & @davideast
Any updates for the auth implementation?
Really excited to implement it on my website!

@MarkPieszak
Copy link
Contributor

MarkPieszak commented Aug 18, 2016

Others interested in this as well. aspnet/JavaScriptServices#268
Hopefully we can get this in for 1.0 if it's not too involving? @davideast

@beckmeindia
Copy link
Author

beckmeindia commented Aug 24, 2016

Hi, I observed that the new angular2-webpack-starter has a very simple approach for integrating Angularfire2 through this:
add 'firebase' to tsconfig.json file.
install typescript 2.0.0 npm install [email protected] --save
install angularfire with npm install angularfire2@next -- save
add "firebase" to types array in tsconfig in the module
{
"compilerOptions": {
"declaration": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": ["es6", "dom"],
"mapRoot": "./",
"module": "es6",
"moduleResolution": "node",
"outDir": "../dist/out-tsc",
"sourceMap": true,
"target": "es5",
"typeRoots": [
"../node_modules/@types"
],
"types": [
"jasmine",
"firebase"
]
}
}

I have observed that the universal-starter uses an earlier version of angular2-webpack-starter.
Could you suggest a way to upgrade to the new angular2-webpack?
@davideast @MarkPieszak @manekinekko

@beckmeindia
Copy link
Author

Hey @manekinekko
Does the new update to rc6 solves the problem we were facing with Auth earlier?

Excited to hear about it

@jeffwhelpley
Copy link

@manekinekko what is the current status of this issue? I am just catching up on open problems and it seems like there was some back and forth on this as we were playing catchup with the angular release. Can you check again on this when you get a chance and let me know?

@beckmeindia
Copy link
Author

I just checked with the updated versions of angularfire2 and angular-universal-nodejs.

It works perfectly fine with all the observables and authentication. So it's resolved.

Thank you so much guys!
@manekinekko @jeffwhelpley @MarkPieszak

@doczoidberg
Copy link

is anywhere a starter project for angular universal with angularfire? Is am still struggling with it

@joaogarin
Copy link

Does universal play well with angularfire2? I see from angular/angularfire#191 that it looks like some custom code is needed and using the firebase nodejs version on the server..is that so?

@manekinekko
Copy link

@doczoidberg @joaogarin can you share some logs or error messages?

@joaogarin
Copy link

I didn't actually try it yet..but I am thinking about using firebase, and if that is a good idea at this point..I will definitely be using universal.

I will try to get some tests running and see what happens. I will report back with what I find.

@joaogarin
Copy link

joaogarin commented Feb 12, 2017

Hello,

Just out of curiosity I tried putting angularfire2 into this repository and I get the following error :

Error: Unexpected value '[object Object]' imported by the module 'AppModule'
    at /Library/WebServer/Documents/universal-starter/dist/server/index.js:20332

This was trying to put the Angularfire2 module into the appModule. I guess it could be done via the browsermodule, but then its not possible to inject the angularfire2 service into components that ideally would be render on client and server..basically would be like maintaining two separate apps for server and client which is not so interesting maybe.

I think it would be super interesting to have even if just a recommendation on the universal repo on how to use angularfire2 with angular using universal, best practices or something like that.

@samvloeberghs
Copy link

samvloeberghs commented Mar 19, 2017

@joaogarin instead of importing AngularFireModule you could provide a substitute for AngularFire service in your app.node.module.

class AngularFireServer{
  constructor(){}
  ...
}
export function createAngularFireServer(){
  return new AngularFireServer();
}

@NgModule({
  bootstrap: [SiteComponent],
  imports: [
       ... // module imports
  ]
  providers: [
       ...
       {provide: 'req', useFactory: getRequest},
       {provide: AngularFire, useFactory: createAngularFireServer},
       ...
   ]
})

I mainly use AngularFire / Firebase for Auth.
In this AngularFireServer I use the req to get a cookie and do authentication with the JWT in it..

My AngularFireServer roughly looks like this:

import { Inject } from '@angular/core';
import { ReplaySubject } from 'rxjs/ReplaySubject';

export class AngularFireServer {

  auth$ = new ReplaySubject(1);
  auth = this.auth$.asObservable();

  constructor(@Inject('req') private req: any) {

    let token = this.req.cookies['token'];
    let user;
    if (token) {
      user = {
        auth: {
          getToken: () => {
            return new Promise((resolve) => {
              resolve(token)
            });
          }
        }
      };
    }

    this.auth$.next(user);

  }
  
}

@joaogarin
Copy link

Yeah thats what I am doing now, essencialy have a provider for browser and server =) bit of DI swap magic and it works 👍

@michaelwaihenya
Copy link

+1. Any update on the possibility of using Universal with angularfire2 4.0.0-rc.0? There is a reference to window in database.js that does not seem to allow this.

@samvloeberghs
Copy link

I didn't tries with dB, but auth seems to work fine

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants