1
votes

I use auth0 to create function login with angularjs.

When I input email and password login success not return message and redirect to login page again. I check data return cannot see 'id_token'.

app.js include config auth

var rootApp = angular.module('xxxx', [
    'auth0.lock',
]);

rootApp.config(function(lockProvider) {
    lockProvider.init({
        clientID: 'xxxxx',
        domain: 'xxxx',
        auth:{
            redirectUrl: window.location.origin + '/callback',
            responseType: 'token',
            params: {
                scope: 'openid profile'
            }
        },
        options: {
            _idTokenVerification: true,
            configurationBaseUrl: 'https://cdn.auth0.com',
            theme:{
                logo:'/logos/full_size/medium.png',
                primaryColor:'#C59D18'
            }
        }
    });
});

auth.service.js

(function () {
    'use strict';

    angular.module('BlurAdmin')
        .service('authService', authService);

    authService.$inject = ['lock', '$location'];

    function authService(lock, $location) {
        function login() {
            // Display the Lock widget using the
            // instance initialized in the app.js config
            lock.show();
        }

        function logout() {
            // Remove tokens and expiry time from localStorage
            localStorage.removeItem('access_token');
            localStorage.removeItem('id_token');
            localStorage.removeItem('expires_at');
            $location.path('/');
        }

        function handleAuthentication() {
            // Uncomment if you are not using HTML5Mode
            // lock.interceptHash();

            lock.on('authenticated', function(authResult) {
                if (authResult && authResult.accessToken && authResult.idToken) {
                    console.log('Authenticated!', authResult);
                    _setSession(authResult);
                }
            });
            lock.on('authorization_error', function(err) {
                console.log(err);
                alert(
                    'Error: ' + err.error + '. Check the console for further details.'
                );
            });
        }

        function _setSession(authResult) {
            // Set the time that the Access Token will expire
            var expiresAt = JSON.stringify(
                authResult.expiresIn * 1000 + new Date().getTime()
            );
            // Save tokens and expiration to localStorage
            localStorage.setItem('access_token', authResult.accessToken);
            localStorage.setItem('id_token', authResult.idToken);
            localStorage.setItem('expires_at', expiresAt);
        }

        function isAuthenticated() {
            // Check whether the current time is
            // past the Access Token's expiry time
            var expiresAt = JSON.parse(localStorage.getItem('expires_at'));
            return new Date().getTime() < expiresAt;
        }

        return {
            login: login,
            logout: logout,
            handleAuthentication: handleAuthentication,
            isAuthenticated: isAuthenticated
        };
    }
})();
1

1 Answers

1
votes

at first glance you are probably not receiving an id_token because you have not specified in your app.js the responseType to contain id_token:

auth: {
  ...
  responseType: 'token id_token',
  ...
}

give that a try and you should be good to go!