realm
Version:
Realm by MongoDB is an offline-first mobile database: an alternative to SQLite and key-value stores
128 lines • 5.94 kB
JavaScript
;
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2022 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////
Object.defineProperty(exports, "__esModule", { value: true });
exports.Credentials = exports.isProviderType = exports.ProviderType = void 0;
const internal_1 = require("../internal");
/**
* Types of an authentication provider.
*/
var ProviderType;
(function (ProviderType) {
ProviderType["AnonUser"] = "anon-user";
ProviderType["ApiKey"] = "api-key";
ProviderType["LocalUserPass"] = "local-userpass";
ProviderType["CustomFunction"] = "custom-function";
ProviderType["CustomToken"] = "custom-token";
ProviderType["OAuth2Google"] = "oauth2-google";
ProviderType["OAuth2Facebook"] = "oauth2-facebook";
ProviderType["OAuth2Apple"] = "oauth2-apple";
})(ProviderType = exports.ProviderType || (exports.ProviderType = {}));
function isProviderType(arg) {
return Object.values(ProviderType).includes(arg);
}
exports.isProviderType = isProviderType;
class Credentials {
/** @internal */
internal;
/** @internal */
constructor(internal) {
this.internal = internal;
}
/**
* Creates credentials for an anonymous user. These can only be used once - using them a second
* time will result in a different user being logged in. If you need to get a user that has already logged
* in with the Anonymous credentials, use {@link App.currentUser} or {@link App.allUsers}.
* @param reuse - Reuse any existing anonymous user already logged in.
* @returns An instance of `Credentials` that can be used in {@link App.logIn}.
* @see https://docs.mongodb.com/realm/authentication/anonymous/
*/
static anonymous(reuse = true) {
return new Credentials(internal_1.binding.AppCredentials.anonymous(reuse));
}
static emailPassword(arg1, password) {
if (typeof arg1 === "string") {
internal_1.assert.string(password, "password");
return new Credentials(internal_1.binding.AppCredentials.usernamePassword(arg1, password));
}
else {
internal_1.assert.string(arg1.email, "email");
internal_1.assert.string(arg1.password, "password");
return new Credentials(internal_1.binding.AppCredentials.usernamePassword(arg1.email, arg1.password));
}
}
/**
* Creates credentials from an API key.
* @param key - A string identifying the API key.
* @returns An instance of `Credentials` that can be used in {@link App.logIn}.
* @see https://www.mongodb.com/docs/atlas/app-services/authentication/api-key/
*/
static apiKey(key) {
return new Credentials(internal_1.binding.AppCredentials.apiKey(key));
}
/**
* Creates credentials based on an Apple login.
* @param token - An Apple authentication token, obtained by logging into Apple.
* @returns An instance of `Credentials` that can be used in {@link App.logIn}.
* @see https://www.mongodb.com/docs/atlas/app-services/authentication/apple/
*/
static apple(token) {
return new Credentials(internal_1.binding.AppCredentials.apple(token));
}
/**
* Creates credentials based on a Facebook login.
* @param token - A Facebook authentication token, obtained by logging into Facebook.
* @returns An instance of `Credentials` that can be used in {@link App.logIn}.
* @see https://www.mongodb.com/docs/atlas/app-services/authentication/facebook/
*/
static facebook(token) {
return new Credentials(internal_1.binding.AppCredentials.facebook(token));
}
static google({ authCode, idToken }) {
let internal;
if (authCode !== undefined) {
(0, internal_1.assert)(idToken === undefined, "Must not supply both an authCode or idToken field");
internal = internal_1.binding.AppCredentials.googleAuth(internal_1.binding.GoogleAuthCode.make(authCode));
}
else {
(0, internal_1.assert)(idToken !== undefined, "Must supply either an authCode or idToken field");
internal = internal_1.binding.AppCredentials.googleId(internal_1.binding.GoogleIdToken.make(idToken));
}
return new Credentials(internal);
}
/**
* Creates credentials with a JSON Web Token (JWT) provider and user identifier.
* @param token - A string identifying the user. Usually an identity token or a username.
* @returns An instance of `Credentials` that can be used in {@link App.logIn}.
* @see https://www.mongodb.com/docs/atlas/app-services/authentication/custom-jwt/
*/
static jwt(token) {
return new Credentials(internal_1.binding.AppCredentials.custom(token));
}
/**
* Creates credentials with an Atlas App Services function and user identifier.
* @param payload - An object identifying the user. Usually an identity token or a username.
* @returns An instance of `Credentials` that can be used in {@link App.logIn}.
* @see https://www.mongodb.com/docs/atlas/app-services/authentication/custom-function/
*/
static function(payload) {
return new Credentials(internal_1.binding.AppCredentials.function(payload));
}
}
exports.Credentials = Credentials;
//# sourceMappingURL=Credentials.js.map