Skip to content
This repository has been archived by the owner on Mar 29, 2019. It is now read-only.

401 (Unauthorized) error #16

Open
krajkumar6 opened this issue Nov 11, 2016 · 0 comments
Open

401 (Unauthorized) error #16

krajkumar6 opened this issue Nov 11, 2016 · 0 comments

Comments

@krajkumar6
Copy link

krajkumar6 commented Nov 11, 2016

Below is my angular js service function( Where auth2 is the google api auth object) and after that is my server passport strategy code. I'm unable to authenticate using headers or params in my http request and keep getting unauthorized error.

this.glogin =function(){
        var deferred = $q.defer();.
        auth2.signIn({
          'scope': 'profile email'
        }).then(function(response){
            self.gUser=response;
            self.accesstoken = self.gUser.getAuthResponse().access_token;
            $cookies.put('acctoken',self.accesstoken);
            $log.log('self.accesstoken',self.accesstoken);
               $http({
                method: 'GET',
                url:"/auth/google/accesstoken",
                //params:{access_token : self.accesstoken},
                headers:{
                 access_token : self.accesstoken
                }
                }).then(function successCallback(srresponse){
                  
                  self.userobj=srresponse.data;
                  $log.log('google passport authenticated user:',srresponse.data);
                  $cookies.putObject('usrobj',srresponse.data);
                  deferred.resolve(self.userobj);
            
                },function errorCallback(srresponse){
                  $log.error("http request for user login failed");
                  deferred.reject(srresponse.statusText);
              }); //$http google+
        });
               
        return deferred.promise;
    };//glogin 

node js code

passport.use(new GoogleTokenStrategy ({
    clientID: Auth.googleAuth.clientID,
    clientSecret: Auth.googleAuth.clientSecret    
  },  function(accessToken, refreshToken, profile, done) {
    // Create or update user, call done() when complete...
     process.nextTick(function(){
        ubCust.findOne({'email' : profile.emails[0].value}, function(err, user) {
          if (err) 
              return done(err);
          if(user)
              {
                if(user.facebook.id)
                {
                    user.google.id = profile.id;
                    user.google.token = accessToken;
                    user.google.fname = profile.displayName;
                    user.google.lname = profile.name.familyName;
                    user.fname = profile.displayName;
                    user.lname = profile.name.familyName;
                    user.email = profile.emails[0].value;
                    user.save(function(err){
                            if (err)
                                throw err;
                            return done(null,user);
                        });
                }
                else
                  return done(null,user);
              }
              
            else
                {
                    var newUser = new ubCust;
                    newUser.google.id = profile.id;
                    newUser.google.token = accessToken;
                    newUser.google.fname = profile.displayName;
                    newUser.google.lname = profile.name.familyName;
                    newUser.fname = profile.displayName;
                    newUser.lname = profile.name.familyName;
                    newUser.email = profile.emails[0].value;
                    newUser.save(function(err){
                        if (err)
                            throw err;
                        return done(null,newUser);
                    });
                    console.log(profile);
                }
            //done(null, profile, tokens);
            }); //findOne
        });//nextTick

  }
));

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant