Issues (10)

example/index.php (1 issue)

1
<?php
2
/**
3
 * SocialConnect project
4
 * @author: Patsura Dmitry https://github.com/ovr <[email protected]>
5
 */
6
7
error_reporting(-1);
8
ini_set('display_errors', 1);
9
10
include_once __DIR__ . '/../vendor/autoload.php';
11
include_once __DIR__ . '/vendor/autoload.php';
12
$configureProviders = include_once 'config.php';
13
14
$httpClient = new \SocialConnect\HttpClient\Curl();
15
16
/**
17
 * By default We are using Curl class from SocialConnect/Common
18
 * but you can use Guzzle wrapper ^5.3|^6.0
19
 */
20
//$httpClient = new \Http\Adapter\Guzzle6\Client(
21
//    new \GuzzleHttp\Client()
22
//);
23
24
/**
25
 * Why We need Cache decorator for HTTP Client?
26
 * Providers like OpenID & OpenIDConnect require US
27
 * to request OpenID specification (and JWK(s) for OpenIDConnect)
28
 *
29
 * It's not a good idea to request it every time, because it's unneeded round trip to the server
30
 * if you are using OpenID or OpenIDConnect we suggest you to use cache
31
 *
32
 * If you don`t use providers like (Steam) from OpenID or OpenIDConnect
33
 * you may skip this because it's not needed
34
 */
35
//$httpClient = new \SocialConnect\HttpClient\Cache(
36
//    $httpClient,
37
//    /**
38
//     * You can use any library with PSR-16 (simple-cache) compatibility
39
//     */
40
//    new \Symfony\Component\Cache\Psr16Cache(
41
//        new \Symfony\Component\Cache\Adapter\PhpFilesAdapter(
42
//            'socialconnect',
43
//            0,
44
//            __DIR__ . '/cache'
45
//        )
46
//    )
47
//);
48
49
/**
50
 * By default collection factory is null, in this case Auth\Service will create
51
 * a new instance of \SocialConnect\Auth\CollectionFactory
52
 * you can use custom or register another providers by CollectionFactory instance
53
 */
54
$collectionFactory = null;
55
56
$service = new \SocialConnect\Auth\Service(
57
    new \SocialConnect\Common\HttpStack(
58
        $httpClient,
59
        new \SocialConnect\HttpClient\RequestFactory(),
60
        new \SocialConnect\HttpClient\StreamFactory()
61
    ),
62
    new \SocialConnect\Provider\Session\Session(),
63
    $configureProviders,
64
    $collectionFactory
65
);
66
67
$app = new \Slim\App(
68
    [
69
        'settings' => [
70
            'displayErrorDetails' => true
71
        ]
72
    ]
73
);
74
75
$app->any('/dump', function() {
76
    dump($_POST);
77
    dump($_GET);
78
    dump($_SERVER);
79
});
80
81
$app->get('/auth/cb/{provider}/', function (\Slim\Http\Request $request) use ($service) {
82
    $provider = strtolower($request->getAttribute('provider'));
83
84
    if (!$service->has($provider)) {
85
        throw new \Exception('Wrong $provider passed in url : ' . $provider);
86
    }
87
88
    $provider = $service->getProvider($provider);
89
90
    $accessToken = $provider->getAccessTokenByRequestParameters($_GET);
91
    dump($accessToken);
92
93
    dump($accessToken->getUserId());
94
    dump($accessToken->getExpires());
95
96
    $user = $provider->getIdentity($accessToken);
97
    dump($user);
98
});
99
100
$app->get('/', function () {
101
    include_once 'page.php';
102
});
103
104
$app->post('/', function () use ($service) {
105
    try {
106
        if (!empty($_POST['provider'])) {
107
            $providerName = $_POST['provider'];
108
        } else {
109
            throw new \Exception('No provider passed in POST Request');
110
        }
111
112
        $provider = $service->getProvider($providerName);
113
114
        header('Location: ' . $provider->makeAuthUrl());
115
    } catch (\Exception $e) {
116
        echo $e->getMessage();
117
    }
118
    exit;
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
119
});
120
121
$app->run();
122