mirror of
https://github.com/supabase/supabase.git
synced 2026-12-05 16:09:06 +00:00
## Problem A full docs E2E run with the anchor check from #52174 found 58 broken anchor links on 53 guide pages. Most point to renamed or removed headings. Fixing them sets a baseline of zero for the IA restructure. Closes DOCS-1516 ## Solution - Retarget each link to the section that covers it today, verified on production. - Add a custom id where links already use a hand-written slug, such as `#phx_join`. - Drop the anchor where no section fits. ## Preview links Rows with an anchor link straight to a new custom id, which is broken on live and works on the preview. For the rest, search the page for the phrase to find the changed link. | Live | Preview | Search for | Change | | --- | --- | --- | --- | | [with-react](https://supabase.com/docs/guides/getting-started/tutorials/with-react) | [with-react](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/getting-started/tutorials/with-react) | Row Level Security | `auth#row-level-security` → `database/postgres/row-level-security` (20 links, 15 files) | | [nextjs](https://supabase.com/docs/guides/auth/quickstarts/nextjs) | [nextjs](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/auth/quickstarts/nextjs) | Supabase Auth docs | `auth#authentication` → `auth` | | [astrojs](https://supabase.com/docs/guides/auth/quickstarts/astrojs) | [astrojs](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/auth/quickstarts/astrojs) | Supabase Auth docs | `auth#authentication` → `auth` | | [custom-domains](https://supabase.com/docs/guides/platform/custom-domains) | [custom-domains](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/platform/custom-domains) | Owner or Admin permissions | `access-control#manage-team-members` → `#manage-organization-members` | | [network-restrictions](https://supabase.com/docs/guides/platform/network-restrictions) | [network-restrictions](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/platform/network-restrictions) | Owner or Admin permissions | `access-control#manage-team-members` → `#manage-organization-members` | | [postgres-connection-logging](https://supabase.com/docs/guides/platform/postgres-connection-logging) | [postgres-connection-logging](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/platform/postgres-connection-logging) | Owner or Admin permissions | `access-control#manage-team-members` → `#manage-organization-members` | | [ssl-enforcement](https://supabase.com/docs/guides/platform/ssl-enforcement) | [ssl-enforcement](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/platform/ssl-enforcement) | Owner or Admin permissions | `access-control#manage-team-members` → `#manage-organization-members` | | [with-expo-react-native-social-sign-in](https://supabase.com/docs/guides/auth/quickstarts/with-expo-react-native-social-sign-in) | [with-expo-react-native-social-sign-in](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/auth/quickstarts/with-expo-react-native-social-sign-in) | copied earlier | `#get-the-api-keys` → `#get-api-details` | | [with-expo-react-native-social-sign-in](https://supabase.com/docs/guides/auth/quickstarts/with-expo-react-native-social-sign-in) | [with-expo-react-native-social-sign-in](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/auth/quickstarts/with-expo-react-native-social-sign-in) | Apple Developer Console | `auth-apple#configuration-web` → `#configuration-web-oauth` | | [with-expo-react-native-social-sign-in](https://supabase.com/docs/guides/auth/quickstarts/with-expo-react-native-social-sign-in) | [with-expo-react-native-social-sign-in](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/auth/quickstarts/with-expo-react-native-social-sign-in) | Google Sign In | Dropped `#react-native`, a non-default tab | | [with-flutter](https://supabase.com/docs/guides/getting-started/tutorials/with-flutter) | [with-flutter](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/getting-started/tutorials/with-flutter) | copied earlier | `#get-the-api-keys` → `#get-api-details` | | [going-to-prod](https://supabase.com/docs/guides/ai/going-to-prod#hnsw-understanding-efconstruction--efsearch--and-m) | [going-to-prod](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/ai/going-to-prod#hnsw-understanding-efconstruction--efsearch--and-m) | HNSW, understanding | Custom id matching the links from `ai/choosing-compute-addon` | | [rag-with-permissions](https://supabase.com/docs/guides/ai/rag-with-permissions) | [rag-with-permissions](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/ai/rag-with-permissions) | query plan analyzer | `platform/performance#optimizing-poor-performing-queries` → `database/query-optimization#analyze-the-query-plan` | | [ivf-indexes](https://supabase.com/docs/guides/ai/vector-indexes/ivf-indexes) | [ivf-indexes](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/ai/vector-indexes/ivf-indexes) | Distance operators | `#distance-operators` → `ai/vector-indexes#distance-operators` | | [custom-claims-and-role-based-access-control-rbac](https://supabase.com/docs/guides/api/custom-claims-and-role-based-access-control-rbac) | [custom-claims-and-role-based-access-control-rbac](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/api/custom-claims-and-role-based-access-control-rbac) | local development | `auth-hooks#local-development` → `#developing` | | [native-mobile-deep-linking](https://supabase.com/docs/guides/auth/native-mobile-deep-linking) | [native-mobile-deep-linking](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/auth/native-mobile-deep-linking) | password reset | `passwords#resetting-a-users-password-forgot-password` → `#resetting-a-password` | | [mcp-authentication](https://supabase.com/docs/guides/auth/oauth-server/mcp-authentication) | [mcp-authentication](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/auth/oauth-server/mcp-authentication) | authorization UI | `getting-started#build-your-authorization-endpoint` → `#build-your-authorization-ui` | | [passwords](https://supabase.com/docs/guides/auth/passwords) | [passwords](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/auth/passwords) | implicit flow | `sessions#implicit-flow`, `sessions#pkce-flow` → `sessions/implicit-flow`, `sessions/pkce-flow` | | [redirect-urls](https://supabase.com/docs/guides/auth/redirect-urls) | [redirect-urls](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/auth/redirect-urls) | third-party providers | Dropped the `auth-signinwithoauth` example anchor | | [users](https://supabase.com/docs/guides/auth/users) | [users](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/auth/users) | User Identity | `auth-identity-linking#the-user-identity` → `identities#the-user-identity-object` | | [upgrading](https://supabase.com/docs/guides/platform/upgrading#upgrading-to-pg_graphql-160) | [upgrading](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/platform/upgrading#upgrading-to-pg_graphql-160) | Upgrading to pg_graphql | Custom id matching the link from `database/extensions/pg_graphql` | | [import-data](https://supabase.com/docs/guides/database/import-data) | [import-data](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/database/import-data) | disk management | `database-size#disk-management` → `#disk-size` | | [multigres](https://supabase.com/docs/guides/database/multigres) | [multigres](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/database/multigres) | Private Alpha | Dropped the link. `getting-started/features` redirects to `/features` | | [data-deletion](https://supabase.com/docs/guides/database/postgres/data-deletion) | [data-deletion](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/database/postgres/data-deletion) | Check for blocked queries | `#monitoring-locks` → `#check-for-blocked-queries` | | [postgres-log-config](https://supabase.com/docs/guides/database/postgres/postgres-log-config) | [postgres-log-config](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/database/postgres/postgres-log-config) | Supavisor | `connecting-to-postgres#poolers` → `pooling-and-limits#shared-pooler` and `#dedicated-pooler` | | [partner-catalog](https://supabase.com/docs/guides/integrations/partner-catalog) | [partner-catalog](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/integrations/partner-catalog) | Project GraphQL API | `api#graphql-api-overview` → `graphql` | | [vercel-marketplace](https://supabase.com/docs/guides/integrations/vercel-marketplace) | [vercel-marketplace](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/integrations/vercel-marketplace) | How your Vercel team maps | `#permissions` → `#how-your-vercel-team-maps-to-supabase` | | [managing-config](https://supabase.com/docs/guides/local-development/managing-config) | [managing-config](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/local-development/managing-config) | Managing secrets for branches | `branching#managing-secrets-for-branches` → `branching/configuration#managing-secrets-for-branches` | | [reports](https://supabase.com/docs/guides/observability/reports) | [reports](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/observability/reports) | Query Performance Advisory | `performance#examine-query-performance` → `#examining-query-performance` | | [backups](https://supabase.com/docs/guides/platform/backups) | [backups](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/platform/backups) | legacy logical backup | `backup-restore#backup-database-using-the-cli` → `#back-up-database-using-the-cli` | | [backups](https://supabase.com/docs/guides/platform/backups#pitr-restoration-process) | [backups](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/platform/backups#pitr-restoration-process) | Restoration process | Custom id matching the link from `read-replicas/getting-started` | | [branching](https://supabase.com/docs/guides/platform/manage-your-usage/branching) | [branching](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/platform/manage-your-usage/branching) | persistent branches | `branching#persistent-branches` → `#how-branching-works` | | [point-in-time-recovery](https://supabase.com/docs/guides/platform/manage-your-usage/point-in-time-recovery) | [point-in-time-recovery](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/platform/manage-your-usage/point-in-time-recovery) | backup frequency | `backups#frequency-of-backups` → `backups` | | [read-replicas](https://supabase.com/docs/guides/platform/read-replicas) | [read-replicas](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/platform/read-replicas) | IPv4 add-on | `ipv4-address#read-replicas` → `#read-replicas-and-ipv4-add-on` | | [protocol](https://supabase.com/docs/guides/realtime/protocol#access-token) | [protocol](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/realtime/protocol#access-token) | access_token | Custom id matching the link from `realtime/authorization` | | [postgres-changes](https://supabase.com/docs/guides/realtime/postgres-changes#negating-a-filter-not) | [postgres-changes](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/realtime/postgres-changes#negating-a-filter-not) | Negating a filter | Custom id matching the existing in-page link | | [protocol](https://supabase.com/docs/guides/realtime/protocol#phx_join) | [protocol](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/realtime/protocol#phx_join) | phx_join | Custom id matching the existing in-page link | | [reports](https://supabase.com/docs/guides/realtime/reports) | [reports](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/realtime/reports) | Broadcast via REST API | `broadcast#broadcast-via-rest-api` → `#broadcast-using-the-rest-api` | <details> <summary>The 15 files with Row Level Security links</summary> - `_partials/quickstart_intro.mdx`, used by the framework tutorials - `auth/phone-login` - `auth/quickstarts/with-expo-react-native-social-sign-in` - `database/postgres/column-level-security` - `getting-started/tutorials/`: `with-flutter`, `with-ionic-angular`, `with-ionic-react`, `with-ionic-vue`, `with-kotlin`, `with-nuxt-3`, `with-redwoodjs`, `with-solidjs`, `with-svelte`, `with-swift`, `with-vue-3` </details> ## Review instructions 1. Open a few live and preview links side by side, and click the changed link on each to confirm the preview lands on the right section. 2. From a checkout of `docs/e2e-anchor-links`, run the anchor check against the preview with the changed pages, as described in `e2e/docs/README.md`: `DOCS_E2E_PAGE_PATHS=<changed /docs/... paths> PLAYWRIGHT_BASE_URL=https://docs-git-docs-fix-broken-anchors-supabase.vercel.app pnpm exec playwright test --grep-invert @a11y` 3. Confirm the anchor check reports no failures. ## Checklist Check all before review: - [x] I have read [CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) - [x] If I wrote a new docs topic or edited an existing topic, I used the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs [style guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)
561 lines
13 KiB
Plaintext
561 lines
13 KiB
Plaintext
---
|
|
title: 'Build a User Management App with Swift and SwiftUI'
|
|
description: 'Learn how to use Supabase in your SwiftUI App.'
|
|
---
|
|
|
|
<$Partial path="quickstart_intro.mdx" />
|
|
|
|

