planiverse

A minimalist, no-JS front-end for Mastodon.
git clone https://git.stjo.hn/planiverse
Log | Files | Refs | README | LICENSE

LoginController.php (3424B)


      1 <?php
      2 
      3 namespace App\Http\Controllers;
      4 
      5 use App\Http\Controllers\Controller;
      6 use Mastodon;
      7 use Illuminate\Support\Facades\DB;
      8 use Socialite;
      9 
     10 /**
     11  * Controller for login functions.
     12  */
     13 class LoginController extends Controller
     14 {
     15     /**
     16      * Direct the user to the Mastodon OAuth login page.
     17      *
     18      * First check to see if we are registered as an app with the Mastodon API,
     19      * then direct users to the OAuth login.
     20      *
     21      * @return Illuminate\Http\RedirectResponse Redirect to the OAuth login.
     22      */
     23     public function login()
     24     {
     25         # Check if this app is already registered.
     26         $app = DB::table('apps')
     27             ->where('server', env('MASTODON_DOMAIN'))
     28             ->first();
     29 
     30         if ($app == null)
     31         {
     32             # Register this app with the API server.
     33             $app_info = Mastodon::domain(env('MASTODON_DOMAIN'))
     34                 ->createApp(
     35                     env('APP_NAME'),
     36                     env('MASTODON_REDIRECT'),
     37                     implode(' ', config('services.mastodon.scopes')),
     38                     env('APP_URL')
     39                 );
     40 
     41             $client_id = $app_info['client_id'];
     42             $client_secret = $app_info['client_secret'];
     43 
     44             # Log the client details so we don't have to re-register.
     45             DB::table('apps')->insert([
     46                 'server' => env('MASTODON_DOMAIN'),
     47                 'client_name' => env('APP_NAME'),
     48                 'redirect_uris' => env('MASTODON_REDIRECT'),
     49                 'scopes' => join(' ', config('services.mastodon.scopes')),
     50                 'website' => env('APP_URL'),
     51                 'response_id' => $app_info['id'],
     52                 'client_id' => $client_id,
     53                 'client_secret' => $client_secret
     54             ]);
     55         }
     56         else
     57         {
     58             $client_id = $app->client_id;
     59             $client_secret = $app->client_secret;
     60         }
     61 
     62         # Set configs required for the redirect.
     63         config(['services.mastodon.domain' => env('MASTODON_DOMAIN')]);
     64         config(['services.mastodon.client_id' => $client_id]);
     65         config(['services.mastodon.client_secret' => $client_secret]);
     66 
     67         # Save this info to the session.
     68         session(['mastodon_domain' => env('MASTODON_DOMAIN')]);
     69         session(['client_id' => $client_id]);
     70         session(['client_secret' => $client_secret]);
     71 
     72         # Redirect the user to their instance to log in.
     73 	return Socialite::driver('mastodon')
     74             ->setScopes(config('services.mastodon.scope', config('services.mastodon.scopes')))
     75             ->redirect();
     76     }
     77 
     78     /**
     79      * Process the logged-in user.
     80      *
     81      * After logging in remotely, the user will be redirected to this callback.
     82      * We juggle their login details, then direct them to the home page.
     83      *
     84      * @return Illuminate\Routing\Redirector Redirect to the home page.
     85      */
     86     public function callback()
     87     {
     88         $domain = session('mastodon_domain');
     89         $client_id = session('client_id');
     90         $client_secret = session('client_secret');
     91 
     92         config(['services.mastodon.domain' => $domain]);
     93         config(['services.mastodon.client_id' => $client_id]);
     94         config(['services.mastodon.client_secret' => $client_secret]);
     95 
     96         # Get user data (token, etc.)
     97         $user = Socialite::driver('mastodon')->user();
     98         session(['user' => $user]);
     99 
    100         return redirect()->route('home');
    101     }
    102 }