react-native-background-geolocation@5.0.0-beta.1
    Preparing search index...

    Interface AuthorizationConfig

    Configure the SDK to authenticate with your server using an access token (e.g., a JSON Web Token), and automatically request new tokens when the server returns 401 Unauthorized.

    Note: Only JSON Web Token (JWT) is currently supported.

    The SDK automatically attaches your token to each HTTP request:

    Authorization: Bearer XXX.YYY.ZZZ
    

    When using Config.authorization, do not manually define Authorization inside HttpConfig.headers. The SDK manages all JWT headers automatically.

    If you supply AuthorizationConfig.refreshUrl, AuthorizationConfig.refreshToken, and AuthorizationConfig.refreshPayload, the SDK can automatically refresh expired tokens when a 401 Unauthorized response is received.

    Configuration

    const token = getMyToken(); // Your own JWT fetch method

    // Listen for authorization refresh results
    BackgroundGeolocation.onAuthorization((event) => {
    if (event.success) {
    console.log("[authorization] SUCCESS:", event.response);
    } else {
    console.log("[authorization] ERROR:", event.error);
    }
    });

    // Initialize with JWT authorization
    BackgroundGeolocation.ready({
    http: {
    url: "https://app.your.server.com/users/locations",
    autoSync: true,
    },
    authorization: {
    strategy: "JWT",
    accessToken: token.accessToken,
    refreshToken: token.refreshToken,
    refreshUrl: "https://auth.your.server.com/tokens",
    refreshPayload: {
    the_refresh_token_field_name: "{refreshToken}"
    },
    expires: token.expiresAt,
    }
    });

    Receiving responses from AuthorizationConfig.refreshUrl

    Whenever the SDK receives a response from your refreshUrl, it fires BackgroundGeolocation.onAuthorization. Your callback will receive an AuthorizationEvent.

    BackgroundGeolocation.onAuthorization((event) => {
    if (event.success) {
    console.log("[authorization] SUCCESS:", event.response);
    } else {
    console.log("[authorization] ERROR:", event.error);
    }
    });
    interface AuthorizationConfig {
        strategy: string;
        accessToken: string;
        refreshToken?: string;
        refreshUrl?: string;
        refreshPayload?: Record<string, string>;
        refreshHeaders?: Record<string, string>;
        expires?: number;
    }
    Index

    Properties

    strategy: string

    Authorization strategy. Only JWT is supported.

    accessToken: string

    Authorization token (eg: JWT) required for authorization by your server at HttpConfig.url.

    The SDK will automatically apply the configured accessToken to each HTTP request's Authorization header, eg:

    "Authorization": "Bearer XXX.YYY.ZZZ"

    You do not need to manually configure HttpConfig.headers with the Authorization parameter. It is all automatic.

    refreshToken?: string

    The token to be POSTed to refreshUrl, encoded into the refreshPayload, when a new accessToken is required after expires or when HTTP 401 Unauthorized is received.

    refreshUrl?: string

    The url to your authorization server that provides new accessToken when expired.

    When the SDK receives a response the server, it will decode the JSON and recursively iterate through the keys, performing regular expressions and other String-analysis to "taste" the data in search of the following 3 items:

    1. "access token"
    2. "refresh token"
    3. "expiry time"

    The SDK is designed to operate with any response data-structure. For example, one authorization server might return a complex response such as:

    {
    "token": {
    "access_token": "XXX.YYY.ZZZ",
    "expires_at": 3900
    },
    "refresh_token": "smTsfaspfgaadsfgqZerUt0wueflasdfkaxjdfeKIacb"
    }

    While another server might return a flat response, such as:

    {
    "accessToken": "XXX.YYY.ZZZ",
    "refreshToken": "smTsfaspfgaadsfgqZerUt0wueflasdfkaxjdfeKIacb",
    "expiry": 3900
    }

    When the response from the server is received, the event BackgroundGeolocation.onAuthorization will be fired, provided with the AuthorizationEvent.

    refreshPayload?: Record<string, string>

    Refresh payload will be encoded into the FORM POST to the refreshUrl when requesting a new accessToken after expiration.

    You must provide one field-template which will represent your "refresh token" using the value: {refreshToken}. The SDK will automatically replace this simple template with the configured refreshToken.

    BackgroundGeolocation.ready({
    authorization: {
    strategy: "JWT",
    accessToken: "XXX.YYY.ZZZ",
    refreshUrl: "https://auth.your.server.com/tokens",
    refreshToken: "smTsfaspfgaadsfgqZerUt0wueflasdfkaxjdfeKIacb",
    refreshPayload: {
    my_refresh_token: "{refreshToken}",
    grant_type: "refresh_token",
    foo: "another arbitrary field"
    }
    }
    });

    with the configuration above, a curl representation of the SDK's FORM POST, might look like this:

    $ curl -X POST \
    -F 'my_refresh_token=smTsfaspfgaadsfgqZerUt0wueflasdfkaxjdfeKIacb' \
    -F 'grant_type=refresh_token' \
    -F 'foo=another arbitrary field' \
    https://auth.your.server.com/tokens
    refreshHeaders?: Record<string, string>

    Optional headers applied on requests to refreshUrl Defaults to: {"Authorization": "Bearer {accessToken}"}

    The template variable {accessToken} will automatically be replaced with your app's current auth token.

    If you do not want any headers applied on requests to {refreshUrl}, provide an empty {}.

    BackgroundGeolocation.ready({
    authorization: {
    accessToken: "XXX.YYY.ZZZ",
    refreshUrl: "https://auth.domain.com/tokens",
    refreshToken: "smTsfaspfgaadsfgqZerUt0wueflasdfkaxjdfeKIacb",
    refreshPayload: {
    "my_refresh_token": "{refreshToken}", // <-- replaced with configured refreshToken above.
    "grant_type": "refresh_token", // <-- arbitrary fields required by your auth server
    "foo": "another arbitrary field"
    },
    refreshHeaders: {} // <-- Empty {} to provide no refreshHeaders.
    )
    ));
    expires?: number

    Token expiry time in seconds.