Skip to content
This repository was archived by the owner on Sep 29, 2023. It is now read-only.

Commit cc3f9be

Browse files
author
Vincent (Wen Yu) Ge
committedJun 13, 2023
complete the example GitHub guide
1 parent eb5a98e commit cc3f9be

File tree

1 file changed

+221
-2
lines changed

1 file changed

+221
-2
lines changed
 

Diff for: ‎app/views/docs/oauth-providers/github.md

+221-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,49 @@ account.createOAuth2Session('github', '[LINK_ON_SUCCESS]', '[LINK_ON_FAILURE]');
4444
```
4545

4646
### Flutter
47+
You can use OAuth in your Flutter application, but some platforms like Android and Apple requires additional configuration to enable the OAuth callback, so the your users can be redirected back to your app..
48+
49+
#### Android OAuth callback
50+
51+
In order to capture the Appwrite OAuth callback url, the following activity needs to be added inside the `<application>` tag, along side the existing `<activity>` tags in your `AndroidManifest.xml`. Be sure to replace the `[PROJECT_ID]` string with your actual Appwrite project ID. You can find your Appwrite project ID in your project settings screen in your Appwrite console.
52+
53+
```xml
54+
<manifest ...>
55+
...
56+
<application ...>
57+
...
58+
<!-- Add this inside the `<application>` tag, along side the existing `<activity>` tags -->
59+
<activity android:name="io.appwrite.views.CallbackActivity" android:exported="true">
60+
<intent-filter android:label="android_web_auth">
61+
<action android:name="android.intent.action.VIEW" />
62+
<category android:name="android.intent.category.DEFAULT" />
63+
<category android:name="android.intent.category.BROWSABLE" />
64+
<data android:scheme="appwrite-callback-[PROJECT_ID]" />
65+
</intent-filter>
66+
</activity>
67+
</application>
68+
</manifest>
69+
```
70+
71+
#### Apple
72+
In order to capture the Appwrite OAuth callback url, the following URL scheme needs to added to your `Info.plist`
73+
```xml
74+
<key>CFBundleURLTypes</key>
75+
<array>
76+
<dict>
77+
<key>CFBundleTypeRole</key>
78+
<string>Editor</string>
79+
<key>CFBundleURLName</key>
80+
<string>io.appwrite</string>
81+
<key>CFBundleURLSchemes</key>
82+
<array>
83+
<string>appwrite-callback-[PROJECT_ID]</string>
84+
</array>
85+
</dict>
86+
</array>
87+
```
4788

48-
For Flutter make sure to follow [getting started for Flutter](https://appwrite.io/docs/getting-started-for-flutter#addYourPlatform) and setup configuration required for each platform to successfully authenticate user with OAuth2 providers.
89+
To authenticate a user in your Flutter application, use the [Create OAuth2 Session](https://appwrite.io/docs/client/account?sdk=flutter-default#accountCreateOAuth2Session) endpoint.
4990

5091
```dart
5192
import 'package:appwrite/appwrite.dart';
@@ -67,11 +108,141 @@ void main() async {
67108
```
68109

69110
### Android (Kotlin)
111+
Before you can add OAuth to your Android app, you need to setup a callback for your OAuth flow.
112+
113+
In order to capture the Appwrite OAuth callback url, the following activity needs to be added inside the `<application>` tag, along side the existing `<activity>` tags in your `AndroidManifest.xml`. Be sure to replace the `[PROJECT_ID]` string with your actual Appwrite project ID. You can find your Appwrite project ID in your project settings screen in your Appwrite console.
114+
115+
```xml
116+
<manifest ...>
117+
...
118+
<application ...>
119+
...
120+
<!-- Add this inside the `<application>` tag, along side the existing `<activity>` tags -->
121+
<activity android:name="io.appwrite.views.CallbackActivity" android:exported="true">
122+
<intent-filter android:label="android_web_auth">
123+
<action android:name="android.intent.action.VIEW" />
124+
<category android:name="android.intent.category.DEFAULT" />
125+
<category android:name="android.intent.category.BROWSABLE" />
126+
<data android:scheme="appwrite-callback-[PROJECT_ID]" />
127+
</intent-filter>
128+
</activity>
129+
</application>
130+
</manifest>
131+
```
132+
133+
To authenticate a user in your Android application, use the [Create OAuth2 Session](https://appwrite.io/docs/client/account?sdk=android-kotlin#accountCreateOAuth2Session) endpoint.
134+
135+
```kotlin
136+
import io.appwrite.Client
137+
import io.appwrite.services.Account
138+
139+
val client = Client(context)
140+
.setEndpoint("https://cloud.appwrite.io/v1") // Your API Endpoint
141+
.setProject("[PROJECT_ID]") // Your project ID
142+
143+
val account = Account(client)
144+
145+
account.createOAuth2Session(
146+
provider = "github",
147+
)
148+
```
70149

71150
### Android (Java)
151+
Before you can add OAuth to your Android app, you need to setup a callback for your OAuth flow.
152+
153+
In order to capture the Appwrite OAuth callback url, the following activity needs to be added inside the `<application>` tag, along side the existing `<activity>` tags in your `AndroidManifest.xml`. Be sure to replace the `[PROJECT_ID]` string with your actual Appwrite project ID. You can find your Appwrite project ID in your project settings screen in your Appwrite console.
154+
155+
```xml
156+
<manifest ...>
157+
...
158+
<application ...>
159+
...
160+
<!-- Add this inside the `<application>` tag, along side the existing `<activity>` tags -->
161+
<activity android:name="io.appwrite.views.CallbackActivity" android:exported="true">
162+
<intent-filter android:label="android_web_auth">
163+
<action android:name="android.intent.action.VIEW" />
164+
<category android:name="android.intent.category.DEFAULT" />
165+
<category android:name="android.intent.category.BROWSABLE" />
166+
<data android:scheme="appwrite-callback-[PROJECT_ID]" />
167+
</intent-filter>
168+
</activity>
169+
</application>
170+
</manifest>
171+
```
172+
173+
To authenticate a user in your Android application, use the [Create OAuth2 Session](https://appwrite.io/docs/client/account?sdk=android-java#accountCreateOAuth2Session) endpoint.
174+
175+
```java
176+
import io.appwrite.Client;
177+
import io.appwrite.coroutines.CoroutineCallback;
178+
import io.appwrite.services.Account;
179+
180+
Client client = new Client(context)
181+
.setEndpoint("https://cloud.appwrite.io/v1") // Your API Endpoint
182+
.setProject("[PROJECT_ID]"); // Your project ID
183+
184+
Account account = new Account(client);
185+
186+
account.createOAuth2Session(
187+
"github",
188+
new CoroutineCallback<>((result, error) -> {
189+
if (error != null) {
190+
error.printStackTrace();
191+
return;
192+
}
193+
194+
Log.d("Appwrite", result.toString());
195+
})
196+
);
197+
```
72198

73199
### iOS (Swift)
74200

201+
In order to capture the Appwrite OAuth callback url, the following URL scheme needs to added to your `Info.plist`
202+
```xml
203+
<key>CFBundleURLTypes</key>
204+
<array>
205+
<dict>
206+
<key>CFBundleTypeRole</key>
207+
<string>Editor</string>
208+
<key>CFBundleURLName</key>
209+
<string>io.appwrite</string>
210+
<key>CFBundleURLSchemes</key>
211+
<array>
212+
<string>appwrite-callback-[PROJECT_ID]</string>
213+
</array>
214+
</dict>
215+
</array>
216+
```
217+
218+
If you're using UIKit, you'll also need to add a hook to your `SceneDelegate.swift` file to ensure cookies work correctly.
219+
```swift
220+
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
221+
guard let url = URLContexts.first?.url,
222+
url.absoluteString.contains("appwrite-callback") else {
223+
return
224+
}
225+
WebAuthComponent.handleIncomingCookie(from: url)
226+
}
227+
```
228+
229+
To authenticate a user in your Android application, use the [Create OAuth2 Session](https://appwrite.io/docs/client/account?sdk=apple-default#accountCreateOAuth2Session) endpoint.
230+
231+
```swift
232+
import Appwrite
233+
234+
let client = Client()
235+
.setEndpoint("https://cloud.appwrite.io/v1") // Your API Endpoint
236+
.setProject("[PROJECT_ID]") // Your project ID
237+
238+
let account = Account(client)
239+
240+
let success = try await account.createOAuth2Session(
241+
provider: "github"
242+
)
243+
244+
```
245+
75246
## Refreshing the OAuth2 session
76247
OAuth2 sessions expire to protect from security risks. This means, OAuth2 sessions should be refreshed to keep the user authenticated. You can do this by calling the [Update OAuth Session](https://appwrite.io/docs/client/account#accountUpdateSession) endpoint when ever your user visits your app.
77248

@@ -124,7 +295,55 @@ void main() async {
124295
```
125296

