Skip to content

Allow multiple AUTH_HEADER_PREFIX values so different login methods can send the correct Authorization header #37

Description

@henrahmagix

For example: Django login via username and password requires Authorization: Token abcdef-123456... whereas using the social auth library for logging in via Facebook, Google, et al requires Authorization: Bearer abcdef-123456.... So Token needs to be replaced with Bearer.

Here is a gist using decoration for use in a project, but this can easily be applied as an upgrade to this library.

The key is calling authStorageFactory.set('authType', 'SOME_KEY_TYPE') to map to a key in settings, when you know what login method has been used. Then a different authorization header prefix will be fetched.

(function (angular) {
    'use strict';

    var module = angular.module('login-with-social-auth', ['angular-token-auth']);

    // The objective here is to alter the http config object if the auth type
    // requires a different AUTH_HEADER_PREFIX.

    // Somewhere, you need to store the auth type by which the user logged in.
    // Store it like this:
    // if (userLoggedInDifferentlyThanNormal) {
    //     authStorageFactory.set('authType', 'SOME_TYPE_KEY');
    // }
    // Then hold some settings where you match SOME_TYPE_KEY to an auth header prefix:
    // "AUTH_HEADER_PREFIXES":
    //     "SOME_TYPE_KEY": "Some prefix string, like Bearer"
    // }
    // Note the key is AUTH_HEADER_PREFIXES, not AUTH_HEADER_PREFIX which is
    // already in use and likely set to `Token` for Django user:pass logins.

    module.config([
        '$provide',
        function (
            $provide
        ) {
            $provide.decorator('authInterceptor', [
                '$delegate',
                'authFactory',
                'authStorageFactory',
                'PROJECT_SETTINGS',
                function (
                    $delegate,
                    authFactory,
                    authStorageFactory,
                    PROJECT_SETTINGS
                ) {
                    var originalRequest = $delegate.request;
                    $delegate.request = function () {
                        var config = originalRequest.apply($delegate, arguments);
                        var authType = authStorageFactory.get('authType');
                        var authHeaderPrefixForType = PROJECT_SETTINGS[authType];
                        if (authHeaderPrefixForType) {
                            config.headers.Authorization = authHeaderPrefixForType + ' ' + authFactory.getToken();
                        }
                        return config;
                    };
                    return $delegate;
                }
            ]);
        }
    ]);
}(window.angular));

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions