Skip to content

Improve README file #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 32 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# supabase-swift

Supabase client for swift. Mirrors the design of [supabase-js](https://github.com/supabase/supabase-js/blob/master/README.md)
Supabase client for swift. Mirrors the design of [supabase-js](https://github.com/supabase/supabase-js/blob/master/README.md).

## Installation

Expand All @@ -27,28 +27,22 @@ If you're using Xcode, [use this guide](https://developer.apple.com/documentatio

## Usage

For all requests made for supabase, you will need to initialize a `SupabaseClient` object.
To make requests to the `Supabase` database, you will need to initialize a `SupabaseClient` object:

```swift
let client = SupabaseClient(supabaseURL: "{ Supabase URL }", supabaseKey: "{ Supabase anonymous Key }")
```

## Login Implementation

Import and Initialize GoTrueSwift which is bundled with Supabase Swift
```swift
import GoTrue

//Intialize Gotrue
var client: GoTrueClient = GoTrueClient(url: "{ Supabase URL }", headers: ["apikey": { Supabase anonymous Key }])
```
Inside the `SupabaseClient` instance created before, you can find an `auth` property of type `GoTrueClient`. You can use it to perform sign in and sign up requests.

Here's how to Sign Up with Email and get the signed in users Session Info.
- Here's how to sign up with an email and password and get the signed in user `Session` info:

```swift
Task {
do {
try await client.signUp(email: email, password: password)
try await client.auth.signUp(email: email, password: password)
let session = try await client.session
print("### Session Info: \(session)")
} catch {
Expand All @@ -57,12 +51,12 @@ Task {
}
```

Here's how to Login with Email for an existing users and get the logged in users Session Info.
- For existing users, here's how to log in with an email and password and get the logged in user `Session` info:

```swift
Task {
do {
try await client.signIn(email: email, password: password)
try await client.auth.signIn(email: email, password: password)
let session = try await client.session
print("### Session Info: \(session)")
} catch {
Expand All @@ -75,9 +69,10 @@ Task {

### Setup Callback URL

We need to first setup the callback URL for all Social Logins inside the app.
We need to first set up the callback URL for all Social Logins inside the app.

- Setup the callback `URL` on `Info.plist`:

- Setup the callback URL on Info.plist
```xml
<array>
<dict>
Expand All @@ -90,21 +85,23 @@ We need to first setup the callback URL for all Social Logins inside the app.
</dict>
</array>
```
- Add this callback URL on Supabase under Authentication -> URL Configuration -> Redirect URLs

- Add this callback `URL` on `Supabase` under `Authentication -> URL Configuration -> Redirect URLs`.

### Google Sign In

- Setup Google Auth as per [Supabase's Documentation](https://supabase.com/docs/guides/auth/social-login/auth-google)
- Note: For iOS we still need to use Google Consent Form for Web
- Import SafariServices to your ViewController and create a SafariVC instance
- Setup Google Auth as per [Supabase's Documentation](https://supabase.com/docs/guides/auth/social-login/auth-google).
- Note: For iOS we still need to use Google Consent Form for Web.
- Import `SafariServices` in your `ViewController` and create a `SFSafariViewController` instance:

```swift
import SafariServices

var safariVC: SFSafariViewController?
```
- Get the URL for Google Sign in from Supabase and load it on SFSafariViewController
- Add the previous callback URL you set up in the redirecTo

- Get the `URL` for Google Sign in from `Supabase` and load it on `SFSafariViewController`.
- Pass the previous callback `URL` you set up in the `redirecTo` parameter:

```swift
Task {
Expand All @@ -117,8 +114,10 @@ Task {
}
}
```
- Handle the callback URL on SceneDelegate. (For older projects you can use AppDelegate if SceneDelegate is not there in the project)
- Post NotificationCenter call to let the View Controller know that callback has been received and pass the URL received. This URL will be used to get the session for the user

- Handle the callback `URL` on `SceneDelegate` (for older projects, you can use `AppDelegate` if `SceneDelegate` is not present).
- Post a `NotificationCenter` call to let the `ViewController` know the callback has been fired and pass the `URL` received. This `URL` will be used to get the user session.

```swift
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
if let url = URLContexts.first?.url as? URL {
Expand All @@ -129,7 +128,9 @@ func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>)
}
}
```
- In your View Controller observe for the Notification and handle minimizing the SFSafariViewController and get the session

- In your `ViewController`, observe for the `Notification` and handle it minimizing the `SFSafariViewController` and getting the session:

```swift
NotificationCenter.default.addObserver(
self,
Expand All @@ -153,9 +154,9 @@ NotificationCenter.default.addObserver(

### Apple Sign In

- Setup Apple Auth as per [Supabase's Documentation](https://supabase.com/docs/guides/auth/social-login/auth-apple)
- For Sign in with Apple follow the above as per Google Sign In and just replace the provider
- Once the user moves to the SFSafariViewController the Apple Native Popup will slide up to continue with Sign In.
- Setup Apple Auth as per [Supabase's Documentation](https://supabase.com/docs/guides/auth/social-login/auth-apple).
- For Sign in with Apple follow the above as per Google Sign In and just replace the provider.
- Once the user moves to the `SFSafariViewController`, an Apple native pop-up will slide up to continue with the sign in.

```swift
Task {
Expand All @@ -171,19 +172,15 @@ Task {

### Other Social Logins

- Other Social Logins if using a webview will be similar to above and just follow the [Supabase's Documentation](https://supabase.com/docs/guides/auth/) for their setup
- If using a WebViews, other social logins will be similar to above. Just follow the [Supabase's Documentation](https://supabase.com/docs/guides/auth/) for their setup.

## Basic CRUD Implementation

Import and Initialize the Supabase client

```swift
let client = SupabaseClient(supabaseURL: "{ Supabase URL }", supabaseKey: "{ Supabase anonymous Key }")
```
First, import and initialize `SupabaseClient`, as explained in "Usage" section.

### Insert Data

Create a model which follows the data structure of your table.
- Create a model which follows your table's data structure:

```swift
struct InsertModel: Encodable {
Expand Down Expand Up @@ -212,7 +209,7 @@ Task {

### Select Data

Using the same model as before
- Using the same model as before:

```swift
let insertData = InsertModel(title: "Test", desc: "Test Desc")
Expand Down