forked from Flynsarmy/oc-sociallogin-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.php
119 lines (95 loc) · 4.32 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
// http://home.flynsarmy.com/flynsarmy/sociallogin/Google?s=/&f=/login
Route::get(
'flynsarmy/sociallogin/{provider}',
[
"as" => "flynsarmy_sociallogin_provider",
'middleware' => ['web'],
function ($provider_name, $action = "") {
$success_redirect = Input::get('s', '/');
$error_redirect = Input::get('f', '/login');
Session::flash('flynsarmy_sociallogin_successredirect', $success_redirect);
Session::flash('flynsarmy_sociallogin_errorredirect', $error_redirect);
$provider_class = Flynsarmy\SocialLogin\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::any(
'flynsarmy/sociallogin/{provider}/callback',
[
'as' => 'flynsarmy_sociallogin_provider_callback',
'middleware' => ['web'],
function ($provider_name) {
$success_redirect = Session::get('flynsarmy_sociallogin_successredirect', '/');
$error_redirect = Session::get('flynsarmy_sociallogin_errorredirect', '/login');
$provider_class = Flynsarmy\SocialLogin\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()]);
}
ksort($provider_response['token']);
$provider_details = [
'provider_id' => $provider_name,
'provider_token' => $provider_response['token'],
];
$user_details = $provider_response['profile'];
// Backend logins
if ($success_redirect == Backend::url()) {
$user = Flynsarmy\SocialLogin\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('flynsarmy.sociallogin.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 = \Flynsarmy\SocialLogin\Classes\UserManager::instance()->find(
$provider_details,
$user_details
);
// Support custom login handling
$result = Event::fire('flynsarmy.sociallogin.handleLogin', [
$provider_details, $provider_response, $user
], true);
if ($result) {
return $result;
}
Auth::login($user);
}
return Redirect::to($success_redirect);
}
]
);