Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
henrahmagix opened this issue May 4, 2017 · 0 comments

Comments

@henrahmagix
Copy link
Contributor

henrahmagix commented May 4, 2017

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));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant