models/communities/Identity.js

import "../../thrift/usermanagement_types";
import * as utils from "../../utils";

export default class Identity {
    /**
     * Creates a new Identity instance from the provider parameters.
     *
     * @constructor Identity
     * @param {Object} identityMap - Identity parameters
     * @param {string} [identityMap.providerUserId] - User ID
     * @param {string} identityMap.accessToken - Access token
     * @param {string} identityMap.providerId - Provider ID
     * @param {boolean} identityMap.trusted - True if it's trusted
     */
    constructor(identityMap) {
        this.providerUserId = identityMap.providerUserId;
        this.accessToken = identityMap.accessToken;
        this.providerId = identityMap.providerId;
        this.trusted = identityMap.trusted;
    }

    /**
     * Creates a Facebook identity with specified access token.
     *
     * @memberof Identity
     * @param {string} accessToken - Token of Facebook user returned from FB SDK.
     * @return {Identity} New instance for Facebook user with specified access token.
    */
    static createFacebookIdentity(accessToken) {
        return new Identity({
            providerId: 'facebook',
            accessToken
        });
    }

    /**
     * Creates a custom identity.
     *
     * @memberof Identity
     * @param {string} customProviderName - Custom provider name.
     * @param {string} userId - Unique user ID for the custom provider.
     * @param {string} accessToken - Password of the user for the custom provider.
     * It's a string, provided by the developer and it will be
     * required by the GetSocial SDK to validate any future
     * intent to add this same identity to another user.
     * @return {Identity} Instance for your custom provider.
    */
    static createCustomIdentity(customProviderName, userId, accessToken) {
        return new Identity({
            providerId: customProviderName,
            providerUserId: userId,
            accessToken
        });
    }

    /**
     * Create trusted identity.
     *
     * @memberof Identity
     * @param {string} trustedProviderName - Your trusted provider name.
     * @param {string} accessToken - Access token for your trusted provider.
     * @return {Identity} Instance for your trusted provider.
     */
    static createTrustedIdentity(trustedProviderName, accessToken) {
        return new Identity({
            providerId: trustedProviderName,
            accessToken,
            trusted: true
        });
    }

    /**
     * Returns a THIdentity instance.
     *
     * @ignore
     * @return {THIdentity}
     */
    getTHIdentity() {
        return new THIdentity({
            provider: this.providerId,
            providerId: this.providerUserId,
            accessToken: this.accessToken
        });
    }

    /**
     * Returns params to authenticate using this identity.
     *
     * @ignore
     * @return {Object}
     */
     getParams() {
        return {
            identity_type: this.providerId,
            token: this.accessToken
        };
    }
}