Skip to content

Commit fa3fc0f

Browse files
authored
Multi tenant beta (#1921)
Added multi-tenant support
1 parent 611cc9d commit fa3fc0f

31 files changed

+6264
-717
lines changed

packages/auth-types/index.d.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export interface User extends UserInfo {
5454
sendEmailVerification(
5555
actionCodeSettings?: ActionCodeSettings | null
5656
): Promise<void>;
57+
readonly tenantId: string | null;
5758
toJSON(): Object;
5859
unlink(providerId: string): Promise<User>;
5960
updateEmail(newEmail: string): Promise<void>;
@@ -74,12 +75,30 @@ export interface UserInfo {
7475
uid: string;
7576
}
7677

77-
export interface ActionCodeInfo {
78+
export class ActionCodeInfo {
79+
private constructor();
7880
data: {
7981
email?: string | null;
8082
fromEmail?: string | null;
8183
};
8284
operation: string;
85+
static Operation: {
86+
PASSWORD_RESET: Operation;
87+
RECOVER_EMAIL: Operation;
88+
EMAIL_SIGNIN: Operation;
89+
VERIFY_EMAIL: Operation;
90+
};
91+
}
92+
93+
export class ActionCodeURL {
94+
private constructor();
95+
apiKey: string;
96+
code: string;
97+
continueUrl: string | null;
98+
languageCode: string | null;
99+
operation: Operation;
100+
static parseLink(link: string): ActionCodeURL | null;
101+
tenantId: string | null;
83102
}
84103

85104
export type ActionCodeSettings = {
@@ -138,6 +157,13 @@ export interface Error {
138157
message: string;
139158
}
140159

160+
export interface AuthError extends Error {
161+
credential?: AuthCredential;
162+
email?: string;
163+
phoneNumber?: string;
164+
tenantId?: string;
165+
}
166+
141167
export class FacebookAuthProvider extends FacebookAuthProvider_Instance {
142168
static PROVIDER_ID: string;
143169
static FACEBOOK_SIGN_IN_METHOD: string;
@@ -251,6 +277,8 @@ export interface UserMetadata {
251277

252278
export type Persistence = string;
253279

280+
export type Operation = string;
281+
254282
export class OAuthCredential extends AuthCredential {
255283
private constructor();
256284
idToken?: string;
@@ -325,6 +353,7 @@ export class FirebaseAuth {
325353
signInWithPopup(provider: AuthProvider): Promise<UserCredential>;
326354
signInWithRedirect(provider: AuthProvider): Promise<void>;
327355
signOut(): Promise<void>;
356+
tenantId: string | null;
328357
updateCurrentUser(user: User | null): Promise<void>;
329358
useDeviceLanguage(): void;
330359
verifyPasswordResetCode(code: string): Promise<string>;

packages/auth/src/actioncodeinfo.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ fireauth.ActionCodeInfo = function(response) {
4444
response[fireauth.ActionCodeInfo.ServerFieldName.REQUEST_TYPE];
4545
// Email could be empty only if the request type is EMAIL_SIGNIN.
4646
if (!operation ||
47-
(operation != fireauth.ActionCodeInfo.RequestType.EMAIL_SIGNIN &&
47+
(operation != fireauth.ActionCodeInfo.Operation.EMAIL_SIGNIN &&
4848
!email)) {
4949
// This is internal only.
5050
throw new Error('Invalid provider user info!');
@@ -63,10 +63,10 @@ fireauth.ActionCodeInfo = function(response) {
6363

6464

6565
/**
66-
* Firebase Auth Action Code Info requestType possible values.
66+
* Firebase Auth Action Code Info operation possible values.
6767
* @enum {string}
6868
*/
69-
fireauth.ActionCodeInfo.RequestType = {
69+
fireauth.ActionCodeInfo.Operation = {
7070
PASSWORD_RESET: 'PASSWORD_RESET',
7171
RECOVER_EMAIL: 'RECOVER_EMAIL',
7272
EMAIL_SIGNIN: 'EMAIL_SIGNIN',

packages/auth/src/actioncodeurl.js

Lines changed: 65 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,78 +16,106 @@
1616
*/
1717

1818
/**
19-
* @fileoverview Defines firebase.auth.ActionCodeUrl class which is the utility
19+
* @fileoverview Defines firebase.auth.ActionCodeURL class which is the utility
2020
* to parse action code URLs.
2121
*/
2222

23-
goog.provide('fireauth.ActionCodeUrl');
23+
goog.provide('fireauth.ActionCodeURL');
2424

25+
goog.require('fireauth.ActionCodeInfo');
26+
goog.require('fireauth.AuthError');
27+
goog.require('fireauth.authenum.Error');
28+
goog.require('fireauth.object');
2529
goog.require('goog.Uri');
2630

2731

2832
/**
2933
* The utility class to help parse action code URLs used for out of band email
3034
* flows such as password reset, email verification, email link sign in, etc.
31-
* @param {string} actionCodeUrl The action code URL.
35+
* @param {string} actionLink The action link string.
3236
* @constructor
3337
*/
34-
fireauth.ActionCodeUrl = function(actionCodeUrl) {
35-
/** @private {!goog.Uri} The action code URL components.*/
36-
this.uri_ = goog.Uri.parse(actionCodeUrl);
38+
fireauth.ActionCodeURL = function(actionLink) {
39+
var uri = goog.Uri.parse(actionLink);
40+
var apiKey = uri.getParameterValue(
41+
fireauth.ActionCodeURL.QueryField.API_KEY) || null;
42+
var code = uri.getParameterValue(
43+
fireauth.ActionCodeURL.QueryField.CODE) || null;
44+
var mode = uri.getParameterValue(
45+
fireauth.ActionCodeURL.QueryField.MODE) || null;
46+
var operation = fireauth.ActionCodeURL.getOperation(mode);
47+
// Validate API key, code and mode.
48+
if (!apiKey || !code || !operation) {
49+
throw new fireauth.AuthError(
50+
fireauth.authenum.Error.ARGUMENT_ERROR,
51+
fireauth.ActionCodeURL.QueryField.API_KEY + ', ' +
52+
fireauth.ActionCodeURL.QueryField.CODE + 'and ' +
53+
fireauth.ActionCodeURL.QueryField.MODE +
54+
' are required in a valid action code URL.');
55+
}
56+
fireauth.object.setReadonlyProperties(this, {
57+
'apiKey': apiKey,
58+
'operation': operation,
59+
'code': code,
60+
'continueUrl': uri.getParameterValue(
61+
fireauth.ActionCodeURL.QueryField.CONTINUE_URL) || null,
62+
'languageCode': uri.getParameterValue(
63+
fireauth.ActionCodeURL.QueryField.LANGUAGE_CODE) || null,
64+
'tenantId': uri.getParameterValue(
65+
fireauth.ActionCodeURL.QueryField.TENANT_ID) || null
66+
});
3767
};
3868

3969

4070
/**
4171
* Enums for fields in URL query string.
4272
* @enum {string}
4373
*/
44-
fireauth.ActionCodeUrl.QueryField = {
74+
fireauth.ActionCodeURL.QueryField = {
4575
API_KEY: 'apiKey',
4676
CODE: 'oobCode',
47-
MODE: 'mode'
77+
CONTINUE_URL: 'continueUrl',
78+
LANGUAGE_CODE: 'languageCode',
79+
MODE: 'mode',
80+
TENANT_ID: 'tenantId'
4881
};
4982

5083

5184
/**
52-
* Enums for action code modes.
53-
* @enum {string}
85+
* Map of mode string to Action Code Info operation.
86+
* @const @private {!Object<string, !fireauth.ActionCodeInfo.Operation>}
5487
*/
55-
fireauth.ActionCodeUrl.Mode = {
56-
RESET_PASSWORD: 'resetPassword',
57-
REVOKE_EMAIL: 'recoverEmail',
58-
SIGN_IN: 'signIn',
59-
VERIFY_EMAIL: 'verifyEmail'
88+
fireauth.ActionCodeURL.ModeToOperationMap_ = {
89+
'recoverEmail': fireauth.ActionCodeInfo.Operation.RECOVER_EMAIL,
90+
'resetPassword': fireauth.ActionCodeInfo.Operation.PASSWORD_RESET,
91+
'signIn': fireauth.ActionCodeInfo.Operation.EMAIL_SIGNIN,
92+
'verifyEmail': fireauth.ActionCodeInfo.Operation.VERIFY_EMAIL
6093
};
6194

6295

6396
/**
64-
* Returns the API key parameter of action code URL.
65-
* @return {?string} The first API key value in action code URL or
66-
* undefined if apiKey does not appear in the URL.
97+
* Maps the mode string in action code URL to Action Code Info operation.
98+
* @param {?string} mode The mode string in the URL.
99+
* @return {?fireauth.ActionCodeInfo.Operation}
67100
*/
68-
fireauth.ActionCodeUrl.prototype.getApiKey = function() {
69-
return this.uri_.getParameterValue(
70-
fireauth.ActionCodeUrl.QueryField.API_KEY) || null;
71-
};
72-
101+
fireauth.ActionCodeURL.getOperation = function(mode) {
102+
if (!mode) {
103+
return null;
104+
}
105+
return fireauth.ActionCodeURL.ModeToOperationMap_[mode] || null;
73106

74-
/**
75-
* Returns the action code parameter of action code URL.
76-
* @return {?string} The first oobCode value in action code URL or
77-
* undefined if oobCode does not appear in the URL.
78-
*/
79-
fireauth.ActionCodeUrl.prototype.getCode = function() {
80-
return this.uri_.getParameterValue(
81-
fireauth.ActionCodeUrl.QueryField.CODE) || null;
82107
};
83108

84109

85110
/**
86-
* Returns the mode parameter of action code URL.
87-
* @return {?string} The first mode value in action code URL or
88-
* undefined if mode does not appear in the URL.
111+
* Returns an ActionCodeURL instance if the link is valid, otherwise null.
112+
* @param {string} actionLink The action code link string.
113+
* @return {?fireauth.ActionCodeURL}
89114
*/
90-
fireauth.ActionCodeUrl.prototype.getMode = function() {
91-
return this.uri_.getParameterValue(
92-
fireauth.ActionCodeUrl.QueryField.MODE) || null;
115+
fireauth.ActionCodeURL.parseLink = function(actionLink) {
116+
try {
117+
return new fireauth.ActionCodeURL(actionLink);
118+
} catch(e) {
119+
return null;
120+
}
93121
};

0 commit comments

Comments
 (0)