Skip to content

Commit 84aaa1d

Browse files
authored
fix(gotrue): AuthResponse return non-optional user (#174)
1 parent 3adc23c commit 84aaa1d

File tree

4 files changed

+28
-30
lines changed

4 files changed

+28
-30
lines changed

Sources/GoTrue/Extensions.swift

Lines changed: 0 additions & 11 deletions
This file was deleted.

Sources/GoTrue/Types.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,18 @@ public enum AuthResponse: Codable, Hashable, Sendable {
341341
case let .user(value): try container.encode(value)
342342
}
343343
}
344+
345+
public var user: User {
346+
switch self {
347+
case let .session(session): return session.user
348+
case let .user(user): return user
349+
}
350+
}
351+
352+
public var session: Session? {
353+
if case let .session(session) = self { return session }
354+
return nil
355+
}
344356
}
345357

346358
public struct UserAttributes: Codable, Hashable, Sendable {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import GoTrue
2+
import SnapshotTesting
3+
import XCTest
4+
5+
final class AuthResponseTests: XCTestCase {
6+
func testSession() throws {
7+
let response = try JSONDecoder.goTrue.decode(AuthResponse.self, from: json(named: "session"))
8+
XCTAssertNotNil(response.session)
9+
XCTAssertEqual(response.user, response.session?.user)
10+
}
11+
12+
func testUser() throws {
13+
let response = try JSONDecoder.goTrue.decode(AuthResponse.self, from: json(named: "user"))
14+
XCTAssertNil(response.session)
15+
}
16+
}

Tests/GoTrueTests/DecoderTests.swift

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)