Skip to content

Commit b5438b0

Browse files
authored
Merge pull request #1561 from appwrite/docs-react-native-oauth
feat: document react native oauth
2 parents b4847f7 + 6b0cbd5 commit b5438b0

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

src/routes/docs/products/auth/oauth2/+page.markdoc

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,44 @@ account.createOAuth2Session(
151151
)
152152
```
153153
{% /tabsitem %}
154+
155+
{% tabsitem #react-native title="React Native" %}
156+
```client-react-native
157+
import { Client, Account, OAuthProvider } from "appwrite";
158+
import * as Linking from 'expo-linking';
159+
import * as WebBrowser from 'expo-web-browser';
160+
161+
const client = new Client()
162+
.setEndpoint('https://cloud.appwrite.io/v1') // Your API Endpoint
163+
.setProject('<PROJECT_ID>'); // Your project ID
164+
165+
const account = new Account(client);
166+
167+
// Create OAuth URL that works across Expo environments
168+
const deepLink = new URL(Linking.createURL('oauth-callback', { isTripleSlashed: true }));
169+
if (!deepLink.hostname) {
170+
deepLink.hostname = 'localhost';
171+
}
172+
173+
// Start OAuth flow
174+
const loginUrl = await account.createOAuth2Token(
175+
OAuthProvider.Github,
176+
`${deepLink}/success`,
177+
`${deepLink}/failure`,
178+
['repo', 'user'] // scopes (optional)
179+
);
180+
181+
const result = await WebBrowser.openAuthSessionAsync(loginUrl);
182+
183+
// Extract credentials from OAuth redirect URL
184+
const url = new URL(result.url);
185+
const secret = url.searchParams.get('secret');
186+
const userId = url.searchParams.get('userId');
187+
188+
// Create session with OAuth credentials
189+
await account.createSession(userId, secret);
190+
```
191+
{% /tabsitem %}
154192
{% /tabs %}
155193

156194
You'll be redirected to the OAuth 2 provider's login page to log in. Once complete, your user will be redirected back to your app.

0 commit comments

Comments
 (0)