router.get('/auth-callback', function(req, res) {
  const spotifyApi = new SpotifyWebApi({
    clientId: process.env.SPOTIFY_CLIENT_ID,
    clientSecret: process.env.SPOTIFY_CLIENT_SECRET,
    redirectUri: process.env.SPOTIFY_AUTH_CALLBACK
  });

  spotifyApi.authorizationCodeGrant(req.query.code)
    .then((data) => spotifyApi.setAccessToken(data.body.access_token))
    .then(() => getEmail(spotifyApi))
    .then((email) => {
      const url = `http://schedule.myoutsidelands.com?email=${email}`;
      const cachedSchedule = cache.get(email);

      if (cachedSchedule) {
        res.redirect(url);
      } else {
        createNewSchedule(spotifyApi, email)
          .then((scheduleWithTags) => {
            cache.set(email, scheduleWithTags);
            res.redirect(url);
          });
      }
    })
});
Example #2
0
function handleCredentials (code, state, callback) {
  const spotifyApi = new SpotifyWebApi({
    redirectUri: redirectUri,
    clientId: config.spotify.clientId,
    clientSecret: config.spotify.clientSecret
  })

  const user = {}

  spotifyApi.authorizationCodeGrant(code)
    .then((data) => {
      user.accessToken = data.body.access_token
      user.refreshToken = data.body.refresh_token

      // Set the access token on the API object to use it in later calls
      spotifyApi.setAccessToken(user.accessToken)
      spotifyApi.setRefreshToken(user.refreshToken)

      return spotifyApi.getMe()
    }).then((data) => {
      user.id = data.body.id
      user.name = data.body.display_name
      user.email = data.body.email

      callback(null, user)
    }).catch((err) => {
      console.log(err)
      callback(Boom.badImplementation('couldn\'t get spotify user'))
    })
}
Example #3
0
app.get('/callback', (req, res) => {
    spotify.authorizationCodeGrant(req.query.code)
        .then(data => {
            clearTimeout(accessTokenTimer);
            updateSpotifyToken(data.body);
            return res.redirect('/');
        })
        .catch(() => res.send("Could not refresh Spotify access token."));
});
Example #4
0
app.get('/callback', function(req, res) {
  spotifyApi.authorizationCodeGrant(req.query.code)
    .then(function(data) {
      spotifyApi.setAccessToken(data.body['access_token']);
      spotifyApi.setRefreshToken(data.body['refresh_token']);
      return res.redirect('/');
    }, function(err) {
      return res.send(err);
    });
});
    login.loginAndGetAuthCode(function(authorizationCode) {
        spotifyApi.authorizationCodeGrant(authorizationCode)
            .then(function(data) {
                spotifyApi.setAccessToken(data['access_token']);
                spotifyApi.setRefreshToken(data['refresh_token']);

                callback();
            }, function(err) {
                console.log('Something went wrong when retrieving the access token!', err);
            });
    });
Example #6
0
	validateAuth(code, res) {
		return this.api.authorizationCodeGrant(code).then((data) => {
			this.api.setAccessToken(data.body['access_token']);
    		this.api.setRefreshToken(data.body['refresh_token']);

    		let expiresOn = new Date(new Date().getTime() + 3600*1000);
    		res.cookie('access_token', data.body['access_token'], {expires: expiresOn});
    		res.cookie('refresh_token', data.body['refresh_token'], {expires: expiresOn});
    		//TODO: save refresh token to DB
		}, function(err) {
			console.error('An error occurred in validateAuth', err);
		});
	}
