-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathLoginController.php
More file actions
135 lines (117 loc) · 4.38 KB
/
LoginController.php
File metadata and controls
135 lines (117 loc) · 4.38 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\{User};
use Auth;
use Exception;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Log;
use Psr\Log\NullLogger;
use RestCord\DiscordClient;
use Socialite;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
/**
* Redirect the user to the Discord authentication page.
*
* @return Response
*/
public function redirectToDiscord() {
if (app()->environment('local') && env('LOGIN_USING_ID')) {
Auth::loginUsingId(env('LOGIN_USING_ID'));
return redirect()->route('home');
}
return Socialite::driver('discord')
// Don't require Discord to send back an email
// https://discord.com/developers/docs/topics/oauth2#shared-resources-oauth2-scopes
->setScopes(['identify', 'guilds']) // If changing these scopes, update the call to refresh a user's access (search 'refresh_token')
// Don't prompt the user to accept our app's usage of their Discord profile EVERY time (only on first signup)
// https://discord.com/developers/docs/topics/oauth2#authorization-code-grant-authorization-url-example
->with(['prompt' => 'none'])
->redirect();
}
/**
* Obtain the user information from Discord.
*
* @return Response
*/
public function handleDiscordCallback()
{
try {
$unauthUser = Socialite::driver('discord')->user();
} catch (Exception $e) {
return redirect('auth/discord');
}
$id = $unauthUser->getId();
if (!$id) {
abort(403, __("Didn't receive your ID from Discord. Try again."));
}
$discord = new DiscordClient([
'token' => $unauthUser->token,
'tokenType' => 'OAuth',
'version' => '9',
'logger' => new NullLogger(),
]);
$authUser = $this->findUser($unauthUser, 'discord');
if ($authUser) {
if ($authUser->banned_at) {
abort(403, 'You have been banned.');
}
Auth::login($authUser, true);
Auth::user()->update([
'discord_token' => $unauthUser->token,
'discord_refresh_token' => $unauthUser->refreshToken,
'discord_token_expiry' => date('Y-m-d H:i:s', time() + $unauthUser->expiresIn),
]);
return redirect()->route('home');
} else if ($unauthUser) {
$user = User::create([
'username' => $unauthUser->getName(),
'discord_username' => $unauthUser->getNickname(),
'discord_id' => $id,
'discord_avatar' => $unauthUser->getAvatar(),
'discord_token' => $unauthUser->token,
'discord_refresh_token' => $unauthUser->refreshToken,
'discord_token_expiry' => date('Y-m-d H:i:s', time() + $unauthUser->expiresIn),
'password' => null,
]);
Auth::login($user, true);
return redirect()->route('home');
} else {
abort(403, __("Something went wrong with the data Discord sent us. Try again."));
}
}
/**
* Used when a user attempts to log in via a social service
*
* @param $user Laravel\Socialite\AbstractUser
* @param $service string The name of the service we're connecting to. eg. 'google' or 'facebook'
*
* @return App\User
*/
private function findUser($user, $service) {
$serviceField = $service . '_id';
$authUser = User::where($serviceField, $user->id)->first();
return $authUser;
}
}