You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/2-features/17-oauth.md
+35-26Lines changed: 35 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,26 +1,26 @@
1
1
---
2
2
title: OAuth
3
-
description: "Tempest's OAuth provides a way to authenticate users with many different OAuth providers, such as GitHub, Google, Discord, and many others."
3
+
description: "Learn how to implement OAuth to authenticate users with many different providers, such as GitHub, Google, Discord, and many others."
4
4
keywords: "Experimental"
5
5
---
6
6
7
7
## Overview
8
8
9
9
Tempest provides the ability to authenticate users with many OAuth providers, such as GitHub, Google, Discord, and many others, using the same interface.
10
10
11
-
This implementation is built on top of [OAuth 2.0 Client](https://github.com/thephpleague/oauth2-client)—a reliable, battle-tested OAuth 2.0 client library.
11
+
This implementation is built on top of the PHP league's [OAuth client](https://github.com/thephpleague/oauth2-client)—a reliable, battle-tested OAuth 2.0 client library.
12
12
13
13
## Getting started
14
14
15
15
To get started with OAuth, you will first need to create a configuration file for your desired OAuth provider.
16
16
17
-
Tempest provides a different configuration object for each provider. For instance, if you wish to authenticate users with GitHub, you may create a `github.config.php` file returning an instance of {b`Tempest\Auth\OAuth\Config\GitHubOAuthConfig`}:
17
+
Tempest provides a [different configuration object for each provider](#available-providers). For instance, if you wish to authenticate users with GitHub, you may create a `github.config.php` file returning an instance of {b`Tempest\Auth\OAuth\Config\GitHubOAuthConfig`}:
@@ -31,58 +31,67 @@ Once your OAuth provider is configured, you may interact with it by using the {`
31
31
32
32
## Implementing the OAuth flow
33
33
34
-
To implement a complete OAuth flow for your application, you will need to use the {b`Tempest\Auth\OAuth\OAuthClient`} interface to redirect the user to the OAuth provider's authorization page, and fetch the user's information in the controller action to which the OAuth provider redirects back.
34
+
To implement a complete OAuth flow for your application, you will need two routes.
35
35
36
-
The following is an example of a full OAuth flow, including CSRF protection, saving or updating the user, and authenticating them against the application:
36
+
- The first one will redirect the user to the OAuth provider's authorization page,
37
+
- The second one, which will be redirected to once the user authorizes your application, will fetch the user's information thanks to the code provided by the OAuth provider.
37
38
38
-
```php app/Auth/GitHubOAuthController.php
39
+
The {b`Tempest\Auth\OAuth\OAuthClient`} interface has the necessary methods to handle both parts of the flow. The following is an example of a complete OAuth flow, including CSRF protection, creating or updating the user, and authenticating them against the application:
// Finally, authenticates the user in the application
74
83
$this->authenticator->authenticate($user);
75
84
76
85
return new Redirect('/');
77
86
}
78
87
}
79
88
```
80
89
81
-
Of course, this example assumes that an [authenticatable model](../2-features/04-authentication.md#authentication)is configured.
90
+
Of course, this example assumes that the database and an [authenticatable model](../2-features/04-authentication.md#authentication)are configured.
82
91
83
92
### Working with the OAuth user
84
93
85
-
When an OAuth flow is completed, you will receive an {b`Tempest\Auth\OAuth\OAuthUser`} object containing the user's information from the OAuth provider:
94
+
When an OAuth flow is completed and you call `fetchUser`, you will receive an {b`Tempest\Auth\OAuth\OAuthUser`} object containing the user's information from the OAuth provider:
86
95
87
96
```php
88
97
$user = $this->oauth->fetchUser($code);
@@ -100,18 +109,18 @@ As seen in the example above, you can use this information to create or update a
100
109
101
110
## Configuring a provider
102
111
103
-
Most providers require only a `clientId`, `clientSecret` and `redirectUri`, but some might need other parameters. A typical configuration looks like the following:
112
+
Most providers require only a `clientId`, `clientSecret` and `redirectTo`, but some might need other parameters. A typical configuration looks like the following:
Note that the `redirectUri` accepts a tuple of a controller class and a method name, which will be resolved to the full URL of the route handled by that method. You may also provide an URI path if you prefer.
123
+
Note that the `redirectTo` accepts a tuple of a controller class and a method name, which will be resolved to the full URL of the route handled by that method. You may also provide an URI path if you prefer.
0 commit comments