OAuth Token Response to Angular View (cookie)

I've been struggling with this for a couple hours now and need some help. I've created a simple app that presents the user with a "Login Using Google" button in an angular view that redirects the user to the Google Oauth page. Here's the controller code that calls the login() function when the button is pressed:

angular.module('dashApp').controller('SigninCtrl', function ($scope) {

$scope.login=function() {
    var client_id="191641883719-5eu80vgnbci49dg3fk47grs85e0iaf9d.apps.googleusercontent.com";
    var scope="email";
    var redirect_uri="http://local.host:9000/api/auth/google";
    var response_type="code";
    var url="https://accounts.google.com/o/oauth2/auth?scope="+scope+"&client_id="+client_id+"&redirect_uri="+redirect_uri+
    "&response_type="+response_type;
    window.location.replace(url);
  };
});

The redirect URI set in my google project redirects to a server page to this server page:

'use strict';

var _ = require('lodash');
var request = require('request');
var qs = require('querystring');
var fs = require('fs');

// Get list of auths
exports.google_get = function (req,res){
  var code = req.query.code,
      error = req.query.error;

  if(code){
    //make https post request to google for auth token
    var token_request = qs.stringify({
      grant_type: "authorization_code",
      code: code,
      client_id: "191641883719-5eu80vgnbci49dg3fk47grs85e0iaf9d.apps.googleusercontent.com",
      client_secret: process.env.GOOGLE_SECRET,
      redirect_uri: "http://local.host:9000/api/auth/google"
    });
    var request_length = token_request.length;
    var headers = {
      'Content-length': request_length,
      'Content-type':'application/x-www-form-urlencoded'
    };

    var options = {
      url:'https://www.googleapis.com/oauth2/v3/token',
      method: 'POST',
      headers: headers,
      body:token_request
    };

    request.post(options,function(error, response, body){
      if(error){
        console.error(error);
      }else{
        //WHAT GOES HERE?

      }
    });
  }
  if(error){
    res.status(403);
  }
}

I'm able to exchange the code returned by google for an auth token object successfully and log it to the terminal. I've been told that I should set a cookie using:

res.setHeader('Content-Type', 'text/plain');
res.setCookie('SID','yes',{
  domain:'local.host',
  expires:0,
  path:'/dashboard',
  httpOnly:false
});

 res.status(200);
 res.end();

Followed by a controller on the page I'm directing the user to that validates the session.

What am I doing wrong?

Since you have already done the hard work so there is no point talking about passport.js which is actually written to simplify these kind of social login authentication.

So let's come directly to session implementaion logic.

You need to set the following header in your app.js/server.js :

app.use(function(req, res, next) {
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST');
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type, Authorization');
    next();
});

Let's say you are returning this token after successful login :

{
    name : "some name",
    role : "some role",
    info : "some info"
}

You can have a client side function in your angular service or controller :

function(user,callback){
    var loginResource = new LoginResource(); //Angular Resource
    loginResource.email = user.email;
    loginResource.password = user.password;
    loginResource.$save(function(result){
        if(typeof result !== 'undefined'){
            if(result.type){
                $localStorage.token = result.token;
                $cookieStore.put('user',result.data);
                $rootScope.currentUser = result.data;
            }
        }
        callback(result);
    }); 
}

LoginResource calls your REST endpoint which returns auth token.

You can store your auth token in localStorage and cookieStore.

localStorage makes sure that we are having the token saved even when user has closed the browser session. If he clears the localStorage and cookieStorage both then log him out as you don't have any valid token to authorize user.

This is the same logic which i am using here. If you need more help then let me know.