-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathweb.php
95 lines (76 loc) · 2.79 KB
/
web.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
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
/** @var \Laravel\Lumen\Routing\Router $router */
/*
|--------------------------------------------------------------------------
| Util Methods
|--------------------------------------------------------------------------
|
| We stub out customer logical space methods
*/
$environment = env('APP_ENV', 'production');
$widget_uuid = env('WIDGET_UUID', '');
$TOKEN_ISSUER_URLS = [
"production" => 'https://widget.ocrolus.com',
];
$API_ISSUER_URLS = [
"production" => 'https://auth.ocrolus.com',
];
function is_user_logged_in() {
return false;
}
function get_current_user_id() {
return 7;
}
function getUserExternalId() {
// Check if the user is logged in.
if (is_user_logged_in()) {
// Get the current user's ID.
$user_id = get_current_user_id();
return $user_id;
} else {
$user_id = "999999";
return $user_id;
}
}
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It is a breeze. Simply tell Lumen the URIs it should respond to
| and give it the Closure to call when that URI is requested.
|
*/
$router->get('/', function () use ($router) {
return $router->app->version();
});
$router->get('/token', function (Request $request) use ($router, $TOKEN_ISSUER_URLS, $environment, $widget_uuid) {
// Get user token from the request headers or set a default value (e.g., 1234).
$user_token = $request->header('Authorization') || '1234';
// You should define the getUserExternalId function to retrieve the user's external ID.
$user_id = getUserExternalId($user_token);
// Define the request data to send to Ocrolus.
$request_data = [
'client_id' => 'ls2wy5PH86OOVTFw7TkAB3Kd8IB0OBU9',
'client_secret' => 'lsrItRO3TKY2POYO2jMstb_PGj5ebyNfy3kgok07hJzNsApiQIE3qEut_buJSKt9',
'custom_id' => $user_id,
'grant_type' => 'client_credentials',
'name' => 'bookName', // Customize the book name as needed.
];
// Make a POST request to Ocrolus using the Laravel HTTP client.
$ocrolus_response = Http::post(
"{$TOKEN_ISSUER_URLS[$environment]}/v1/widget/{$widget_uuid}/token",
$request_data
);
// Log the request data and response for debugging.
error_log(json_encode($request_data));
error_log($ocrolus_response->body());
if ($ocrolus_response->failed()) {
return response()->json(['error' => 'ocrolus_error: Failed to get a token from Ocrolus'], 500);
}
$response_data = $ocrolus_response->json();
return response()->json(['accessToken' => $response_data['access_token']]);
});