@@ -33,6 +33,146 @@ For all requests made for supabase, you will need to initialize a `SupabaseClien
33
33
let client = SupabaseClient (supabaseURL : " { Supabase URL }" , supabaseKey : " { Supabase anonymous Key }" )
34
34
```
35
35
36
+ ## Login Implementation
37
+
38
+ Import and Initialize GoTrueSwift which is bundled with Supabase Swift
39
+ ``` swift
40
+ import GoTrue
41
+
42
+ // Intialize Gotrue
43
+ var client: GoTrueClient = GoTrueClient (url : " { Supabase URL }" , headers : [" apikey" : { Supabase anonymous Key }])
44
+ ```
45
+
46
+ Here's how to Sign Up with Email and get the signed in users Session Info.
47
+
48
+ ``` swift
49
+ Task {
50
+ do {
51
+ try await client.signUp (email : email, password : password)
52
+ let session = try await client.session
53
+ print (" ### Session Info: \( session ) " )
54
+ } catch {
55
+ print (" ### Sign Up Error: \( error ) " )
56
+ }
57
+ }
58
+ ```
59
+
60
+ Here's how to Login with Email for an existing users and get the logged in users Session Info.
61
+
62
+ ``` swift
63
+ Task {
64
+ do {
65
+ try await client.signIn (email : email, password : password)
66
+ let session = try await client.session
67
+ print (" ### Session Info: \( session ) " )
68
+ } catch {
69
+ print (" ### Sign Up Error: \( error ) " )
70
+ }
71
+ }
72
+ ```
73
+
74
+ ## Social Login Implementation
75
+
76
+ ### Setup Callback URL
77
+
78
+ We need to first setup the callback URL for all Social Logins inside the app.
79
+
80
+ - Setup the callback URL on Info.plist
81
+ ``` xml
82
+ <array >
83
+ <dict >
84
+ <key >CFBundleTypeRole</key >
85
+ <string >Editor</string >
86
+ <key >CFBundleURLSchemes</key >
87
+ <array >
88
+ <string >app.yourapp://login-callback</string >
89
+ </array >
90
+ </dict >
91
+ </array >
92
+ ```
93
+ - Add this callback URL on Supabase under Authentication -> URL Configuration -> Redirect URLs
94
+
95
+ ### Google Sign In
96
+
97
+ - Setup Google Auth as per [ Supabase's Documentation] ( https://supabase.com/docs/guides/auth/social-login/auth-google )
98
+ - Note: For iOS we still need to use Google Consent Form for Web
99
+ - Import SafariServices to your ViewController and create a SafariVC instance
100
+
101
+ ``` swift
102
+ import SafariServices
103
+
104
+ var safariVC: SFSafariViewController?
105
+ ```
106
+ - Get the URL for Google Sign in from Supabase and load it on SFSafariViewController
107
+ - Add the previous callback URL you set up in the redirecTo
108
+
109
+ ``` swift
110
+ Task {
111
+ do {
112
+ let url = try await client.getOAuthSignInURL (provider : Provider.google , redirectTo : URL (string : {Your Callback URL})! )
113
+ safariVC = SFSafariViewController (url : url as URL)
114
+ self .present (safariVC! , animated : true , completion : nil )
115
+ } catch {
116
+ print (" ### Google Sign in Error: \( error ) " )
117
+ }
118
+ }
119
+ ```
120
+ - Handle the callback URL on SceneDelegate. (For older projects you can use AppDelegate if SceneDelegate is not there in the project)
121
+ - 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
122
+ ``` swift
123
+ func scene (_ scene : UIScene, openURLContexts URLContexts : Set <UIOpenURLContext>) {
124
+ if let url = URLContexts.first ? .url as? URL {
125
+ if url.host == " login-callback" {
126
+ let urlDict: [String : URL] = [" url" : url]
127
+ NotificationCenter.default .post (name : Notification.Name (" OAuthCallBack" ), object : nil , userInfo : urlDict)
128
+ }
129
+ }
130
+ }
131
+ ```
132
+ - In your View Controller observe for the Notification and handle minimizing the SFSafariViewController and get the session
133
+ ``` swift
134
+ NotificationCenter.default .addObserver (
135
+ self ,
136
+ selector : #selector (self .oAuthCallback (_:)),
137
+ name : NSNotification.Name (rawValue : " OAuthCallBack" ),
138
+ object : nil )
139
+
140
+ @objc func oAuthCallback (_ notification : NSNotification){
141
+ guard let url = notification.userInfo? [" url" ] as? URL else { return }
142
+ Task {
143
+ do {
144
+ let session = try await SupaBaseAuth ().client .session (from : url)
145
+ print (" ### Session Info: \( session ) " )
146
+ } catch {
147
+ print (" ### oAuthCallback error: \( error ) " )
148
+ }
149
+ }
150
+ safariVC? .dismiss (animated : true )
151
+ }
152
+ ```
153
+
154
+ ### Apple Sign In
155
+
156
+ - Setup Apple Auth as per [ Supabase's Documentation] ( https://supabase.com/docs/guides/auth/social-login/auth-apple )
157
+ - For Sign in with Apple follow the above as per Google Sign In and just replace the provider
158
+ - Once the user moves to the SFSafariViewController the Apple Native Popup will slide up to continue with Sign In.
159
+
160
+ ``` swift
161
+ Task {
162
+ do {
163
+ let url = try await client.getOAuthSignInURL (provider : ** Provider.apple ** , redirectTo : URL (string : {Your Callback URL})! )
164
+ safariVC = SFSafariViewController (url : url as URL)
165
+ self .present (safariVC! , animated : true , completion : nil )
166
+ } catch {
167
+ print (" ### Apple Sign in Error: \( error ) " )
168
+ }
169
+ }
170
+ ```
171
+
172
+ ### Other Social Logins
173
+
174
+ - 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
175
+
36
176
## Contributing
37
177
38
178
- Fork the repo on GitHub
0 commit comments