Skip to content

Commit

Permalink
Merge pull request #1561 from appwrite/docs-react-native-oauth
Browse files Browse the repository at this point in the history
feat: document react native oauth
  • Loading branch information
loks0n authored Dec 30, 2024
2 parents b4847f7 + 6b0cbd5 commit b5438b0
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/routes/docs/products/auth/oauth2/+page.markdoc
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,44 @@ account.createOAuth2Session(
)
```
{% /tabsitem %}

{% tabsitem #react-native title="React Native" %}
```client-react-native
import { Client, Account, OAuthProvider } from "appwrite";
import * as Linking from 'expo-linking';
import * as WebBrowser from 'expo-web-browser';

const client = new Client()
.setEndpoint('https://cloud.appwrite.io/v1') // Your API Endpoint
.setProject('<PROJECT_ID>'); // Your project ID

const account = new Account(client);

// Create OAuth URL that works across Expo environments
const deepLink = new URL(Linking.createURL('oauth-callback', { isTripleSlashed: true }));
if (!deepLink.hostname) {
deepLink.hostname = 'localhost';
}

// Start OAuth flow
const loginUrl = await account.createOAuth2Token(
OAuthProvider.Github,
`${deepLink}/success`,
`${deepLink}/failure`,
['repo', 'user'] // scopes (optional)
);

const result = await WebBrowser.openAuthSessionAsync(loginUrl);

// Extract credentials from OAuth redirect URL
const url = new URL(result.url);
const secret = url.searchParams.get('secret');
const userId = url.searchParams.get('userId');

// Create session with OAuth credentials
await account.createSession(userId, secret);
```
{% /tabsitem %}
{% /tabs %}

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.
Expand Down

0 comments on commit b5438b0

Please sign in to comment.