我使用 auth0 使用 angularjs 创建函数登录。
当我输入电子邮件和密码登录成功时,不返回消息并再次重定向到登录页面。我检查数据返回看不到“id_token”。
app.js 包含配置身份验证
var rootApp = angular.module('xxxx', [
'auth0.lock',
]);
rootApp.config(function(lockProvider) {
lockProvider.init({
客户 ID:'xxxxx',
域:'xxxx',
授权:{
redirectUrl: window.location.origin + '/callback',
响应类型:'令牌',
参数:{
范围:'openid 个人资料'
}
},
选项: {
_idTokenVerification:真,
configurationBaseUrl: 'https://cdn.auth0.com',
主题:{
标志:'/logos/full_size/medium.png',
原色:'#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
};
}
})();