-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
🪟 🎉 Enable OAuth login #15414
Merged
🪟 🎉 Enable OAuth login #15414
Changes from 19 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
e0d6c42
Enable OAuth login
368a35d
Style buttons
2ad7b8e
Make sure to hide error wrapper without error
e8676df
Extract OAuthProviders type
996cec0
Merge branch 'master' into tim/oauth
7ba2a85
Make google login button outline more visible
14cd79e
Add provider to segment identify call
be7177a
Switch TOS checkbox by disclaimer
3ba46ad
Address review feedback
c2754d1
Merge branch 'master' into tim/oauth
734fc2d
Hide password change section for OAuth accounts
50eea48
Update airbyte-webapp/src/packages/cloud/locales/en.json
84ff5a3
Address review feedback
c480fdd
Merge branch 'master' into tim/oauth
8d080aa
Add additional flags to disable on sign-up
9310e92
Adding more tests
6fd5a83
Review feedback
e21e0e1
Fix broken linting
23cef5b
Merge branch 'master' into tim/oauth
36bbf80
Merge branch 'master' into tim/oauth
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export enum AuthProviders { | ||
GoogleIdentityPlatform = "google_identity_platform", | ||
} | ||
|
||
export type OAuthProviders = "github" | "google"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
import { User as FbUser } from "firebase/auth"; | ||
import { User as FirebaseUser } from "firebase/auth"; | ||
import React, { useCallback, useContext, useMemo, useRef } from "react"; | ||
import { useQueryClient } from "react-query"; | ||
import { useEffectOnce } from "react-use"; | ||
import { Observable, Subject } from "rxjs"; | ||
|
||
import { Action, Namespace } from "core/analytics"; | ||
import { isCommonRequestError } from "core/request/CommonRequestError"; | ||
import { useAnalyticsService } from "hooks/services/Analytics"; | ||
import useTypesafeReducer from "hooks/useTypesafeReducer"; | ||
import { AuthProviders } from "packages/cloud/lib/auth/AuthProviders"; | ||
import { AuthProviders, OAuthProviders } from "packages/cloud/lib/auth/AuthProviders"; | ||
import { GoogleAuthService } from "packages/cloud/lib/auth/GoogleAuthService"; | ||
import { User } from "packages/cloud/lib/domain/users"; | ||
import { useGetUserService } from "packages/cloud/services/users/UserService"; | ||
|
@@ -39,13 +41,18 @@ export type AuthSendEmailVerification = () => Promise<void>; | |
export type AuthVerifyEmail = (code: string) => Promise<void>; | ||
export type AuthLogout = () => Promise<void>; | ||
|
||
type OAuthLoginState = "waiting" | "loading" | "done"; | ||
|
||
interface AuthContextApi { | ||
user: User | null; | ||
inited: boolean; | ||
emailVerified: boolean; | ||
isLoading: boolean; | ||
loggedOut: boolean; | ||
providers: string[] | null; | ||
hasPasswordLogin: () => boolean; | ||
login: AuthLogin; | ||
loginWithOAuth: (provider: OAuthProviders) => Observable<OAuthLoginState>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ℹ️ This method returns an Obersvable, since we need to know in the UI, when the middle of the method starts (the user selected an account, and we start loading the user), to show the loading spinner. Thus we can't express that with a Promise. |
||
signUpWithEmailLink: (form: { name: string; email: string; password: string; news: boolean }) => Promise<void>; | ||
signUp: AuthSignUp; | ||
updatePassword: AuthUpdatePassword; | ||
|
@@ -70,10 +77,61 @@ export const AuthenticationProvider: React.FC = ({ children }) => { | |
const analytics = useAnalyticsService(); | ||
const authService = useInitService(() => new GoogleAuthService(() => auth), [auth]); | ||
|
||
/** | ||
* Create a user object in the Airbyte database from an existing Firebase user. | ||
* This will make sure that the user account is tracked in our database as well | ||
* as create a workspace for that user. This method also takes care of sending | ||
* the relevant user creation analytics events. | ||
*/ | ||
const createAirbyteUser = async ( | ||
firebaseUser: FirebaseUser, | ||
userData: { name?: string; companyName?: string; news?: boolean } = {} | ||
): Promise<User> => { | ||
// Create the Airbyte user on our server | ||
const user = await userService.create({ | ||
authProvider: AuthProviders.GoogleIdentityPlatform, | ||
authUserId: firebaseUser.uid, | ||
email: firebaseUser.email ?? "", | ||
name: userData.name ?? firebaseUser.displayName ?? "", | ||
companyName: userData.companyName ?? "", | ||
news: userData.news ?? false, | ||
}); | ||
|
||
analytics.track(Namespace.USER, Action.CREATE, { | ||
actionDescription: "New user registered", | ||
user_id: firebaseUser.uid, | ||
name: user.name, | ||
email: user.email, | ||
// Which login provider was used, e.g. "password", "google.com", "github.com" | ||
provider: firebaseUser.providerData[0]?.providerId, | ||
...getUtmFromStorage(), | ||
}); | ||
|
||
return user; | ||
}; | ||
|
||
const onAfterAuth = useCallback( | ||
async (currentUser: FbUser, user?: User) => { | ||
user ??= await userService.getByAuthId(currentUser.uid, AuthProviders.GoogleIdentityPlatform); | ||
loggedIn({ user, emailVerified: currentUser.emailVerified }); | ||
async (currentUser: FirebaseUser, user?: User) => { | ||
try { | ||
user ??= await userService.getByAuthId(currentUser.uid, AuthProviders.GoogleIdentityPlatform); | ||
loggedIn({ | ||
user, | ||
emailVerified: currentUser.emailVerified, | ||
providers: currentUser.providerData.map(({ providerId }) => providerId), | ||
}); | ||
} catch (e) { | ||
if (isCommonRequestError(e) && e.status === 404) { | ||
// If there is a firebase user but not database user we'll create a db user in this step | ||
// and retry the onAfterAuth step. This will always happen when a user logins via OAuth | ||
// the first time and we don't have a database user yet for them. In rare cases this can | ||
// also happen for email/password users if they closed their browser or got some network | ||
// errors in between creating the firebase user and the database user originally. | ||
const user = await createAirbyteUser(currentUser); | ||
await onAfterAuth(currentUser, user); | ||
} else { | ||
throw e; | ||
} | ||
} | ||
}, | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
[userService] | ||
|
@@ -103,13 +161,37 @@ export const AuthenticationProvider: React.FC = ({ children }) => { | |
isLoading: state.loading, | ||
emailVerified: state.emailVerified, | ||
loggedOut: state.loggedOut, | ||
providers: state.providers, | ||
hasPasswordLogin(): boolean { | ||
return !!state.providers?.includes("password"); | ||
}, | ||
async login(values: { email: string; password: string }): Promise<void> { | ||
await authService.login(values.email, values.password); | ||
|
||
if (auth.currentUser) { | ||
await onAfterAuth(auth.currentUser); | ||
} | ||
}, | ||
loginWithOAuth(provider): Observable<OAuthLoginState> { | ||
const state = new Subject<OAuthLoginState>(); | ||
try { | ||
state.next("waiting"); | ||
authService | ||
.loginWithOAuth(provider) | ||
.then(async () => { | ||
state.next("loading"); | ||
if (auth.currentUser) { | ||
await onAfterAuth(auth.currentUser); | ||
state.next("done"); | ||
state.complete(); | ||
} | ||
}) | ||
.catch((e) => state.error(e)); | ||
} catch (e) { | ||
state.error(e); | ||
} | ||
return state.asObservable(); | ||
}, | ||
async logout(): Promise<void> { | ||
await userService.revokeUserSession(); | ||
await authService.signOut(); | ||
|
@@ -148,7 +230,7 @@ export const AuthenticationProvider: React.FC = ({ children }) => { | |
await authService.finishResetPassword(code, newPassword); | ||
}, | ||
async signUpWithEmailLink({ name, email, password, news }): Promise<void> { | ||
let firebaseUser: FbUser; | ||
let firebaseUser: FirebaseUser; | ||
|
||
try { | ||
({ user: firebaseUser } = await authService.signInWithEmailLink(email)); | ||
|
@@ -175,29 +257,14 @@ export const AuthenticationProvider: React.FC = ({ children }) => { | |
news: boolean; | ||
}): Promise<void> { | ||
// Create a user account in firebase | ||
const { user: fbUser } = await authService.signUp(form.email, form.password); | ||
|
||
// Create the Airbyte user on our server | ||
const user = await userService.create({ | ||
authProvider: AuthProviders.GoogleIdentityPlatform, | ||
authUserId: fbUser.uid, | ||
email: form.email, | ||
name: form.name, | ||
companyName: form.companyName, | ||
news: form.news, | ||
}); | ||
const { user: firebaseUser } = await authService.signUp(form.email, form.password); | ||
|
||
// Create a user in our database for that firebase user | ||
await createAirbyteUser(firebaseUser, { name: form.name, companyName: form.companyName, news: form.news }); | ||
|
||
// Send verification mail via firebase | ||
await authService.sendEmailVerifiedLink(); | ||
|
||
analytics.track(Namespace.USER, Action.CREATE, { | ||
actionDescription: "New user registered", | ||
user_id: fbUser.uid, | ||
name: user.name, | ||
email: user.email, | ||
...getUtmFromStorage(), | ||
}); | ||
|
||
if (auth.currentUser) { | ||
await onAfterAuth(auth.currentUser); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
ℹ️ We never exported this type, and implementing the interface in TypeScript does not even help not needing to respecify the types in the
GoogleAuthService
anyway. Thus this interface is not really fulfilling any purpose (besides needing to arbitrarily type the method signature twice) and I removed it.