mirror of
https://github.com/koel/koel
synced 2024-12-12 21:52:28 +00:00
87 lines
2 KiB
PHP
87 lines
2 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace App\Http\Controllers\API;
|
||
|
|
||
|
use Illuminate\Contracts\Auth\Guard;
|
||
|
use Illuminate\Http\Request;
|
||
|
use Illuminate\Routing\Redirector;
|
||
|
use App\Services\Lastfm;
|
||
|
|
||
|
class LastfmController extends Controller
|
||
|
{
|
||
|
/*
|
||
|
* The Guard implementation.
|
||
|
*
|
||
|
* @var Guard
|
||
|
*/
|
||
|
protected $auth;
|
||
|
|
||
|
/**
|
||
|
* Construct the controller with a custom Redirector via DI.
|
||
|
*
|
||
|
* @param Redirector $redirector
|
||
|
* @param Guard $auth
|
||
|
*/
|
||
|
public function __construct(Guard $auth)
|
||
|
{
|
||
|
$this->auth = $auth;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Connect the current user to Last.fm.
|
||
|
*
|
||
|
* @param Redirector $redirector
|
||
|
* @param Lastfm $lastfm
|
||
|
*
|
||
|
* @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
|
||
|
*/
|
||
|
public function connect(Redirector $redirector, Lastfm $lastfm)
|
||
|
{
|
||
|
if (!$lastfm->enabled()) {
|
||
|
abort(401, 'Koel is not configured to use with Last.fm yet.');
|
||
|
}
|
||
|
|
||
|
return $redirector->to(
|
||
|
'https://www.last.fm/api/auth/?api_key='
|
||
|
.$lastfm->getKey()
|
||
|
.'&cb='.route('lastfm.callback')
|
||
|
);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Serve the callback request from Last.fm.
|
||
|
*
|
||
|
* @param Request $request
|
||
|
* @param Lastfm $lastfm
|
||
|
*
|
||
|
* @return \Illuminate\Http\Response
|
||
|
*/
|
||
|
public function callback(Request $request, Lastfm $lastfm)
|
||
|
{
|
||
|
if (!$token = $request->input('token')) {
|
||
|
abort(500, 'Something wrong happened.');
|
||
|
}
|
||
|
|
||
|
// Get the session key using the obtained token.
|
||
|
if (!$sessionKey = $lastfm->getSessionKey($token)) {
|
||
|
abort(500, 'Invalid token key.');
|
||
|
}
|
||
|
|
||
|
$this->auth->user()->savePreference('lastfm_session_key', $sessionKey);
|
||
|
|
||
|
return view('api.lastfm.callback');
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Disconnect the current user from Last.fm.
|
||
|
*
|
||
|
* @return \Illuminate\Http\JsonResponse
|
||
|
*/
|
||
|
public function disconnect()
|
||
|
{
|
||
|
$this->auth->user()->deletePreference('lastfm_session_key');
|
||
|
|
||
|
return response()->json();
|
||
|
}
|
||
|
}
|