126297
### Android (Kotlin)
298+
```kotlin
299+
import io.appwrite.Client
300+
import io.appwrite.services.Account
127301

128-
### Android (Java)
302+
val client = Client(context)
303+
.setEndpoint("https://cloud.appwrite.io/v1") // Your API Endpoint
304+
.setProject("[PROJECT_ID]") // Your project ID
305+
306+
val account = Account(client)
129307

308+
val response = account.updateSession(
309+
sessionId = "current"
310+
)
311+
```
312+
### Android (Java)
313+
```java
314+
import io.appwrite.Client;
315+
import io.appwrite.coroutines.CoroutineCallback;
316+
import io.appwrite.services.Account;
317+
318+
Client client = new Client(context)
319+
.setEndpoint("https://cloud.appwrite.io/v1") // Your API Endpoint
320+
.setProject("[PROJECT_ID]"); // Your project ID
321+
322+
Account account = new Account(client);
323+
324+
account.updateSession(
325+
"current"
326+
new CoroutineCallback<>((result, error) -> {
327+
if (error != null) {
328+
error.printStackTrace();
329+
return;
330+
}
331+
332+
Log.d("Appwrite", result.toString());
333+
})
334+
);
335+
```
130336
### iOS (Swift)
337+
``` swift
338+
import Appwrite
339+
340+
let client = Client()
341+
.setEndpoint("https://cloud.appwrite.io/v1") // Your API Endpoint
342+
.setProject("[PROJECT_ID]") // Your project ID
343+
344+
let account = Account(client)
345+
346+
let session = try await account.updateSession(
347+
sessionId: "current"
348+
)
349+
```

0 commit comments

Comments
 (0)