Example #7
0
app.use(redirectUri, (req, res, next) => {
  const { code } = req.query

  spotifyApi.authorizationCodeGrant(code)
    .then((authData) => {
      writeAuthFile(authData.body)
        .then(() => {
          res.redirect(`/?auth=done`)
        }, (err) => {
          console.log('error on auth', err)
          res.redirect(`/auth`)
        })
    }, (err) => {
      console.log('error on auth', err)
      res.redirect(`/auth`)
    })
})
Example #8
0
                .then(function (data) {
                    console.log('The access token expires in ' + data['expires_in']);
                    console.log('The access token is ' + data['access_token']);

                    // Save the access token so that it's used in future calls
                    spotifyApi.setAccessToken(data['access_token']);
                    spotifyApi.authorizationCodeGrant(obj.SpotifyToken)
                        .then(function (data) {
                            console.log('Retrieved access token', data['access_token']);

                            // Set the access token
                            spotifyApi.setAccessToken(data['access_token']);
                            console.log('The access token expires in ' + data['expires_in']);
                            console.log('The access token is ' + data['access_token']);
                            ///THIS TOKEN NEEDS TO BE STORED!!!!!
                            User.update({username: obj.username}, {access_token_spotify: data['access_token']}, function (obj, err) {
                                console.log(obj)
                            })
                            // Use the access token to retrieve information about the user connected to it
                            return spotifyApi.getMe();
                        })
                        .then(function (data) {
                            User.update({username: obj.username}, {spotify_id:data['id']}, function (obj, err) {
                                console.log(obj)
                            })
                                // "Retrieved data for Faruk Sahin"
                            console.log('Retrieved data for ' + data['display_name']);
                            console.log(data);
                            // "Email is farukemresahin@gmail.com"
                            console.log('Email is ' + data.email);

                            // "Image URL is http://media.giphy.com/media/Aab07O5PYOmQ/giphy.gif"
                            console.log('Image URL is ' + data.images[0].url);

                            // "This user has a premium account"
                            console.log('This user has a ' + data.product + ' account');
                        })
                        .catch(function (err) {
                            console.log('Something went wrong', err);
                        });


                })
Example #9
0
auth.get('/callback', (req, res) => {
  const { code, state } = req.query;
  const storedState = req.cookies ? req.cookies[SPOTIFY_STATE_KEY] : null;

  if(state === null || state !== storedState) {
    res.redirect('/auth/spotify');
  } else {
    res.clearCookie(SPOTIFY_STATE_KEY);

    spotifyAPI.authorizationCodeGrant(code)
      .then(data => {
        const { access_token, refresh_token } = data.body;

        res.redirect(`/auth/spotify/${access_token}/${refresh_token}`);
      })
      .catch(() => {
        res.redirect('/auth/spotify');
      });
  }
});
Example #10
0
    this.app.get('/return', (req, res) => {

      var code = req.query.code;
      this.spotifyApi.authorizationCodeGrant(code)
        .then((data) => {
          console.log('The token expires in ' + data.body['expires_in']);
          console.log('The access token is ' + data.body['access_token']);
          console.log('The refresh token is ' + data.body['refresh_token']);

          // Set the access token on the API object to use it in later calls
          this.spotifyApi.setAccessToken(data.body['access_token']);
          this.spotifyApi.setRefreshToken(data.body['refresh_token']);

          return res.send('Ok')

        })
        .catch((error) => {
          console.log(error);
          return res.send(error);
        });

    });
Example #11
0
router.get('/callback', (req, res) => {
  const { code, state } = req.query;
  const storedState = req.cookies ? req.cookies[STATE_KEY] : null;
  if (state === null || state !== storedState) {
    res.redirect('/#/error/state mismatch');
  } else {
    res.clearCookie(STATE_KEY);
    spotifyApi.authorizationCodeGrant(code).then(data => {
      const { expires_in, access_token, refresh_token } = data.body;

      spotifyApi.setAccessToken(access_token);
      spotifyApi.setRefreshToken(refresh_token);

      spotifyApi.getMe().then(({ body }) => {
        console.log(body);
      });

      res.redirect(`/#/user/${access_token}/${refresh_token}`);
    }).catch(err => {
      res.redirect('/#/error/invalid token');
    });
  }
});