|
|
|
|
<Admonition type="note">
|
|
|
|
If you get stuck while working through this guide, you can find the [full example on GitHub](https://github.com/supabase/supabase/tree/master/examples/user-management/swift-user-management).
|
|
|
|
</Admonition>
|
|
|
|
<$Partial path="project_setup.mdx" variables={{ "framework": "swift", "tab": "mobiles" }} />
|
|
|
|
## Building the app
|
|
|
|
Build the SwiftUI app from scratch.
|
|
|
|
### Create a SwiftUI app in Xcode
|
|
|
|
Open Xcode and create a new SwiftUI project.
|
|
|
|
Add the [supabase-swift](https://github.com/supabase/supabase-swift) dependency.
|
|
|
|
Add the `https://github.com/supabase/supabase-swift` package to your app. For instructions, see the [Apple tutorial on adding package dependencies](https://developer.apple.com/documentation/xcode/adding-package-dependencies-to-your-app).
|
|
|
|
Create a helper file to initialize the Supabase client.
|
|
You need the API URL and the key that you copied [earlier](#get-api-details).
|
|
These variables will be exposed on the application, and that's completely fine since you have
|
|
[Row Level Security](/docs/guides/database/postgres/row-level-security) enabled on your database.
|
|
|
|
<$CodeTabs>
|
|
|
|
```swift name=Supabase.swift
|
|
import Foundation
|
|
import Supabase
|
|
|
|
let supabase = SupabaseClient(
|
|
supabaseURL: URL(string: "YOUR_SUPABASE_URL")!,
|
|
supabaseKey: "YOUR_SUPABASE_PUBLISHABLE_KEY"
|
|
)
|
|
```
|
|
|
|
</$CodeTabs>
|
|
|
|
### Set up a sign-in view
|
|
|
|
Set up a SwiftUI view to manage sign-ins and sign-ups.
|
|
Users should be able to sign in using a magic link.
|
|
|
|
<$CodeTabs>
|
|
|
|
```swift name=AuthView.swift
|
|
import SwiftUI
|
|
import Supabase
|
|
|
|
struct AuthView: View {
|
|
@State var email = ""
|
|
@State var isLoading = false
|
|
@State var result: Result<Void, Error>?
|
|
|
|
var body: some View {
|
|
Form {
|
|
Section {
|
|
TextField("Email", text: $email)
|
|
.textContentType(.emailAddress)
|
|
.textInputAutocapitalization(.never)
|
|
.autocorrectionDisabled()
|
|
}
|
|
|
|
Section {
|
|
Button("Sign in") {
|
|
signInButtonTapped()
|
|
}
|
|
|
|
if isLoading {
|
|
ProgressView()
|
|
}
|
|
}
|
|
|
|
if let result {
|
|
Section {
|
|
switch result {
|
|
case .success:
|
|
Text("Check your inbox.")
|
|
case .failure(let error):
|
|
Text(error.localizedDescription).foregroundStyle(.red)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
.onOpenURL(perform: { url in
|
|
Task {
|
|
do {
|
|
try await supabase.auth.session(from: url)
|
|
} catch {
|
|
self.result = .failure(error)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
func signInButtonTapped() {
|
|
Task {
|
|
isLoading = true
|
|
defer { isLoading = false }
|
|
|
|
do {
|
|
try await supabase.auth.signInWithOTP(
|
|
email: email,
|
|
redirectTo: URL(string: "io.supabase.user-management://login-callback")
|
|
)
|
|
result = .success(())
|
|
} catch {
|
|
result = .failure(error)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
```
|
|
|
|
</$CodeTabs>
|
|
|
|
<Admonition type="note">
|
|
|
|
The example uses a custom `redirectTo` URL. For this to work, add a custom redirect URL to Supabase and a custom URL scheme to your SwiftUI application. Follow the guide on [implementing deep link handling](/docs/guides/auth/native-mobile-deep-linking?platform=swift).
|
|
|
|
</Admonition>
|
|
|
|
### Account view
|
|
|
|
After a user is signed in, you can allow them to edit their profile details and manage their account.
|
|
|
|
Create a new view for that called `ProfileView.swift`.
|
|
|
|
<$CodeTabs>
|
|
|
|
```swift name=ProfileView.swift
|
|
import SwiftUI
|
|
|
|
struct ProfileView: View {
|
|
@State var username = ""
|
|
@State var fullName = ""
|
|
@State var website = ""
|
|
|
|
@State var isLoading = false
|
|
|
|
var body: some View {
|
|
NavigationStack {
|
|
Form {
|
|
Section {
|
|
TextField("Username", text: $username)
|
|
.textContentType(.username)
|
|
.textInputAutocapitalization(.never)
|
|
TextField("Full name", text: $fullName)
|
|
.textContentType(.name)
|
|
TextField("Website", text: $website)
|
|
.textContentType(.URL)
|
|
.textInputAutocapitalization(.never)
|
|
}
|
|
|
|
Section {
|
|
Button("Update profile") {
|
|
updateProfileButtonTapped()
|
|
}
|
|
.bold()
|
|
|
|
if isLoading {
|
|
ProgressView()
|
|
}
|
|
}
|
|
}
|
|
.navigationTitle("Profile")
|
|
.toolbar(content: {
|
|
ToolbarItem(placement: .topBarLeading){
|
|
Button("Sign out", role: .destructive) {
|
|
Task {
|
|
try? await supabase.auth.signOut()
|
|
}
|
|
}
|
|
}
|
|
})
|
|
}
|
|
.task {
|
|
await getInitialProfile()
|
|
}
|
|
}
|
|
|
|
func getInitialProfile() async {
|
|
do {
|
|
let currentUser = try await supabase.auth.session.user
|
|
|
|
let profile: Profile =
|
|
try await supabase
|
|
.from("profiles")
|
|
.select()
|
|
.eq("id", value: currentUser.id)
|
|
.single()
|
|
.execute()
|
|
.value
|
|
|
|
self.username = profile.username ?? ""
|
|
self.fullName = profile.fullName ?? ""
|
|
self.website = profile.website ?? ""
|
|
|
|
} catch {
|
|
debugPrint(error)
|
|
}
|
|
}
|
|
|
|
func updateProfileButtonTapped() {
|
|
Task {
|
|
isLoading = true
|
|
defer { isLoading = false }
|
|
do {
|
|
let currentUser = try await supabase.auth.session.user
|
|
|
|
try await supabase
|
|
.from("profiles")
|
|
.update(
|
|
UpdateProfileParams(
|
|
username: username,
|
|
fullName: fullName,
|
|
website: website
|
|
)
|
|
)
|
|
.eq("id", value: currentUser.id)
|
|
.execute()
|
|
} catch {
|
|
debugPrint(error)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
```
|
|
|
|
</$CodeTabs>
|
|
|
|
### Models
|
|
|
|
In `ProfileView.swift`, you used 2 model types for deserializing the response and serializing the request to Supabase. Add those in a new `Models.swift` file.
|
|
|
|
<$CodeTabs>
|
|
|
|
```swift name=Models.swift
|
|
struct Profile: Decodable {
|
|
let username: String?
|
|
let fullName: String?
|
|
let website: String?
|
|
|
|
enum CodingKeys: String, CodingKey {
|
|
case username
|
|
case fullName = "full_name"
|
|
case website
|
|
}
|
|
}
|
|
|
|
struct UpdateProfileParams: Encodable {
|
|
let username: String
|
|
let fullName: String
|
|
let website: String
|
|
|
|
enum CodingKeys: String, CodingKey {
|
|
case username
|
|
case fullName = "full_name"
|
|
case website
|
|
}
|
|
}
|
|
```
|
|
|
|
</$CodeTabs>
|
|
|
|
## Profile photos
|
|
|
|
Next, add a way for users to upload a profile photo. Supabase configures every project with [Storage](/docs/guides/storage) for managing large files like photos and videos.
|
|
|
|
### Add `PhotosPicker`
|
|
|
|
Add support for the user to pick an image from the library and upload it.
|
|
Start by creating a new type to hold the picked avatar image:
|
|
|
|
<$CodeTabs>
|
|
|
|
```swift name=AvatarImage.swift
|
|
import SwiftUI
|
|
|
|
struct AvatarImage: Transferable, Equatable {
|
|
let image: Image
|
|
let data: Data
|
|
|
|
static var transferRepresentation: some TransferRepresentation {
|
|
DataRepresentation(importedContentType: .image) { data in
|
|
guard let image = AvatarImage(data: data) else {
|
|
throw TransferError.importFailed
|
|
}
|
|
|
|
return image
|
|
}
|
|
}
|
|
}
|
|
|
|
extension AvatarImage {
|
|
init?(data: Data) {
|
|
guard let uiImage = UIImage(data: data) else {
|
|
return nil
|
|
}
|
|
|
|
let image = Image(uiImage: uiImage)
|
|
self.init(image: image, data: data)
|
|
}
|
|
}
|
|
|
|
enum TransferError: Error {
|
|
case importFailed
|
|
}
|
|
```
|
|
|
|
</$CodeTabs>
|
|
|
|
#### Add `PhotosPicker` to profile page
|
|
|
|
<$CodeTabs>
|
|
|
|
```swift name=ProfileView.swift
|
|
import PhotosUI
|
|
import Storage
|
|
import Supabase
|
|
import SwiftUI
|
|
|
|
struct ProfileView: View {
|
|
@State var username = ""
|
|
@State var fullName = ""
|
|
@State var website = ""
|
|
|
|
@State var isLoading = false
|
|
|
|
@State var imageSelection: PhotosPickerItem?
|
|
@State var avatarImage: AvatarImage?
|
|
|
|
var body: some View {
|
|
NavigationStack {
|
|
Form {
|
|
Section {
|
|
HStack {
|
|
Group {
|
|
if let avatarImage {
|
|
avatarImage.image.resizable()
|
|
} else {
|
|
Color.clear
|
|
}
|
|
}
|
|
.scaledToFit()
|
|
.frame(width: 80, height: 80)
|
|
|
|
Spacer()
|
|
|
|
PhotosPicker(selection: $imageSelection, matching: .images) {
|
|
Image(systemName: "pencil.circle.fill")
|
|
.symbolRenderingMode(.multicolor)
|
|
.font(.system(size: 30))
|
|
.foregroundColor(.accentColor)
|
|
}
|
|
}
|
|
}
|
|
|
|
Section {
|
|
TextField("Username", text: $username)
|
|
.textContentType(.username)
|
|
.textInputAutocapitalization(.never)
|
|
TextField("Full name", text: $fullName)
|
|
.textContentType(.name)
|
|
TextField("Website", text: $website)
|
|
.textContentType(.URL)
|
|
.textInputAutocapitalization(.never)
|
|
}
|
|
|
|
Section {
|
|
Button("Update profile") {
|
|
updateProfileButtonTapped()
|
|
}
|
|
.bold()
|
|
|
|
if isLoading {
|
|
ProgressView()
|
|
}
|
|
}
|
|
}
|
|
.navigationTitle("Profile")
|
|
.toolbar(content: {
|
|
ToolbarItem {
|
|
Button("Sign out", role: .destructive) {
|
|
Task {
|
|
try? await supabase.auth.signOut()
|
|
}
|
|
}
|
|
}
|
|
})
|
|
.onChange(of: imageSelection) { _, newValue in
|
|
guard let newValue else { return }
|
|
loadTransferable(from: newValue)
|
|
}
|
|
}
|
|
.task {
|
|
await getInitialProfile()
|
|
}
|
|
}
|
|
|
|
func getInitialProfile() async {
|
|
do {
|
|
let currentUser = try await supabase.auth.session.user
|
|
|
|
let profile: Profile =
|
|
try await supabase
|
|
.from("profiles")
|
|
.select()
|
|
.eq("id", value: currentUser.id)
|
|
.single()
|
|
.execute()
|
|
.value
|
|
|
|
username = profile.username ?? ""
|
|
fullName = profile.fullName ?? ""
|
|
website = profile.website ?? ""
|
|
|
|
if let avatarURL = profile.avatarURL, !avatarURL.isEmpty {
|
|
try await downloadImage(path: avatarURL)
|
|
}
|
|
|
|
} catch {
|
|
debugPrint(error)
|
|
}
|
|
}
|
|
|
|
func updateProfileButtonTapped() {
|
|
Task {
|
|
isLoading = true
|
|
defer { isLoading = false }
|
|
do {
|
|
let imageURL = try await uploadImage()
|
|
|
|
let currentUser = try await supabase.auth.session.user
|
|
|
|
let updatedProfile = Profile(
|
|
username: username,
|
|
fullName: fullName,
|
|
website: website,
|
|
avatarURL: imageURL
|
|
)
|
|
|
|
try await supabase
|
|
.from("profiles")
|
|
.update(updatedProfile)
|
|
.eq("id", value: currentUser.id)
|
|
.execute()
|
|
} catch {
|
|
debugPrint(error)
|
|
}
|
|
}
|
|
}
|
|
|
|
private func loadTransferable(from imageSelection: PhotosPickerItem) {
|
|
Task {
|
|
do {
|
|
avatarImage = try await imageSelection.loadTransferable(type: AvatarImage.self)
|
|
} catch {
|
|
debugPrint(error)
|
|
}
|
|
}
|
|
}
|
|
|
|
private func downloadImage(path: String) async throws {
|
|
let data = try await supabase.storage.from("avatars").download(path: path)
|
|
avatarImage = AvatarImage(data: data)
|
|
}
|
|
|
|
private func uploadImage() async throws -> String? {
|
|
guard let data = avatarImage?.data else { return nil }
|
|
|
|
let filePath = "\(UUID().uuidString).jpeg"
|
|
|
|
try await supabase.storage
|
|
.from("avatars")
|
|
.upload(
|
|
filePath,
|
|
data: data,
|
|
options: FileOptions(contentType: "image/jpeg")
|
|
)
|
|
|
|
return filePath
|
|
}
|
|
}
|
|
```
|
|
|
|
</$CodeTabs>
|
|
|
|
Finally, update your Models.
|
|
|
|
<$CodeTabs>
|
|
|
|
```swift name=Models.swift
|
|
struct Profile: Codable {
|
|
let username: String?
|
|
let fullName: String?
|
|
let website: String?
|
|
let avatarURL: String?
|
|
|
|
enum CodingKeys: String, CodingKey {
|
|
case username
|
|
case fullName = "full_name"
|
|
case website
|
|
case avatarURL = "avatar_url"
|
|
}
|
|
}
|
|
```
|
|
|
|
</$CodeTabs>
|
|
|
|
You no longer need the `UpdateProfileParams` struct, as you can now reuse the `Profile` struct for both request and response calls.
|
|
|
|
### Launch!
|
|
|
|
With all the views in place, add an entry point for the application.
|
|
|
|
Add a new `AppView.swift` file.
|
|
|
|
<$CodeTabs>
|
|
|
|
```swift name=AppView.swift
|
|
import SwiftUI
|
|
|
|
struct AppView: View {
|
|
@State var isAuthenticated = false
|
|
|
|
var body: some View {
|
|
Group {
|
|
if isAuthenticated {
|
|
ProfileView()
|
|
} else {
|
|
AuthView()
|
|
}
|
|
}
|
|
.task {
|
|
for await state in supabase.auth.authStateChanges {
|
|
if [.initialSession, .signedIn, .signedOut].contains(state.event) {
|
|
isAuthenticated = state.session != nil
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
```
|
|
|
|
</$CodeTabs>
|
|
|
|
Update the entry point to the newly created `AppView`. Run in Xcode to launch your application in the simulator.
|
|
|
|
At this stage you have a fully functional application!
|