Skip to content
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

feat(authentication): support sign-in with redirect on Web #238

Merged
merged 8 commits into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/eleven-hats-happen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@capacitor-firebase/authentication": minor
---

feat(web): support sign-in with redirect
160 changes: 91 additions & 69 deletions packages/authentication/README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,6 @@ public void applyActionCode(PluginCall call) {
}
}

@PluginMethod
public void createUserWithEmailAndPassword(PluginCall call) {
try {
implementation.createUserWithEmailAndPassword(call);
} catch (Exception ex) {
call.reject(ex.getLocalizedMessage());
}
}

@PluginMethod
public void confirmPasswordReset(PluginCall call) {
try {
Expand All @@ -94,6 +85,15 @@ public void confirmPasswordReset(PluginCall call) {
}
}

@PluginMethod
public void createUserWithEmailAndPassword(PluginCall call) {
try {
implementation.createUserWithEmailAndPassword(call);
} catch (Exception ex) {
call.reject(ex.getLocalizedMessage());
}
}

@PluginMethod
public void getCurrentUser(PluginCall call) {
try {
Expand Down Expand Up @@ -133,6 +133,11 @@ public void error(String message) {
}
}

@PluginMethod
public void getRedirectResult(PluginCall call) {
call.reject("Not available on Android.");
}

@PluginMethod
public void getTenantId(PluginCall call) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import FirebaseAuth

public typealias AuthStateChangedObserver = () -> Void

// swiftlint:disable type_body_length
@objc public class FirebaseAuthentication: NSObject {
public var authStateObserver: AuthStateChangedObserver?
private let plugin: FirebaseAuthenticationPlugin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
// each method the plugin supports using the CAP_PLUGIN_METHOD macro.
CAP_PLUGIN(FirebaseAuthenticationPlugin, "FirebaseAuthentication",
CAP_PLUGIN_METHOD(applyActionCode, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(createUserWithEmailAndPassword, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(confirmPasswordReset, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(createUserWithEmailAndPassword, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(getCurrentUser, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(getIdToken, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(getRedirectResult, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(getTenantId, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(isSignInWithEmailLink, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(linkWithApple, CAPPluginReturnPromise);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ public class FirebaseAuthenticationPlugin: CAPPlugin {
})
}

@objc func createUserWithEmailAndPassword(_ call: CAPPluginCall) {
implementation?.createUserWithEmailAndPassword(call)
}

@objc func confirmPasswordReset(_ call: CAPPluginCall) {
guard let oobCode = call.getString("oobCode") else {
call.reject(errorOobCodeMissing)
Expand All @@ -80,6 +76,10 @@ public class FirebaseAuthenticationPlugin: CAPPlugin {
})
}

@objc func createUserWithEmailAndPassword(_ call: CAPPluginCall) {
implementation?.createUserWithEmailAndPassword(call)
}

@objc func getCurrentUser(_ call: CAPPluginCall) {
let user = implementation?.getCurrentUser()
let userResult = FirebaseAuthenticationHelper.createUserResult(user)
Expand All @@ -102,6 +102,10 @@ public class FirebaseAuthenticationPlugin: CAPPlugin {
})
}

@objc func getRedirectResult(_ call: CAPPluginCall) {
call.reject("Not available on iOS.")
}

@objc func getTenantId(_ call: CAPPluginCall) {
var result = JSObject()
result["tenantId"] = implementation?.getTenantId()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import FBSDKLoginKit
#endif

class FacebookAuthProviderHandler: NSObject {
public let errorSignInCanceled = "Sign in canceled."
public let errorLinkCanceled = "Link canceled."
let errorSignInCanceled = "Sign in canceled."
let errorLinkCanceled = "Link canceled."
private var pluginImplementation: FirebaseAuthentication
#if RGCFA_INCLUDE_FACEBOOK
private var loginManager: LoginManager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import AuthenticationServices
import GameKit

class GameCenterAuthProviderHandler: NSObject {
public let errorGetCredentialFailed = "getCredential failed."
let errorGetCredentialFailed = "getCredential failed."
private var pluginImplementation: FirebaseAuthentication

init(_ pluginImplementation: FirebaseAuthentication) {
Expand Down
63 changes: 33 additions & 30 deletions packages/authentication/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ export interface FirebaseAuthenticationPlugin {
* @since 0.2.2
*/
applyActionCode(options: ApplyActionCodeOptions): Promise<void>;
/**
* Completes the password reset process.
*
* @since 0.2.2
*/
confirmPasswordReset(options: ConfirmPasswordResetOptions): Promise<void>;
/**
* Creates a new user account with email and password.
* If the new account was created, the user is signed in automatically.
Expand All @@ -56,12 +62,6 @@ export interface FirebaseAuthenticationPlugin {
createUserWithEmailAndPassword(
options: CreateUserWithEmailAndPasswordOptions,
): Promise<SignInResult>;
/**
* Completes the password reset process.
*
* @since 0.2.2
*/
confirmPasswordReset(options: ConfirmPasswordResetOptions): Promise<void>;
/**
* Fetches the currently signed-in user.
*
Expand All @@ -74,6 +74,17 @@ export interface FirebaseAuthenticationPlugin {
* @since 0.1.0
*/
getIdToken(options?: GetIdTokenOptions): Promise<GetIdTokenResult>;
/**
* Returns the `SignInResult` from the redirect-based sign-in flow.
*
* If sign-in was unsuccessful, fails with an error.
* If no redirect operation was called, returns a `SignInResult` with a null user.
*
* Only available for Web.
*
* @since 1.3.0
*/
getRedirectResult(): Promise<SignInResult>;
/**
* Get the tenant id.
*
Expand Down Expand Up @@ -245,9 +256,7 @@ export interface FirebaseAuthenticationPlugin {
*
* @since 0.1.0
*/
signInWithApple(
options?: SignInOptions | SignInWithOAuthOptions,
): Promise<SignInResult>;
signInWithApple(options?: SignInWithOAuthOptions): Promise<SignInResult>;
/**
* Starts the Custom Token sign-in flow.
*
Expand Down Expand Up @@ -280,9 +289,7 @@ export interface FirebaseAuthenticationPlugin {
*
* @since 0.1.0
*/
signInWithFacebook(
options?: SignInOptions | SignInWithOAuthOptions,
): Promise<SignInResult>;
signInWithFacebook(options?: SignInWithOAuthOptions): Promise<SignInResult>;
/**
* Starts the Game Center sign-in flow.
*
Expand All @@ -298,25 +305,19 @@ export interface FirebaseAuthenticationPlugin {
*
* @since 0.1.0
*/
signInWithGithub(
options?: SignInOptions | SignInWithOAuthOptions,
): Promise<SignInResult>;
signInWithGithub(options?: SignInWithOAuthOptions): Promise<SignInResult>;
/**
* Starts the Google sign-in flow.
*
* @since 0.1.0
*/
signInWithGoogle(
options?: SignInOptions | SignInWithOAuthOptions,
): Promise<SignInResult>;
signInWithGoogle(options?: SignInWithOAuthOptions): Promise<SignInResult>;
/**
* Starts the Microsoft sign-in flow.
*
* @since 0.1.0
*/
signInWithMicrosoft(
options?: SignInOptions | SignInWithOAuthOptions,
): Promise<SignInResult>;
signInWithMicrosoft(options?: SignInWithOAuthOptions): Promise<SignInResult>;
/**
* Starts the sign-in flow using a phone number.
*
Expand All @@ -336,25 +337,19 @@ export interface FirebaseAuthenticationPlugin {
*
* @since 0.1.0
*/
signInWithPlayGames(
options?: SignInOptions | SignInWithOAuthOptions,
): Promise<SignInResult>;
signInWithPlayGames(options?: SignInWithOAuthOptions): Promise<SignInResult>;
/**
* Starts the Twitter sign-in flow.
*
* @since 0.1.0
*/
signInWithTwitter(
options?: SignInOptions | SignInWithOAuthOptions,
): Promise<SignInResult>;
signInWithTwitter(options?: SignInWithOAuthOptions): Promise<SignInResult>;
/**
* Starts the Yahoo sign-in flow.
*
* @since 0.1.0
*/
signInWithYahoo(
options?: SignInOptions | SignInWithOAuthOptions,
): Promise<SignInResult>;
signInWithYahoo(options?: SignInWithOAuthOptions): Promise<SignInResult>;
/**
* Starts the sign-out flow.
*
Expand Down Expand Up @@ -678,6 +673,14 @@ export interface SignInWithOAuthOptions extends SignInOptions {
* @since 1.1.0
*/
customParameters?: SignInCustomParameter[];
/**
* Whether to use the popup-based OAuth authentication flow or the full-page redirect flow.
* If you choose `redirect`, you will get the result of the call via the `authStateChange` listener after the redirect.
*
* @default 'popup'
* @since 1.3.0
*/
mode?: 'popup' | 'redirect';
/**
* Scopes to request from provider.
*
Expand Down
Loading