1- import { create } from 'zustand' ;
2- import { createJSONStorage , persist } from 'zustand/middleware' ;
3- import AsyncStorage from '@react-native-async-storage/async-storage' ;
4- import type { CloudRegion , StoredTokens } from '../types/oauth' ;
5- import { TOKEN_REFRESH_BUFFER_MS , OAUTH_SCOPES } from '../constants/oauth' ;
6- import { performOAuthFlow , refreshAccessToken as refreshAccessTokenRequest } from '../lib/oauth' ;
7- import { saveTokens , getTokens , deleteTokens } from '../lib/secureStorage' ;
1+ import AsyncStorage from "@react-native-async-storage/async-storage" ;
2+ import { create } from "zustand" ;
3+ import { createJSONStorage , persist } from "zustand/middleware" ;
4+ import { OAUTH_SCOPES , TOKEN_REFRESH_BUFFER_MS } from "../constants/oauth" ;
5+ import {
6+ performOAuthFlow ,
7+ refreshAccessToken as refreshAccessTokenRequest ,
8+ } from "../lib/oauth" ;
9+ import { deleteTokens , getTokens , saveTokens } from "../lib/secureStorage" ;
10+ import type { CloudRegion , StoredTokens } from "../types/oauth" ;
811
912interface AuthState {
1013 // OAuth state
@@ -49,15 +52,15 @@ export const useAuthStore = create<AuthState>()(
4952 } ) ;
5053
5154 if ( ! result . success || ! result . data ) {
52- throw new Error ( result . error || ' OAuth flow failed' ) ;
55+ throw new Error ( result . error || " OAuth flow failed" ) ;
5356 }
5457
5558 const tokenResponse = result . data ;
5659 const expiresAt = Date . now ( ) + tokenResponse . expires_in * 1000 ;
5760 const projectId = tokenResponse . scoped_teams ?. [ 0 ] ;
5861
5962 if ( ! projectId ) {
60- throw new Error ( ' No team found in OAuth scopes' ) ;
63+ throw new Error ( " No team found in OAuth scopes" ) ;
6164 }
6265
6366 const storedTokens : StoredTokens = {
@@ -87,12 +90,12 @@ export const useAuthStore = create<AuthState>()(
8790 const state = get ( ) ;
8891
8992 if ( ! state . oauthRefreshToken || ! state . cloudRegion ) {
90- throw new Error ( ' No refresh token available' ) ;
93+ throw new Error ( " No refresh token available" ) ;
9194 }
9295
9396 const tokenResponse = await refreshAccessTokenRequest (
9497 state . oauthRefreshToken ,
95- state . cloudRegion
98+ state . cloudRegion ,
9699 ) ;
97100
98101 const expiresAt = Date . now ( ) + tokenResponse . expires_in * 1000 ;
@@ -131,21 +134,22 @@ export const useAuthStore = create<AuthState>()(
131134 return ;
132135 }
133136
134- const timeUntilRefresh = state . tokenExpiry - Date . now ( ) - TOKEN_REFRESH_BUFFER_MS ;
137+ const timeUntilRefresh =
138+ state . tokenExpiry - Date . now ( ) - TOKEN_REFRESH_BUFFER_MS ;
135139
136140 if ( timeUntilRefresh > 0 ) {
137141 refreshTimeoutId = setTimeout ( ( ) => {
138142 get ( )
139143 . refreshAccessToken ( )
140144 . catch ( ( error ) => {
141- console . error ( ' Proactive token refresh failed:' , error ) ;
145+ console . error ( " Proactive token refresh failed:" , error ) ;
142146 } ) ;
143147 } , timeUntilRefresh ) ;
144148 } else {
145149 get ( )
146150 . refreshAccessToken ( )
147151 . catch ( ( error ) => {
148- console . error ( ' Immediate token refresh failed:' , error ) ;
152+ console . error ( " Immediate token refresh failed:" , error ) ;
149153 } ) ;
150154 }
151155 } ,
@@ -176,7 +180,7 @@ export const useAuthStore = create<AuthState>()(
176180 try {
177181 await get ( ) . refreshAccessToken ( ) ;
178182 } catch ( error ) {
179- console . error ( ' Failed to refresh expired token:' , error ) ;
183+ console . error ( " Failed to refresh expired token:" , error ) ;
180184 await deleteTokens ( ) ;
181185 set ( { isLoading : false , isAuthenticated : false } ) ;
182186 return false ;
@@ -187,7 +191,7 @@ export const useAuthStore = create<AuthState>()(
187191 get ( ) . scheduleTokenRefresh ( ) ;
188192 return true ;
189193 } catch ( error ) {
190- console . error ( ' Failed to initialize auth:' , error ) ;
194+ console . error ( " Failed to initialize auth:" , error ) ;
191195 set ( { isLoading : false , isAuthenticated : false } ) ;
192196 return false ;
193197 }
@@ -212,12 +216,12 @@ export const useAuthStore = create<AuthState>()(
212216 } ,
213217 } ) ,
214218 {
215- name : ' posthog-mobile- auth' ,
219+ name : " posthog-auth" ,
216220 storage : createJSONStorage ( ( ) => AsyncStorage ) ,
217221 partialize : ( state ) => ( {
218222 cloudRegion : state . cloudRegion ,
219223 projectId : state . projectId ,
220224 } ) ,
221- }
222- )
225+ } ,
226+ ) ,
223227) ;
0 commit comments