2022-10-30 16:28:14 +08:00
|
|
|
<script>
|
|
|
|
import ApiClient from "@/utils/ApiClient";
|
|
|
|
import CommonHelper from "@/utils/CommonHelper";
|
|
|
|
import CodeBlock from "@/components/base/CodeBlock.svelte";
|
|
|
|
import SdkTabs from "@/components/collections/docs/SdkTabs.svelte";
|
2023-05-20 10:58:52 +08:00
|
|
|
import FieldsQueryParam from "@/components/collections/docs/FieldsQueryParam.svelte";
|
2022-10-30 16:28:14 +08:00
|
|
|
|
2023-08-15 02:20:49 +08:00
|
|
|
export let collection;
|
2022-10-30 16:28:14 +08:00
|
|
|
|
|
|
|
let responseTab = 200;
|
|
|
|
let responses = [];
|
|
|
|
|
|
|
|
$: backendAbsUrl = CommonHelper.getApiExampleUrl(ApiClient.baseUrl);
|
|
|
|
|
|
|
|
$: responses = [
|
|
|
|
{
|
|
|
|
code: 200,
|
|
|
|
body: JSON.stringify(
|
|
|
|
{
|
2023-04-13 20:38:12 +08:00
|
|
|
token: "JWT_AUTH_TOKEN",
|
2022-10-30 16:28:14 +08:00
|
|
|
record: CommonHelper.dummyCollectionRecord(collection),
|
|
|
|
meta: {
|
|
|
|
id: "abc123",
|
|
|
|
name: "John Doe",
|
|
|
|
username: "john.doe",
|
|
|
|
email: "test@example.com",
|
|
|
|
avatarUrl: "https://example.com/avatar.png",
|
2023-04-13 20:38:12 +08:00
|
|
|
accessToken: "...",
|
|
|
|
refreshToken: "...",
|
|
|
|
rawUser: {},
|
2022-10-30 16:28:14 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
null,
|
|
|
|
2
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
code: 400,
|
|
|
|
body: `
|
|
|
|
{
|
|
|
|
"code": 400,
|
|
|
|
"message": "An error occurred while submitting the form.",
|
|
|
|
"data": {
|
|
|
|
"provider": {
|
|
|
|
"code": "validation_required",
|
|
|
|
"message": "Missing required value."
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<h3 class="m-b-sm">Auth with OAuth2 ({collection.name})</h3>
|
|
|
|
<div class="content txt-lg m-b-sm">
|
2022-11-13 06:38:18 +08:00
|
|
|
<p>Authenticate with an OAuth2 provider and returns a new auth token and record data.</p>
|
2022-10-30 16:28:14 +08:00
|
|
|
<p>
|
2023-04-13 20:38:12 +08:00
|
|
|
For more details please check the
|
2022-10-30 16:28:14 +08:00
|
|
|
<a href={import.meta.env.PB_OAUTH2_EXAMPLE} target="_blank" rel="noopener noreferrer">
|
2023-04-13 20:38:12 +08:00
|
|
|
OAuth2 integration documentation
|
2022-10-30 16:28:14 +08:00
|
|
|
</a>.
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<SdkTabs
|
|
|
|
js={`
|
|
|
|
import PocketBase from 'pocketbase';
|
|
|
|
|
|
|
|
const pb = new PocketBase('${backendAbsUrl}');
|
|
|
|
|
|
|
|
...
|
|
|
|
|
2023-04-17 21:29:56 +08:00
|
|
|
// OAuth2 authentication with a single realtime call.
|
2023-04-13 20:38:12 +08:00
|
|
|
//
|
2023-04-17 21:29:56 +08:00
|
|
|
// Make sure to register ${backendAbsUrl}/api/oauth2-redirect as redirect url.
|
2023-04-13 20:38:12 +08:00
|
|
|
const authData = await pb.collection('users').authWithOAuth2({ provider: 'google' });
|
2022-10-30 16:28:14 +08:00
|
|
|
|
2023-04-17 21:29:56 +08:00
|
|
|
// OR authenticate with manual OAuth2 code exchange
|
|
|
|
// const authData = await pb.collection('users').authWithOAuth2Code(...);
|
|
|
|
|
2022-10-30 16:28:14 +08:00
|
|
|
// after the above you can also access the auth data from the authStore
|
|
|
|
console.log(pb.authStore.isValid);
|
|
|
|
console.log(pb.authStore.token);
|
|
|
|
console.log(pb.authStore.model.id);
|
|
|
|
|
2023-04-13 20:38:12 +08:00
|
|
|
// "logout" the last authenticated model
|
2022-10-30 16:28:14 +08:00
|
|
|
pb.authStore.clear();
|
|
|
|
`}
|
|
|
|
dart={`
|
|
|
|
import 'package:pocketbase/pocketbase.dart';
|
2023-04-13 20:38:12 +08:00
|
|
|
import 'package:url_launcher/url_launcher.dart';
|
2022-10-30 16:28:14 +08:00
|
|
|
|
|
|
|
final pb = PocketBase('${backendAbsUrl}');
|
|
|
|
|
|
|
|
...
|
|
|
|
|
2023-04-17 21:29:56 +08:00
|
|
|
// OAuth2 authentication with a single realtime call.
|
2023-04-13 20:38:12 +08:00
|
|
|
//
|
2023-04-17 21:29:56 +08:00
|
|
|
// Make sure to register ${backendAbsUrl}/api/oauth2-redirect as redirect url.
|
2023-04-13 20:38:12 +08:00
|
|
|
final authData = await pb.collection('users').authWithOAuth2('google', (url) async {
|
|
|
|
await launchUrl(url);
|
|
|
|
});
|
2022-10-30 16:28:14 +08:00
|
|
|
|
2023-04-17 21:29:56 +08:00
|
|
|
// OR authenticate with manual OAuth2 code exchange
|
|
|
|
// final authData = await pb.collection('users').authWithOAuth2Code(...);
|
|
|
|
|
2022-10-30 16:28:14 +08:00
|
|
|
// after the above you can also access the auth data from the authStore
|
|
|
|
print(pb.authStore.isValid);
|
|
|
|
print(pb.authStore.token);
|
|
|
|
print(pb.authStore.model.id);
|
|
|
|
|
2023-04-13 20:38:12 +08:00
|
|
|
// "logout" the last authenticated model
|
2022-10-30 16:28:14 +08:00
|
|
|
pb.authStore.clear();
|
|
|
|
`}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<h6 class="m-b-xs">API details</h6>
|
|
|
|
<div class="alert alert-success">
|
|
|
|
<strong class="label label-primary">POST</strong>
|
|
|
|
<div class="content">
|
|
|
|
<p>
|
|
|
|
/api/collections/<strong>{collection.name}</strong>/auth-with-oauth2
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="section-title">Body Parameters</div>
|
|
|
|
<table class="table-compact table-border m-b-base">
|
|
|
|
<thead>
|
|
|
|
<tr>
|
|
|
|
<th>Param</th>
|
|
|
|
<th>Type</th>
|
|
|
|
<th width="50%">Description</th>
|
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
<tr>
|
|
|
|
<td>
|
|
|
|
<div class="inline-flex">
|
|
|
|
<span class="label label-success">Required</span>
|
|
|
|
<span>provider</span>
|
|
|
|
</div>
|
|
|
|
</td>
|
|
|
|
<td>
|
|
|
|
<span class="label">String</span>
|
|
|
|
</td>
|
|
|
|
<td>The name of the OAuth2 client provider (eg. "google").</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>
|
|
|
|
<div class="inline-flex">
|
|
|
|
<span class="label label-success">Required</span>
|
|
|
|
<span>code</span>
|
|
|
|
</div>
|
|
|
|
</td>
|
|
|
|
<td>
|
|
|
|
<span class="label">String</span>
|
|
|
|
</td>
|
|
|
|
<td>The authorization code returned from the initial request.</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>
|
|
|
|
<div class="inline-flex">
|
|
|
|
<span class="label label-success">Required</span>
|
|
|
|
<span>codeVerifier</span>
|
|
|
|
</div>
|
|
|
|
</td>
|
|
|
|
<td>
|
|
|
|
<span class="label">String</span>
|
|
|
|
</td>
|
|
|
|
<td>The code verifier sent with the initial request as part of the code_challenge.</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>
|
|
|
|
<div class="inline-flex">
|
|
|
|
<span class="label label-success">Required</span>
|
|
|
|
<span>redirectUrl</span>
|
|
|
|
</div>
|
|
|
|
</td>
|
|
|
|
<td>
|
|
|
|
<span class="label">String</span>
|
|
|
|
</td>
|
|
|
|
<td>The redirect url sent with the initial request.</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>
|
|
|
|
<div class="inline-flex">
|
|
|
|
<span class="label label-warning">Optional</span>
|
|
|
|
<span>createData</span>
|
|
|
|
</div>
|
|
|
|
</td>
|
|
|
|
<td>
|
|
|
|
<span class="label">Object</span>
|
|
|
|
</td>
|
|
|
|
<td>
|
|
|
|
<p>Optional data that will be used when creating the auth record on OAuth2 sign-up.</p>
|
|
|
|
<p>
|
|
|
|
The created auth record must comply with the same requirements and validations in the
|
|
|
|
regular <strong>create</strong> action.
|
|
|
|
<br />
|
|
|
|
<em>
|
|
|
|
The data can only be in <code>json</code>, aka. <code>multipart/form-data</code> and files
|
|
|
|
upload currently are not supported during OAuth2 sign-ups.
|
|
|
|
</em>
|
|
|
|
</p>
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
|
|
|
|
<div class="section-title">Query parameters</div>
|
|
|
|
<table class="table-compact table-border m-b-base">
|
|
|
|
<thead>
|
|
|
|
<tr>
|
|
|
|
<th>Param</th>
|
|
|
|
<th>Type</th>
|
|
|
|
<th width="60%">Description</th>
|
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
<tr>
|
|
|
|
<td>expand</td>
|
|
|
|
<td>
|
|
|
|
<span class="label">String</span>
|
|
|
|
</td>
|
|
|
|
<td>
|
|
|
|
Auto expand record relations. Ex.:
|
|
|
|
<CodeBlock content={`?expand=relField1,relField2.subRelField`} />
|
|
|
|
Supports up to 6-levels depth nested relations expansion. <br />
|
|
|
|
The expanded relations will be appended to the record under the
|
|
|
|
<code>expand</code> property (eg. <code>{`"expand": {"relField1": {...}, ...}`}</code>).
|
|
|
|
<br />
|
2022-11-13 06:38:18 +08:00
|
|
|
Only the relations to which the request user has permissions to <strong>view</strong> will be expanded.
|
2022-10-30 16:28:14 +08:00
|
|
|
</td>
|
|
|
|
</tr>
|
2023-05-20 10:58:52 +08:00
|
|
|
<FieldsQueryParam />
|
2022-10-30 16:28:14 +08:00
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
|
|
|
|
<div class="section-title">Responses</div>
|
|
|
|
<div class="tabs">
|
2023-09-01 17:44:43 +08:00
|
|
|
<div class="tabs-header compact combined left">
|
2022-10-30 16:28:14 +08:00
|
|
|
{#each responses as response (response.code)}
|
|
|
|
<button
|
|
|
|
class="tab-item"
|
|
|
|
class:active={responseTab === response.code}
|
|
|
|
on:click={() => (responseTab = response.code)}
|
|
|
|
>
|
|
|
|
{response.code}
|
|
|
|
</button>
|
|
|
|
{/each}
|
|
|
|
</div>
|
|
|
|
<div class="tabs-content">
|
|
|
|
{#each responses as response (response.code)}
|
|
|
|
<div class="tab-item" class:active={responseTab === response.code}>
|
|
|
|
<CodeBlock content={response.body} />
|
|
|
|
</div>
|
|
|
|
{/each}
|
|
|
|
</div>
|
|
|
|
</div>
|