Skip to content

Commit

Permalink
fix: tvos age group
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasgangso committed Apr 19, 2024
1 parent f188560 commit 55ece0d
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions Authentication/Sources/Authentication/Userinfo.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

import Foundation
import Auth0
import Foundation

public func getAgeGroup(_ age: Int?) -> String {
let breakpoints: [Int: String] = [
Expand All @@ -25,8 +25,8 @@ public func getAgeGroup(_ age: Int?) -> String {
return "UNKNOWN"
}

extension Provider {
public struct Profile: Codable {
public extension Provider {
struct Profile: Codable {
public let name: String?
public let ageGroup: String?
public let personId: Int?
Expand All @@ -35,7 +35,7 @@ extension Provider {
private static let profileKey = "profile"
private static let profileExpiryKey = "profile_expiry"

public func userInfo() async -> Profile? {
func userInfo() async -> Profile? {
let ud = UserDefaults.standard

if let expiry = ud.object(forKey: Provider.profileExpiryKey) as? Date {
Expand All @@ -56,19 +56,24 @@ extension Provider {
}
let userInfo = try await authentication().userInfo(withAccessToken: token!).start()

var personId: Int? = nil
if let appMetadata = userInfo.customClaims?["https://members.bcc.no/app_metadata"] as? [String:Any] {
var personId: Int?
if let appMetadata = userInfo.customClaims?["https://members.bcc.no/app_metadata"] as? [String: Any] {
personId = appMetadata["personId"] as? Int
}

let formatter = ISO8601DateFormatter()
formatter.formatOptions = .withFractionalSeconds
var age: Int? = nil
formatter.formatOptions = [
.withFractionalSeconds,
.withFullDate, // Make time always 00.00.00. This is the only way to get fractional seconds to be optional. See https://forums.swift.org/t/iso8601dateformatter-fails-to-parse-a-valid-iso-8601-date/22999/19
]
var age: Int?

if let bd = userInfo.birthdate {
let date = formatter.date(from: bd)!
let ageComponents = Calendar.current.dateComponents([.year], from: date, to: Date.now)
age = ageComponents.year
let date = formatter.date(from: bd)
if let date = date {
let ageComponents = Calendar.current.dateComponents([.year], from: date, to: Date.now)
age = ageComponents.year
}
}

let profile = Profile(
Expand All @@ -88,7 +93,7 @@ extension Provider {
return nil
}

public func clearUserInfoCache() {
func clearUserInfoCache() {
UserDefaults.standard.removeObject(forKey: Provider.profileExpiryKey)
UserDefaults.standard.removeObject(forKey: Provider.profileKey)
}
Expand Down

0 comments on commit 55ece0d

Please sign in to comment.