/
SpotifyController.php
56 lines (44 loc) · 1.6 KB
/
SpotifyController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
namespace App\Http\Controllers;
use App\Http\Integrations\Spotify\AuthConnector;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
class SpotifyController extends Controller
{
/**
* Handle the authorization URL redirection.
*
* @return \Illuminate\Http\RedirectResponse
* @throws \Saloon\Exceptions\OAuthConfigValidationException
*/
public function handleAuthorization()
{
$connector = new AuthConnector;
$authorizationUrl = $connector->getAuthorizationUrl(['user-read-currently-playing', 'user-library-read']);
Session::put('spotifyAuthState', $connector->getState());
return redirect()->to($authorizationUrl);
}
/**
* Handle the call back from Spotify.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\RedirectResponse
* @throws \ReflectionException
* @throws \Saloon\Exceptions\InvalidResponseClassException
* @throws \Saloon\Exceptions\InvalidStateException
* @throws \Saloon\Exceptions\OAuthConfigValidationException
* @throws \Saloon\Exceptions\PendingRequestException
*/
public function handleCallback(Request $request)
{
$code = $request->input('code');
$state = $request->input('state');
$expectedState = Session::pull('spotifyAuthState');
$connector = new AuthConnector;
$authorization = $connector->getAccessToken($code, $state, $expectedState);
$user = auth()->user();
$user->spotify_auth = $authorization;
$user->save();
return redirect()->route('home');
}
}