-
Notifications
You must be signed in to change notification settings - Fork 3
/
routes.php
104 lines (83 loc) · 3.91 KB
/
routes.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
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
<?php
// http://home.tohur.com/tohur/socialconnect/Google?s=/&f=/login
Route::get('socialconnect/{provider}', array("as" => "tohur_socialconnect_provider", 'middleware' => ['web'], function ($provider_name, $action = "") {
$success_redirect = Input::get('s', '/');
$error_redirect = Input::get('f', '/login');
Session::flash('tohur_socialconnect_successredirect', $success_redirect);
Session::flash('tohur_socialconnect_errorredirect', $error_redirect);
$provider_class = Tohur\SocialConnect\Classes\ProviderManager::instance()
->resolveProvider($provider_name);
if (!$provider_class)
return Redirect::to($error_redirect)->withErrors("Unknown login provider: $provider_name.");
$provider = $provider_class::instance();
return $provider->redirectToProvider();
}))->where(['provider' => '[A-Z][a-zA-Z ]+']);
Route::get('socialconnect/{provider}/callback', ['as' => 'tohur_socialconnect_provider_callback', 'middleware' => ['web'], function ($provider_name) {
$success_redirect = Session::get('tohur_socialconnect_successredirect', '/');
$error_redirect = Session::get('tohur_socialconnect_errorredirect', '/login');
$provider_class = Tohur\SocialConnect\Classes\ProviderManager::instance()
->resolveProvider($provider_name);
if (!$provider_class)
return Redirect::to($error_redirect)->withErrors("Unknown login provider: $provider_name.");
$provider = $provider_class::instance();
try {
// This will contain [token => ..., email => ..., ...]
$provider_response = $provider->handleProviderCallback($provider_name);
if (!is_array($provider_response))
return Redirect::to($error_redirect);
} catch (Exception $e) {
// Log the error
Log::error($e->getMessage() . ' in ' . $e->getFile() . ' on line ' . $e->getLine());
return Redirect::to($error_redirect)->withErrors([$e->getMessage()]);
}
if ($provider_name == 'Twitter') {
$provider_details = [
'provider_id' => $provider_name,
'provider_token' => $provider_response['token'],
];
} else {
$provider_details = [
'provider_id' => $provider_name,
'provider_userid' => $provider_response['id'],
'provider_token' => $provider_response['token'],
'provider_refreshToken' => $provider_response['refreshToken'],
'provider_expiresIn' => $provider_response['expiresIn'],
];
}
$user_details = array_except($provider_response, 'token');
// Backend logins
if ($success_redirect == Backend::url()) {
$user = Tohur\SocialConnect\Classes\UserManager::instance()
->findBackendUserByEmail($user_details['email']);
if (!$user)
throw new October\Rain\Auth\AuthException(sprintf(
'Administrator with email address "%s" not found.', $user_details['email']
));
// Support custom login handling
$result = Event::fire('tohur.socialconnect.handleBackendLogin', [
$provider_details, $provider_response, $user
], true);
if ($result)
return $result;
BackendAuth::login($user, true);
// Load version updates
System\Classes\UpdateManager::instance()->update();
// Log the sign in event
Backend\Models\AccessLog::add($user);
} // Frontend Logins
else {
// Grab the user associated with this provider. Creates or attach one if need be.
$user = \Tohur\SocialConnect\Classes\UserManager::instance()->find(
$provider_details,
$user_details
);
// Support custom login handling
$result = Event::fire('tohur.socialconnect.handleLogin', [
$provider_details, $provider_response, $user
], true);
if ($result)
return $result;
Auth::login($user);
}
return Redirect::to($success_redirect);
}]);