Skip to content

Commit a73609b

Browse files
Add memberwise initalizers to user and session (#599)
* Add memberwise initalizers to user and session * Fix linting errors
1 parent 44ff37a commit a73609b

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

Sources/StytchCore/SharedModels/AuthenticationFactor.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ public struct AuthenticationFactor: Sendable {
1717
public let kind: String
1818
/// The date this factor was last used to authenticate.
1919
public let lastAuthenticatedAt: Date
20+
21+
public init(rawData: JSON, kind: String, lastAuthenticatedAt: Date) {
22+
self.rawData = rawData
23+
self.kind = kind
24+
self.lastAuthenticatedAt = lastAuthenticatedAt
25+
}
2026
}
2127

2228
extension AuthenticationFactor: Equatable {

Sources/StytchCore/StytchClient/Models/Session.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,24 @@ public struct Session: Sendable {
3030
public let startedAt: Date
3131
/// The user id associated with this session.
3232
public let userId: User.ID
33+
34+
public init(
35+
attributes: Attributes,
36+
authenticationFactors: [AuthenticationFactor],
37+
expiresAt: Date,
38+
lastAccessedAt: Date,
39+
sessionId: Self.ID,
40+
startedAt: Date,
41+
userId: User.ID
42+
) {
43+
self.attributes = attributes
44+
self.authenticationFactors = authenticationFactors
45+
self.expiresAt = expiresAt
46+
self.lastAccessedAt = lastAccessedAt
47+
self.sessionId = sessionId
48+
self.startedAt = startedAt
49+
self.userId = userId
50+
}
3351
}
3452

3553
extension Session: Equatable {
@@ -78,6 +96,11 @@ public extension Session {
7896
/// The user agent associated with a session.
7997
public let userAgent: String
8098

99+
public init(ipAddress: String, userAgent: String) {
100+
self.ipAddress = ipAddress
101+
self.userAgent = userAgent
102+
}
103+
81104
public static func == (lhs: Self, rhs: Self) -> Bool {
82105
lhs.ipAddress == rhs.ipAddress &&
83106
lhs.userAgent == rhs.userAgent

Sources/StytchCore/StytchClient/Models/User.swift

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,38 @@ public struct User: Sendable {
3434
public let untrustedMetadata: JSON?
3535
/// The user's trusted metadata
3636
public let trustedMetadata: JSON?
37+
38+
public init(
39+
createdAt: Date,
40+
cryptoWallets: [CryptoWallet],
41+
emails: [Email],
42+
userId: Self.ID,
43+
name: Name,
44+
password: Password?,
45+
phoneNumbers: [PhoneNumber],
46+
providers: [Provider],
47+
status: UserStatus,
48+
totps: [TOTP],
49+
webauthnRegistrations: [WebAuthNRegistration],
50+
biometricRegistrations: [BiometricRegistration],
51+
untrustedMetadata: JSON?,
52+
trustedMetadata: JSON?
53+
) {
54+
self.createdAt = createdAt
55+
self.cryptoWallets = cryptoWallets
56+
self.emails = emails
57+
self.userId = userId
58+
self.name = name
59+
self.password = password
60+
self.phoneNumbers = phoneNumbers
61+
self.providers = providers
62+
self.status = status
63+
self.totps = totps
64+
self.webauthnRegistrations = webauthnRegistrations
65+
self.biometricRegistrations = biometricRegistrations
66+
self.untrustedMetadata = untrustedMetadata
67+
self.trustedMetadata = trustedMetadata
68+
}
3769
}
3870

3971
extension User: Equatable {
@@ -83,6 +115,11 @@ public extension User {
83115
let passwordId: ID
84116
let requiresReset: Bool
85117

118+
public init(passwordId: Self.ID, requiresReset: Bool) {
119+
self.passwordId = passwordId
120+
self.requiresReset = requiresReset
121+
}
122+
86123
public static func == (lhs: Self, rhs: Self) -> Bool {
87124
lhs.passwordId == rhs.passwordId &&
88125
lhs.requiresReset == rhs.requiresReset
@@ -103,6 +140,13 @@ public extension User {
103140
/// The verification status of the cryptowallet.
104141
public let verified: Bool
105142

143+
public init(cryptoWalletId: Self.ID, cryptoWalletAddress: String, cryptoWalletType: String, verified: Bool) {
144+
self.cryptoWalletId = cryptoWalletId
145+
self.cryptoWalletAddress = cryptoWalletAddress
146+
self.cryptoWalletType = cryptoWalletType
147+
self.verified = verified
148+
}
149+
106150
public static func == (lhs: Self, rhs: Self) -> Bool {
107151
lhs.cryptoWalletId == rhs.cryptoWalletId &&
108152
lhs.cryptoWalletAddress == rhs.cryptoWalletAddress &&
@@ -121,6 +165,12 @@ public extension User {
121165
/// The verification status of the email.
122166
public let verified: Bool
123167

168+
public init(email: String, emailId: Self.ID, verified: Bool) {
169+
self.email = email
170+
self.emailId = emailId
171+
self.verified = verified
172+
}
173+
124174
public static func == (lhs: Self, rhs: Self) -> Bool {
125175
lhs.email == rhs.email &&
126176
lhs.emailId == rhs.emailId &&
@@ -161,6 +211,13 @@ public extension User {
161211
public var id: ID { oauthUserRegistrationId }
162212
let oauthUserRegistrationId: ID
163213

214+
public init(providerSubject: String, providerType: String, profilePictureUrl: String?, oauthUserRegistrationId: Self.ID) {
215+
self.providerSubject = providerSubject
216+
self.providerType = providerType
217+
self.profilePictureUrl = profilePictureUrl
218+
self.oauthUserRegistrationId = oauthUserRegistrationId
219+
}
220+
164221
public static func == (lhs: Self, rhs: Self) -> Bool {
165222
lhs.providerSubject == rhs.providerSubject &&
166223
lhs.providerType == rhs.providerType &&
@@ -179,6 +236,12 @@ public extension User {
179236
/// The verification status of the phone number.
180237
public let verified: Bool
181238

239+
public init(phoneNumber: String, phoneId: Self.ID, verified: Bool) {
240+
self.phoneNumber = phoneNumber
241+
self.phoneId = phoneId
242+
self.verified = verified
243+
}
244+
182245
public static func == (lhs: Self, rhs: Self) -> Bool {
183246
lhs.phoneNumber == rhs.phoneNumber &&
184247
lhs.phoneId == rhs.phoneId &&
@@ -201,6 +264,11 @@ public extension User {
201264
/// The verification status of the TOTP.
202265
public let verified: Bool
203266

267+
public init(totpId: Self.ID, verified: Bool) {
268+
self.totpId = totpId
269+
self.verified = verified
270+
}
271+
204272
public static func == (lhs: Self, rhs: Self) -> Bool {
205273
lhs.totpId == rhs.totpId &&
206274
lhs.verified == rhs.verified
@@ -219,6 +287,13 @@ public extension User {
219287
public var id: ID { webauthnRegistrationId }
220288
let webauthnRegistrationId: ID
221289

290+
public init(domain: String, userAgent: String, verified: Bool, webauthnRegistrationId: Self.ID) {
291+
self.domain = domain
292+
self.userAgent = userAgent
293+
self.verified = verified
294+
self.webauthnRegistrationId = webauthnRegistrationId
295+
}
296+
222297
public static func == (lhs: Self, rhs: Self) -> Bool {
223298
lhs.domain == rhs.domain &&
224299
lhs.userAgent == rhs.userAgent &&
@@ -235,6 +310,11 @@ public extension User {
235310
public var id: ID { biometricRegistrationId }
236311
let biometricRegistrationId: ID
237312

313+
public init(verified: Bool, biometricRegistrationId: Self.ID) {
314+
self.verified = verified
315+
self.biometricRegistrationId = biometricRegistrationId
316+
}
317+
238318
public static func == (lhs: Self, rhs: Self) -> Bool {
239319
lhs.verified == rhs.verified &&
240320
lhs.biometricRegistrationId == rhs.biometricRegistrationId

0 commit comments

Comments
 (0)