-
Notifications
You must be signed in to change notification settings - Fork 26
/
OAuthController.php
88 lines (76 loc) · 3.13 KB
/
OAuthController.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
<?php
namespace App\Http\Controllers;
use App\Services\SallaAuthService;
use Illuminate\Http\Request;
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
class OAuthController extends Controller
{
/**
* @var SallaAuthService
*/
private $service;
public function __construct(SallaAuthService $service)
{
$this->service = $service;
}
public function redirect()
{
return redirect($this->service->getProvider()->getAuthorizationUrl());
}
public function callback(Request $request)
{
abort_if($this->service->isEasyMode(), 401,'The Authorization mode is not supported');
// Try to obtain an access token by utilizing the authorisations code grant.
try {
$token = $this->service->getAccessToken('authorization_code', [
'code' => $request->code ?? ''
]);
/** @var \Salla\OAuth2\Client\Provider\SallaUser $user */
$user = $this->service->getResourceOwner($token);
/**
* {
* "id": 181690847,
* "name": "eman elsbay",
* "email": "[email protected]",
* "mobile": "555454545",
* "role": "user",
* "created_at": "2018-04-28 17:46:25",
* "store": {
* "id": 633170215,
* "owner_id": 181690847,
* "owner_name": "eman elsbay",
* "username": "good-store",
* "name": "متجر الموضة",
* "avatar": "https://cdn.salla.sa/XrXj/g2aYPGNvafLy0TUxWiFn7OqPkKCJFkJQz4Pw8WsS.jpeg",
* "store_location": "26.989000873354787,49.62477639657287",
* "plan": "special",
* "status": "active",
* "created_at": "2019-04-28 17:46:25"
* }
* }
*/
// var_export($user->toArray());
// echo 'User ID: '.$user->getId()."<br>";
// echo 'User Name: '.$user->getName()."<br>";
// echo 'Store ID: '.$user->getStoreID()."<br>";
// echo 'Store Name: '.$user->getStoreName()."<br>";
//
// 🥳
//
// You can now save the access token and refresh token in your database
// with the merchant details and redirect him again to Salla dashboard (https://s.salla.sa/apps)
$request->user()->token()->delete();
$request->user()->token()->create([
'access_token' => $token->getToken(),
'expires_in' => $token->getExpires(),
'refresh_token' => $token->getRefreshToken()
]);
// TODO :: change it later to https://s.salla.sa/apps before go alive
return redirect('/dashboard');
} catch (IdentityProviderException $e) {
// Failed to get the access token or merchant details.
// show an error message to the merchant with good UI
return redirect('/dashboard')->withStatus($e->getMessage());
}
}
}