|
8 | 8 | import Foundation
|
9 | 9 | import Helpers
|
10 | 10 |
|
11 |
| -/// A locally stored ``Session``, it contains metadata such as `expirationDate`. |
12 |
| -struct StoredSession: Codable { |
13 |
| - var session: Session |
14 |
| - var expirationDate: Date |
15 |
| - |
16 |
| - init(session: Session, expirationDate _: Date? = nil) { |
17 |
| - self.session = session |
18 |
| - expirationDate = Date(timeIntervalSince1970: session.expiresAt) |
19 |
| - } |
20 |
| -} |
21 |
| - |
22 | 11 | struct SessionStorage {
|
23 | 12 | var get: @Sendable () throws -> Session?
|
24 | 13 | var store: @Sendable (_ session: Session) throws -> Void
|
25 | 14 | var delete: @Sendable () throws -> Void
|
26 | 15 | }
|
27 | 16 |
|
28 | 17 | extension SessionStorage {
|
| 18 | + /// Key used to store session on ``AuthLocalStorage``. |
| 19 | + /// |
| 20 | + /// It uses value from ``AuthClient/Configuration/storageKey`` or default to `supabase.auth.token` if not provided. |
| 21 | + static func key(_ clientID: AuthClientID) -> String { |
| 22 | + Dependencies[clientID].configuration.storageKey ?? STORAGE_KEY |
| 23 | + } |
| 24 | + |
29 | 25 | static func live(clientID: AuthClientID) -> SessionStorage {
|
30 |
| - var key: String { |
31 |
| - Dependencies[clientID].configuration.storageKey ?? STORAGE_KEY |
| 26 | + var storage: any AuthLocalStorage { |
| 27 | + Dependencies[clientID].configuration.localStorage |
32 | 28 | }
|
33 | 29 |
|
34 |
| - var oldKey: String { "supabase.session" } |
| 30 | + var logger: (any SupabaseLogger)? { |
| 31 | + Dependencies[clientID].configuration.logger |
| 32 | + } |
35 | 33 |
|
36 |
| - var storage: any AuthLocalStorage { |
37 |
| - Dependencies[clientID].configuration.localStorage |
| 34 | + let migrations: [StorageMigration] = [ |
| 35 | + .sessionNewKey(clientID: clientID), |
| 36 | + .storeSessionDirectly(clientID: clientID), |
| 37 | + ] |
| 38 | + |
| 39 | + var key: String { |
| 40 | + SessionStorage.key(clientID) |
38 | 41 | }
|
39 | 42 |
|
40 | 43 | return SessionStorage(
|
41 | 44 | get: {
|
42 |
| - var storedData = try? storage.retrieve(key: oldKey) |
43 |
| - |
44 |
| - if let storedData { |
45 |
| - // migrate to new key. |
46 |
| - try storage.store(key: key, value: storedData) |
47 |
| - try? storage.remove(key: oldKey) |
48 |
| - } else { |
49 |
| - storedData = try storage.retrieve(key: key) |
| 45 | + for migration in migrations { |
| 46 | + do { |
| 47 | + try migration.run() |
| 48 | + } catch { |
| 49 | + logger?.error("Storage migration failed: \(error.localizedDescription)") |
| 50 | + } |
50 | 51 | }
|
51 | 52 |
|
| 53 | + let storedData = try storage.retrieve(key: key) |
52 | 54 | return try storedData.flatMap {
|
53 |
| - try AuthClient.Configuration.jsonDecoder.decode(StoredSession.self, from: $0).session |
| 55 | + try AuthClient.Configuration.jsonDecoder.decode(Session.self, from: $0) |
54 | 56 | }
|
55 | 57 | },
|
56 | 58 | store: { session in
|
57 | 59 | try storage.store(
|
58 | 60 | key: key,
|
59 |
| - value: AuthClient.Configuration.jsonEncoder.encode(StoredSession(session: session)) |
| 61 | + value: AuthClient.Configuration.jsonEncoder.encode(session) |
60 | 62 | )
|
61 | 63 | },
|
62 | 64 | delete: {
|
63 | 65 | try storage.remove(key: key)
|
64 |
| - try? storage.remove(key: oldKey) |
65 | 66 | }
|
66 | 67 | )
|
67 | 68 | }
|
68 | 69 | }
|
| 70 | + |
| 71 | +struct StorageMigration { |
| 72 | + var run: @Sendable () throws -> Void |
| 73 | +} |
| 74 | + |
| 75 | +extension StorageMigration { |
| 76 | + /// Migrate stored session from `supabase.session` key to the custom provided storage key |
| 77 | + /// or the default `supabase.auth.token` key. |
| 78 | + static func sessionNewKey(clientID: AuthClientID) -> StorageMigration { |
| 79 | + StorageMigration { |
| 80 | + let storage = Dependencies[clientID].configuration.localStorage |
| 81 | + let newKey = SessionStorage.key(clientID) |
| 82 | + |
| 83 | + if let storedData = try? storage.retrieve(key: "supabase.session") { |
| 84 | + // migrate to new key. |
| 85 | + try storage.store(key: newKey, value: storedData) |
| 86 | + try? storage.remove(key: "supabase.session") |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + /// Migrate the stored session. |
| 92 | + /// |
| 93 | + /// Migrate the stored session which used to be stored as: |
| 94 | + /// ```json |
| 95 | + /// { |
| 96 | + /// "session": <Session>, |
| 97 | + /// "expiration_date": <Date> |
| 98 | + /// } |
| 99 | + /// ``` |
| 100 | + /// To directly store the `Session` object. |
| 101 | + static func storeSessionDirectly(clientID: AuthClientID) -> StorageMigration { |
| 102 | + struct StoredSession: Codable { |
| 103 | + var session: Session |
| 104 | + var expirationDate: Date |
| 105 | + } |
| 106 | + |
| 107 | + return StorageMigration { |
| 108 | + let storage = Dependencies[clientID].configuration.localStorage |
| 109 | + let key = SessionStorage.key(clientID) |
| 110 | + |
| 111 | + if let data = try? storage.retrieve(key: key), |
| 112 | + let storedSession = try? AuthClient.Configuration.jsonDecoder.decode(StoredSession.self, from: data) |
| 113 | + { |
| 114 | + let session = try AuthClient.Configuration.jsonEncoder.encode(storedSession.session) |
| 115 | + try storage.store(key: key, value: session) |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments