Я новичок в AngularJS.Я пытаюсь войти, используя Google OAuth
.Он перенаправляет меня на ту же страницу входа.
2 месяца назад это работало без проблем.Теперь есть проблема login
.Вот мой полный код.
Index.html
<body ng-app="futusome2App" ng-controller='appCtrl'>
<md-content class='maincontent'>
<div class='container' flex-gt-md>
<div class='header' layout="row" layout-sm="column" layout-align="space-between end" layout-align-sm="center center" style="padding:20px 0 10px">
<a href="#/"><img src='images/header.png' style='width:100%;max-width:255px;border:0;outline:0;margin-bottom:10px'></a>
<div ng-if="isLoggedIn" class="" layout="row" layout-align="end center" ng-cloak>
<md-button aria-label="cxo" class="md-icon-button md-fab md-mini md-primary" ng-if="loginData.cxo" ng-click="go('cxo')">
<i class="fa fa-line-chart"></i>
<md-tooltip>View CXO data</md-tooltip>
</md-button>
<md-menu md-position-mode="target-right target" class="userInfo">
<md-button aria-label="User" class="md-icon-button md-fab md-mini" ng-click="$mdOpenMenu($event)" data-ng-style="{'background-image':'url('+loginData.picture+')'}" style="background-size: cover;">
<md-tooltip>User / Logout</md-tooltip>
</md-button>
<md-menu-content>
<md-menu-item>
<p>
<img class='userThumb' ng-src="{{loginData.picture}}">
<span>{{loginData.name}}</span>
</p>
</md-menu-item>
<md-menu-divider></md-menu-divider>
<md-menu-item>
<md-button aria-label="My accounts" ng-click="go('/rep/'+loginData.id)">
My accounts
</md-button>
</md-menu-item>
<md-menu-item>
<md-button aria-label="Logout" ng-click="logout()">
Logout
</md-button>
</md-menu-item>
</md-menu-content>
</md-menu>
<md-menu md-position-mode="target-right target" class="areaSelect" ng-show="currentArea">
<md-button aria-label="Set area" class="md-icon-button md-fab md-mini" ng-click="$mdOpenMenu($event)" data-ng-style="{'background-image':'url(images/flags/'+currentArea.flag+')'}" style="background-size: cover;">
<md-tooltip>Select area</md-tooltip>
</md-button>
<md-menu-content>
<md-menu-item ng-click="">
<p class="menuheader">Set area</p>
</md-menu-item>
<md-menu-divider></md-menu-divider>
<md-menu-item ng-repeat="area in areas | orderBy:'name'">
<md-button ng-click="setArea(area)">
<span class="area-flag setArea" ng-style="{'background-image':'url(images/flags/'+area.flag+')'}"></span>
<span class="area-name">{{area.name}}</span>
</md-button>
</md-menu-item>
</md-menu-content>
</md-menu>
</div>
</div>
</div>
<div class='container' layout="column" layout-margin>
<div ng-view="" ng-show="isLoggedIn" ng-cloak></div>
<div ng-show="!isLoggedIn" layout="column" layout-align="center center" ng-cloak>
<h1 style="margin-bottom: 0.8em;">Login</h1>
<md-button class="md-primary md-raised md-default-theme" ng-click="googleLogin()" style="padding: 5px 15px;"> <img src="images/google.png" class="ng-scope" style="margin-bottom: 4px;margin-right: 10px;border-radius: 50%;width: 24px;">Sign in with Google</md-button>
<p ng-if="loginError" style="color:red">Error. {{loginError}}.</p>
</div>
</div>
</md-content>
В Index.html мы проверяем, логин верен или нет.
App.js
angular
.module('futusome2App', [
'ngAnimate',
'ngAria',
'ngCookies',
'ngMessages',
'ngResource',
'ngRoute',
'ngSanitize',
'ngMaterial',
'ui.bootstrap',
'chart.js',
'wu.masonry',
'angularMoment',
'ui.codemirror',
'LocalStorageModule',
'angular-cache',
])
.config(function ($routeProvider,$locationProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/main.html',
controller: 'MainCtrl'
})
.when('/access_token=:accessToken', {
template: '',
controller: function ($location,LoginService,$rootScope,$cookies) {
var hash = $location.path().substr(1);
var splitted = hash.split('&');
var params = {};
alert("testttt");
for (var i = 0; i < splitted.length; i++) {
var param = splitted[i].split('=');
var key = param[0];
var value = param[1];
params[key] = value;
}
if(params.access_token) {
$rootScope.access_token = params.access_token;
LoginService.getGoogleLoginCredentials($rootScope.access_token) ;
}
var goTo = $cookies.get("gotopage");
$cookies.remove("gotopage");
console.log("check for goto cookie",goTo);
if(goTo != "" && typeof goTo !== 'undefined') {
// $rootScope.go(goTo);
$location.path(goTo);
}
else {
$rootScope.go("/");
}
return false;
}
})
.when('/agent/:id', {
templateUrl: 'views/agent.html',
controller: 'AgentCtrl'
})
.otherwise({
redirectTo: '/'
})
;
})
Проверьте вход в файл app.js
$rootScope.googleLogin = function(){
$cookies.put('init_login',"yes");
var client_id="590264372642-vio47il7v8oqu5dmc5q25okpkh18e332.apps.googleusercontent.com";
var scope="profile email";
var redirect_uri = ($window.location.href.split("#"))[0].replace(/www\`enter code here`./,"");
while(redirect_uri.slice(-1) == "/") {
redirect_uri = redirect_uri.slice(0,redirect_uri.length-1);
}
redirect_uri += "/";
var response_type="token";
var url="https://accounts.google.com/o/oauth2/auth?scope="+scope+"&client_id="+client_id+"&redirect_uri=" + encodeURIComponent(redirect_uri) + "&response_type="+encodeURIComponent(response_type);
console.log("url: "+redirect_uri);
alert("ankur "+redirect_uri);
$rootScope.go(url);
return false;
}
$rootScope.checkLogin = function() {
// console.log('Is user logged in?');
if($rootScope.isLoggedIn) return false;
$rootScope.access_token = $cookies.get('access_token');
if($rootScope.access_token) {
// console.log('yes,token is',$rootScope.access_token);
LoginService.getGoogleLoginCredentials($rootScope.access_token);
}
else {
console.log('no, login first.',"set gotocookie:",$location.path());
if($location.path().indexOf("/access_token") == -1 && $location.path() != "/") {
$cookies.put("gotopage",$location.path());
}
$rootScope.isLoggedIn = false;
}
}
$rootScope.checkLogin();
Служба входа в app.js
.service('LoginService',function($q, $rootScope, $http, $cookies, $location, $timeout, apiService, localStorageService){
return {
getGoogleLoginCredentials: function(token) {
if($location.path().indexOf("/access_token") == -1 && $location.path() != "/") {
$cookies.put("gotopage",$location.path());
}
//GET CREDENTIALS WITH TOKEN
$http.get('https://www.googleapis.com/oauth2/v2/userinfo?access_token='+token)
.then(function(data){
data.data.token = token;
if(data.data.hd == "company.com") {
$rootScope.favoriteClients = [];
var expireDate = new Date();
expireDate.setDate(expireDate.getDate() + 1);
$cookies.put('access_token',token,{'expires': expireDate});
$rootScope.isLoggedIn = true;
$rootScope.loginData = data.data;
console.log("just logged in", $rootScope.loginData);
if($rootScope.loginData.office != "") {
$rootScope.currentArea = _.filter($rootScope.areas,_.matches({'id':$rootScope.loginData.office}))[0];
localStorageService.set('currentArea',$rootScope.currentArea);
}
//update users with login creds
$http.post('api/users/',JSON.stringify(data.data)).then(function(userdata){
apiService.getReps($rootScope.loginData.id).then(function(data){
$rootScope.loginData = _.merge($rootScope.loginData, data[0]);
//Make favorites array
if(typeof $rootScope.loginData.favs !== 'undefined'){
$rootScope.favoriteClients = _.without($rootScope.loginData.favs.split(","),"");
}
//set area is set in prefs, else force setting office
if($rootScope.loginData.office != "") {
$rootScope.currentArea = _.filter($rootScope.areas,_.matches({'id':$rootScope.loginData.office}))[0];
localStorageService.set('currentArea',$rootScope.currentArea);
}
else {
$rootScope.go("/setarea");
}
});
});
var admins = [
"test.mail@company.com"
];
if(admins.indexOf(data.data.email) > -1) {
$rootScope.loginData.cxo = true;
}
else {
$rootScope.loginData.cxo = false;
}
//TEST COOKIES
var a = $cookies.get('access_token');
if(a == "" || typeof a !== 'string') {
alert("Unable to set cookie, make sure cookies are enabled or try using another browser");
}
}
else {
//if not mw
$rootScope.isLoggedIn = false;
$cookies.remove('access_token');
$rootScope.loginError = "Invalid credentials";
$location.path("/");
}
},function(error){
$rootScope.isLoggedIn = false;
$cookies.remove('access_token');
$location.path("/");
});
},
logout: function(){
console.log("logout");
$cookies.remove('access_token');
$cookies.remove("gotopage");
$location.path("/");
$rootScope.isLoggedIn = false;
}
}
})
Плз Помогите мне.Спасибо