-
Notifications
You must be signed in to change notification settings - Fork 2.2k
feat(auth): Add auth and unauth methods #53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@jteplitz602 great PR! It brings up some questions of how to best abstract Firebase. The current implementation essentially proxies to Firebase SDK methods, so any component in the app that would want to log in would need to know the right method and configuration to use to log in. // This would probably be imported from a config, or injected
const provider:AuthProviders = AuthProviders.Github;
@Component({
selector: 'my-login'
})
export class MyLogin {
constructor(private _af:AngularFire) {}
login () {
this._af.authWithOAuthPopup(provider);
}
logout () {
this._af.unauth()
}
} Could you explore moving some of the options to injectable configuration, and make the auth service just have bootstrap(App, [
FIREBASE_PROVIDERS,
defaultFirebase('http://somefirebase'),
defaultAuthProvider(AuthProviders.Github),
// We could even have a default if we find that one method is far more common than others
defaultAuthMethod(AuthMethods.PopUp)
]);
@Component({
template: `
<button (click)="af.auth.login()">Login with Github</button>
<button (click)="twitterLogin()">Login with Twitter</button>
`
})
class App {
constructor(public af:AngularFire) {}
twitterLogin() {
// Would still use default method of PopUp
this.af.auth.login({provider: AuthProviders.Twitter});
}
} Maybe you've already given this approach of "fewer methods+more configurability" some thought and have some reasons why it provides a worse user experience. |
@jeffbcross In general I love the idea of reducing the number of public methods, but I'm not sure it will work here. That design works well for popup auth, redirect auth, and anonymous auth. But it runs into problems with customToken, email, and oauthToken login. These methods all require specific arguments. It feels weird to me that your call to We could separate the firebase login methods into two groups: automatic login (popup, redirect, and anonymous) and manual login (customToken, email, and oauthToken). We could provide one generic login method for the first group and then three manual login methods for the second group, but that might be overkill. What do you think? |
I've never actually implemented Firebase's login, please forgive my naivety! The auth service seems to be inherently stateful, and the internal details of providers, credentials, and tokens are nearly constant through the entire life of the app. Could we possibly have a separate means of providing options (like custom OAuth tokens, passwords/emails, etc.) with some kind of I'm assuming the Please correct anything I've got backwards! |
@TylerEich thanks for the input! // Providers to give bootstrap
provide(DefaultFirebaseAuthProvider, AuthProviders.Github),
provide(DefaultFirebaseAuthMethod, AuthMethods.Popup)
// to login
auth.login(); However, all of these options can be overwritten by an optional config object that gets passed into // Providers to bootstrap
provide(DefaultFirebaseAuthProvider, AuthProviders.Github),
provide(DefaultFirebaseAuthMethod, AuthMethods.Popup)
// this will login the user with Facebook popup since Facebook will override Github, but nothing overwrote Popup
auth.login({
provider: AuthProviders.Facebook
});
auth.login({
method: AuthMethods.Password
}, {
email: 'test@example.com',
password: 'password'
}) |
@jteplitz602 +1 I think we could drop the |
Talked to @jeffbcross and @hansl a bit more. We're going to create one config provider, and have two signatures for the login method. I'll write up a longer description this afternoon. |
@jteplitz602 @jeffbcross yeah, I like that design better. Keeps the API clean and centralized 😎👍 |
Here's a more complete overview of the design that we settled on: You can, but are not required to, provide a single set of configuration arguments to DI like this: bootstrap(app, [
FIREBASE_PROVIDERS,
defaultFirebase('https://<some-firebase>.firebaseio.com'),
firebaseAuthConfig({
provider: AuthProviders.Facebook,
method: AuthMethods.Popup,
remember: 'default',
scope: ['email']
})
]); Once you've done that you can simply call // This will perform popup auth with google oauth and the scope will be email
auth.login({
provider: AuthProviders.Google
}); The Auth service will use all values provided by the given configuration object over those provided through DI, and will fall back to the DI provided config object (if available) for any options not provided by the caller. Additionally, if you're using OAuthToken, CustomToken, or Password auth then you can pass a credentials object to login like so: bootstrap(app, [
FIREBASE_PROVIDERS,
defaultFirebase('https://<some-firebase>.firebaseio.com'),
firebaseAuthConfig({
method: AuthMethods.Password,
remember: 'default'
})
]);
auth.login({
email: 'test@example.com',
password: 'password'
}); If you want to pass both a credentials object and a configuration object you can do so like this: auth.login({
email: 'test@example.com',
password: 'password'
}, {
method: AuthMethods.Password,
remember: 'sessionOnly'
}); If two arguments are passed to login(config?: AuthConfiguration): Promise<FirebaseAuthState>;
login(credentials: AuthCredentials, config?: AuthConfiguration): Promise<FirebaseAuthState>; The first signature is used for Popup, Redirect, and anonymous auth. The second signature is used for @jeffbcross does that sound like a good overview of everything we discussed? |
0df3233
to
a01add1
Compare
README.md
Outdated
@@ -1,4 +1,4 @@ | |||
# AngularFire2 | |||
# AngularFire3 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
haha what?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got ahead of myself 😉
There's a vim key that increments numbers. I guess I hit it by accident...
src/providers/auth.ts
Outdated
} | ||
config = this._mergeConfigs(config); | ||
|
||
if (!config.hasOwnProperty('method')) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replace all hasOwnProperty instances with something inheritance-friendly.
LGTM with comments addressed |
Part of #4