clerk / clerk-ios

Clerk helps developers build user management. We provide streamlined user experiences for your users to sign up, sign in, and manage their profile.
https://clerk.com
MIT License
37 stars 3 forks source link

JWT Token expires fast, and new one is not provided #50

Closed semirteskeredzic closed 4 months ago

semirteskeredzic commented 4 months ago

I am using the token in the Authorization header when sending request to my API endpoint.

Here is how I'm fetching data with async function and using lastActiveToken from clerk.session:

@ObservedObject private var clerk = Clerk.shared

Task {
  do {
    isLoading = true
    defer { isLoading = false }
    try await locationData.fetchLocations(token: clerk.session?.lastActiveToken?.jwt ?? "")
  } catch {
     print(error)
}

This token expires really fast while the app is running (cca 1 minute) and it doesn't get any new one.

When I print the session the exp of the session is expiring a couple of days from now (printed on 2024-05-15):

status: ClerkSDK.SessionStatus.active, expireAt: 2024-05-21 06:43:37 +0000, abandonAt: 2024-06-13 06:43:37 +0000, lastActiveAt: 2024-05-14 06:43:37 +0000

On my server I am logging returned JSON object from response:

[iostest] [2024-05-15 08:33:29] decoded {
[iostest] [2024-05-15 08:33:29]   exp: 1715762069,
[iostest] [2024-05-15 08:33:29]   iat: 1715762009,
[iostest] [2024-05-15 08:33:29]   iss: 'https://x.clerk.accounts.dev',
[iostest] [2024-05-15 08:33:29]   nbf: 1715761999,
[iostest] [2024-05-15 08:33:29]   sid: 'sess_x',
[iostest] [2024-05-15 08:33:29]   sub: 'user_x'
[iostest] [2024-05-15 08:33:29] }
semirteskeredzic commented 4 months ago

After going through the API documentation, I've figured out that I've been using wrong jwt. The correct one is: clerk.session?.getToken()?.jwt This is resolved.