main
md 134 lines 3.54 KB
Rendered Raw
1 # Quick Start — macOS
2
3 This guide shows hosted smbCloud Auth in a macOS app using `SmbCloudWebAuth` and `SmbCloudCredentialsManager`.
4
5 ## 1. Add the package
6
7 Add `https://github.com/smbcloudXYZ/smbcloud-auth-swift` to your macOS app.
8
9 ## 2. Configure a callback URL
10
11 Use a callback URL such as:
12
13 - `mymacapp://auth/callback`
14
15 Register that URL scheme for your app so the hosted auth callback returns to the application.
16
17 ## 3. Build a small store
18
19 ```swift
20 import AppKit
21 import SmbCloudAuth
22 import SwiftUI
23
24 @MainActor
25 final class AuthenticationStore: ObservableObject {
26 @Published private(set) var session: SmbCloudSession?
27 @Published private(set) var email: String?
28 @Published private(set) var errorMessage: String?
29
30 private let webAuth = SmbCloudWebAuth(
31 environment: .production,
32 oidcClientId: "YOUR_OIDC_CLIENT_ID",
33 redirectURL: URL(string: "mymacapp://auth/callback")!
34 )
35 private let credentialsManager = SmbCloudCredentialsManager(
36 service: "com.example.mymacapp.smbcloud-auth"
37 )
38
39 func restoreSession() {
40 do {
41 session = try credentialsManager.currentValidSession()
42 } catch {
43 errorMessage = error.localizedDescription
44 }
45 }
46
47 func signIn() async {
48 do {
49 let session = try await webAuth.login(
50 presentationAnchorProvider: {
51 NSApplication.shared.keyWindow ?? NSWindow()
52 },
53 credentialsManager: credentialsManager
54 )
55 self.session = session
56
57 let user = try await webAuth.userInfo(session: session)
58 email = user.email
59 } catch {
60 errorMessage = error.localizedDescription
61 }
62 }
63
64 func signOut() {
65 do {
66 try webAuth.clearSession(credentialsManager: credentialsManager)
67 session = nil
68 email = nil
69 } catch {
70 errorMessage = error.localizedDescription
71 }
72 }
73 }
74 ```
75
76 ## 4. Use it from SwiftUI
77
78 ```swift
79 import SwiftUI
80
81 struct ContentView: View {
82 @StateObject private var authStore = AuthenticationStore()
83
84 var body: some View {
85 VStack(spacing: 12) {
86 if let email = authStore.email {
87 Text("Signed in as \(email)")
88 } else if authStore.session != nil {
89 Text("Signed in")
90 } else {
91 Text("Signed out")
92 }
93
94 HStack {
95 Button("Sign In") {
96 Task {
97 await authStore.signIn()
98 }
99 }
100
101 Button("Sign Out") {
102 authStore.signOut()
103 }
104 .disabled(authStore.session == nil)
105 }
106
107 if let errorMessage = authStore.errorMessage {
108 Text(errorMessage)
109 .foregroundStyle(.red)
110 .multilineTextAlignment(.center)
111 }
112 }
113 .padding()
114 .frame(width: 360)
115 .task {
116 authStore.restoreSession()
117 }
118 }
119 }
120 ```
121
122 ## Notes
123
124 - Supply a real public client ID from smbCloud Auth.
125 - Use an explicit Keychain `service` value for your app.
126 - Do not ship an `app_secret` in the macOS app.
127 - Do not show or log raw access tokens in production UI.
128 - `clearSession()` and `logout()` currently clear local stored credentials only.
129 - If you need native email/password forms, keep confidential auth credentials on your backend.
130
131 ## Related docs
132
133 - `Docs/QuickStart-iOS.md`
134 - `Docs/Migration-From-Proxy-Native-Forms.md`