Skip to content

Commit

Permalink
feat: custom auth providers
Browse files Browse the repository at this point in the history
fixes : #28
fixes: #27
  • Loading branch information
triniwiz committed Dec 20, 2021
1 parent a3eee24 commit 8549685
Show file tree
Hide file tree
Showing 30 changed files with 316 additions and 53 deletions.
28 changes: 26 additions & 2 deletions apps/demo/src/plugin-demos/firebase-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Observable, EventData, Page, fromObject } from '@nativescript/core';
import { DemoSharedFirebaseAuth } from '@demo/shared';
import { firebase, Firebase } from '@nativescript/firebase-core';
import '@nativescript/firebase-auth';
import { Auth, User } from '@nativescript/firebase-auth';
import { Auth, User, OAuthProvider } from '@nativescript/firebase-auth';
export function navigatingTo(args: EventData) {
const page = <Page>args.object;
page.bindingContext = new DemoModel();
Expand All @@ -14,12 +14,14 @@ export class DemoModel extends DemoSharedFirebaseAuth {
user: User;
constructor() {
super();

firebase()
.auth()
.addAuthStateChangeListener((user) => {
this._setCurrentUser(user);
});
}

createUser() {
firebase()
.auth()
Expand Down Expand Up @@ -55,11 +57,33 @@ export class DemoModel extends DemoSharedFirebaseAuth {

getCurrentUser() {
const auth = firebase().auth();
this._setCurrentUser(auth?.user);
this._setCurrentUser(auth?.currentUser);
}

logOutUser() {
firebase().auth().signOut();
this._setCurrentUser(undefined);
}


loginMs() {
// https://firebase.google.com/docs/auth/android/microsoft-oauth#handle_the_sign-in_flow_with_the_firebase_sdk

const provider = new OAuthProvider('microsoft.com');
provider.addCustomParameter("prompt", "consent");
provider.addCustomParameter("login_hint", "user@firstadd.onmicrosoft.com");
provider.addCustomParameter("tenant", "TENANT_ID");

provider.setScopes(["mail.read", "calendars.read"]);

firebase()
.auth()
.signInWithProvider(provider)
.then(credentials => {

})
.catch(err => {

});
}
}
2 changes: 1 addition & 1 deletion packages/firebase-admob/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nativescript/firebase-admob",
"version": "1.0.0-alpha.25",
"version": "1.0.0-alpha.28",
"description": "NativeScript Firebase - Admob",
"main": "index",
"typings": "index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/firebase-analytics/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nativescript/firebase-analytics",
"version": "1.0.0-alpha.25",
"version": "1.0.0-alpha.28",
"description": "NativeScript Firebase - Analytics",
"main": "index",
"typings": "index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/firebase-app-check/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nativescript/firebase-app-check",
"version": "1.0.0-alpha.25",
"version": "1.0.0-alpha.28",
"description": "NativeScript Firebase - App Check",
"main": "index",
"typings": "index.d.ts",
Expand Down
12 changes: 10 additions & 2 deletions packages/firebase-auth/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export interface IUser {
getIdToken(forceRefresh?: undefined | false | true): Promise<string>;
getIdTokenResult(forceRefresh?: undefined | false | true): Promise<IAuthTokenResult>;
linkWithCredential(credential: IAuthCredential): Promise<UserCredential>;
reauthenticateWithProvider(credential: IOAuthProvider): Promise<UserCredential>;
reauthenticateWithCredential(credential: IAuthCredential): Promise<UserCredential>;
reload(): Promise<void>;
sendEmailVerification(actionCodeSettings?: IActionCodeSettings): Promise<void>;
Expand Down Expand Up @@ -110,21 +111,27 @@ export interface IOAuthCredential extends IAuthCredential {
readonly secret: string;
}

export interface IPhoneAuthCredential extends IAuthCredential {}
export interface IPhoneAuthCredential extends IAuthCredential { }

export interface UserProfileChangeRequest {
displayName?: string;
photoUri?: string;
}

export interface IOAuthProvider {
addCustomParameter(key: string, value: string);
setScopes(scopes: string[]);
credential(optionsOrIdToken: OAuthCredentialOptions | string | null, accessToken?: string);
}

export interface IAuth {
readonly app: FirebaseApp;
readonly currentUser: IUser;
readonly languageCode: string;
readonly settings: IAuthSettings;
readonly tenantId: string;

useEmulator(host: string, port: number);
useEmulator(host: string, port: number);
applyActionCode(code: string): Promise<void>;
checkActionCode(code: string): Promise<ActionCodeInfo>;
confirmPasswordReset(code: string, newPassword: string): Promise<void>;
Expand All @@ -138,6 +145,7 @@ export interface IAuth {
sendPasswordResetEmail(email: string, actionCodeSettings?: IActionCodeSettings): Promise<void>;
sendSignInLinkToEmail(email: string, actionCodeSettings?: IActionCodeSettings): Promise<void>;
signInAnonymously(): Promise<UserCredential>;
signInWithProvider(provider: IOAuthProvider): Promise<UserCredential>
signInWithCredential(credential: IAuthCredential): Promise<UserCredential>;
signInWithCustomToken(customToken: string): Promise<UserCredential>;
signInWithEmailAndPassword(email: string, password: string): Promise<UserCredential>;
Expand Down
89 changes: 86 additions & 3 deletions packages/firebase-auth/index.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ import {
OAuthCredentialOptions,
UserCredential,
UserProfileChangeRequest,
AdditionalUserInfo
AdditionalUserInfo,
IOAuthProvider
} from './common';
import lazy from '@nativescript/core/utils/lazy';
import { Application } from '@nativescript/core';
import { FirebaseAuth } from '.';

export { AdditionalUserInfo, ActionCodeInfo, ActionCodeInfoOperation, UserCredential, UserProfileChangeRequest };

Expand Down Expand Up @@ -275,6 +277,30 @@ export class User implements IUser {
});
}

reauthenticateWithProvider(provider: OAuthProvider): Promise<UserCredential> {
return new Promise((resolve, reject) => {
if (provider._isCustomProvider && provider._builder) {
org.nativescript.firebaseauth.FirebaseAuth.User.reauthenticateWithProvider(
Application.android.foregroundActivity || Application.android.startActivity,
this.native, provider._builder, new org.nativescript.firebaseauth.FirebaseAuth.Callback({
onSuccess(success) {
resolve(toUserCredential(success));
},
onError(error) {
reject({
message: error.getMessage(),
native: error,
});
},
})
)
} else {
reject(FirebaseError.fromNative(null, 'OAuthProvider not configured'));
}
})
}


reauthenticateWithCredential(credential: AuthCredential): Promise<UserCredential> {
return new Promise((resolve, reject) => {
if (!this.native) {
Expand Down Expand Up @@ -775,11 +801,45 @@ export class OAuthCredential extends AuthCredential implements IOAuthCredential
}
}

export class OAuthProvider {
#providerId: string;


export class OAuthProvider implements IOAuthProvider {
#providerId: string;
#customProvider: boolean;
#builder: com.google.firebase.auth.OAuthProvider.Builder;
constructor(providerId: string) {
this.#providerId = providerId;
this.#customProvider = false;
}

get _isCustomProvider() {
return this.#customProvider;
}

get _builder() {
return this.#builder
}

addCustomParameter(key: string, value: string) {
if (!this.#builder) {
this.#builder = com.google.firebase.auth.OAuthProvider.newBuilder(this.#providerId);
this.#customProvider = true;
}
this.#builder.addCustomParameter(key, value);
}

setScopes(scopes: string[]) {
if (!this.#builder) {
this.#builder = com.google.firebase.auth.OAuthProvider.newBuilder(this.#providerId);
this.#customProvider = true;
}
if (Array.isArray(scopes)) {
const array = new java.util.ArrayList<string>();
scopes.forEach(item => {
array.add(item);
})
this.#builder.setScopes(array);
}
}

credential(optionsOrIdToken: OAuthCredentialOptions | string | null, accessToken?: string) {
Expand Down Expand Up @@ -1053,6 +1113,29 @@ export class Auth implements IAuth {
});
}

signInWithProvider(provider: OAuthProvider): Promise<UserCredential> {
return new Promise((resolve, reject) => {
if (provider._isCustomProvider && provider._builder) {
org.nativescript.firebaseauth.FirebaseAuth.signInWithProvider(
Application.android.foregroundActivity || Application.android.startActivity,
this.native, provider._builder, new org.nativescript.firebaseauth.FirebaseAuth.Callback({
onSuccess(success) {
resolve(toUserCredential(success));
},
onError(error) {
reject({
message: error.getMessage(),
native: error,
});
},
})
)
} else {
reject(FirebaseError.fromNative(null, 'OAuthProvider not configured'));
}
})
}

signInWithCredential(credential: AuthCredential): Promise<UserCredential> {
return new Promise((resolve, reject) => {
if (!this.native) {
Expand Down
22 changes: 17 additions & 5 deletions packages/firebase-auth/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { IUser, IAuth, IAuthSettings, AdditionalUserInfo, ActionCodeInfo, ActionCodeInfoOperation, UserCredential, UserProfileChangeRequest, IUserMetadata, IUserInfo, IActionCodeSettings, IAuthCredential, IOAuthCredential, IAuthTokenResult } from './common';
import { IUser, IAuth, IAuthSettings, AdditionalUserInfo, ActionCodeInfo, ActionCodeInfoOperation, UserProfileChangeRequest, IUserMetadata, IUserInfo, IActionCodeSettings, IAuthCredential, IOAuthCredential, IAuthTokenResult } from './common';
import { Firebase, FirebaseApp } from '@nativescript/firebase-core';

export { AdditionalUserInfo, ActionCodeInfo, ActionCodeInfoOperation, UserCredential, UserProfileChangeRequest };
export { AdditionalUserInfo, ActionCodeInfo, ActionCodeInfoOperation, UserProfileChangeRequest };


export interface UserCredential {
additionalUserInfo: AdditionalUserInfo;
user: User;
credential: AuthCredential;
}


export declare class UserMetadata implements IUserMetadata {
readonly native;
Expand Down Expand Up @@ -41,6 +49,7 @@ export declare class User implements IUser {
getIdToken(forceRefresh?: undefined | false | true): Promise<string>;
getIdTokenResult(forceRefresh?: undefined | false | true): Promise<AuthTokenResult>;
linkWithCredential(credential: AuthCredential): Promise<UserCredential>;
reauthenticateWithProvider(provider: OAuthProvider): Promise<UserCredential>
reauthenticateWithCredential(credential: AuthCredential): Promise<UserCredential>;
reload(): Promise<void>;
sendEmailVerification(actionCodeSettings?: ActionCodeSettings): Promise<void>;
Expand Down Expand Up @@ -129,9 +138,10 @@ export declare class OAuthCredential extends AuthCredential implements IOAuthCre
readonly secret: string;
}

export declare class OAuthProvider {
export declare class OAuthProvider implements IOAuthProvider{
constructor(providerId: string);

addCustomParameter(key: string, value: string);
setScopes(scopes: string[]);
credential(optionsOrIdToken: OAuthCredentialOptions | string | null, accessToken?: string): OAuthCredential;
}

Expand Down Expand Up @@ -189,6 +199,8 @@ export declare class Auth implements IAuth {

signInAnonymously(): Promise<UserCredential>;

signInWithProvider(provider: OAuthProvider): Promise<UserCredential>

signInWithCredential(credential: AuthCredential): Promise<UserCredential>;

signInWithCustomToken(customToken: string): Promise<UserCredential>;
Expand All @@ -205,7 +217,7 @@ export declare class Auth implements IAuth {
}

declare module '@nativescript/firebase-core' {
export interface Firebase extends FirebaseAuth {}
export interface Firebase extends FirebaseAuth { }
}

export interface FirebaseAuth {
Expand Down
Loading

0 comments on commit 8549685

Please sign in to comment.