Skip to content

Commit f59c01c

Browse files
committed
chore(auth): add helper function
1 parent 9c6aecd commit f59c01c

File tree

1 file changed

+68
-78
lines changed

1 file changed

+68
-78
lines changed

packages/core/auth-js/src/GoTrueClient.ts

Lines changed: 68 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,18 @@ export default class GoTrueClient {
381381
this.initialize()
382382
}
383383

384+
/**
385+
* Centralizes return handling with optional error throwing. When `throwOnError` is enabled
386+
* and the provided result contains a non-nullish error, the error is thrown instead of
387+
* being returned. This ensures consistent behavior across all public API methods.
388+
*/
389+
private _returnResult<T extends { data: any; error: any }>(result: T): T {
390+
if (this.throwOnError && result && result.error) {
391+
throw result.error
392+
}
393+
return result
394+
}
395+
384396
private _debug(...args: any[]): GoTrueClient {
385397
if (this.logDebugMessages) {
386398
this.logger(
@@ -514,8 +526,7 @@ export default class GoTrueClient {
514526
const { data, error } = res
515527

516528
if (error || !data) {
517-
if (this.throwOnError) throw error
518-
return { data: { user: null, session: null }, error: error }
529+
return this._returnResult({ data: { user: null, session: null }, error: error })
519530
}
520531
const session: Session | null = data.session
521532
const user: User | null = data.user
@@ -525,11 +536,10 @@ export default class GoTrueClient {
525536
await this._notifyAllSubscribers('SIGNED_IN', session)
526537
}
527538

528-
return { data: { user, session }, error: null }
539+
return this._returnResult({ data: { user, session }, error: null })
529540
} catch (error) {
530541
if (isAuthError(error)) {
531-
if (this.throwOnError) throw error
532-
return { data: { user: null, session: null }, error }
542+
return this._returnResult({ data: { user: null, session: null }, error })
533543
}
534544

535545
throw error
@@ -594,8 +604,7 @@ export default class GoTrueClient {
594604
const { data, error } = res
595605

596606
if (error || !data) {
597-
if (this.throwOnError) throw error
598-
return { data: { user: null, session: null }, error: error }
607+
return this._returnResult({ data: { user: null, session: null }, error: error })
599608
}
600609

601610
const session: Session | null = data.session
@@ -606,11 +615,10 @@ export default class GoTrueClient {
606615
await this._notifyAllSubscribers('SIGNED_IN', session)
607616
}
608617

609-
return { data: { user, session }, error: null }
618+
return this._returnResult({ data: { user, session }, error: null })
610619
} catch (error) {
611620
if (isAuthError(error)) {
612-
if (this.throwOnError) throw error
613-
return { data: { user: null, session: null }, error }
621+
return this._returnResult({ data: { user: null, session: null }, error })
614622
}
615623

616624
throw error
@@ -660,29 +668,26 @@ export default class GoTrueClient {
660668
const { data, error } = res
661669

662670
if (error) {
663-
if (this.throwOnError) throw error
664-
return { data: { user: null, session: null }, error }
671+
return this._returnResult({ data: { user: null, session: null }, error })
665672
} else if (!data || !data.session || !data.user) {
666673
const invalidTokenError = new AuthInvalidTokenResponseError()
667-
if (this.throwOnError) throw invalidTokenError
668-
return { data: { user: null, session: null }, error: invalidTokenError }
674+
return this._returnResult({ data: { user: null, session: null }, error: invalidTokenError })
669675
}
670676
if (data.session) {
671677
await this._saveSession(data.session)
672678
await this._notifyAllSubscribers('SIGNED_IN', data.session)
673679
}
674-
return {
680+
return this._returnResult({
675681
data: {
676682
user: data.user,
677683
session: data.session,
678684
...(data.weak_password ? { weakPassword: data.weak_password } : null),
679685
},
680686
error,
681-
}
687+
})
682688
} catch (error) {
683689
if (isAuthError(error)) {
684-
if (this.throwOnError) throw error
685-
return { data: { user: null, session: null }, error }
690+
return this._returnResult({ data: { user: null, session: null }, error })
686691
}
687692
throw error
688693
}
@@ -858,20 +863,17 @@ export default class GoTrueClient {
858863
throw error
859864
}
860865
if (!data || !data.session || !data.user) {
861-
return {
862-
data: { user: null, session: null },
863-
error: new AuthInvalidTokenResponseError(),
864-
}
866+
const invalidTokenError = new AuthInvalidTokenResponseError()
867+
return this._returnResult({ data: { user: null, session: null }, error: invalidTokenError })
865868
}
866869
if (data.session) {
867870
await this._saveSession(data.session)
868871
await this._notifyAllSubscribers('SIGNED_IN', data.session)
869872
}
870-
return { data: { ...data }, error }
873+
return this._returnResult({ data: { ...data }, error })
871874
} catch (error) {
872875
if (isAuthError(error)) {
873-
if (this.throwOnError) throw error
874-
return { data: { user: null, session: null }, error }
876+
return this._returnResult({ data: { user: null, session: null }, error })
875877
}
876878

877879
throw error
@@ -1047,20 +1049,17 @@ export default class GoTrueClient {
10471049
throw error
10481050
}
10491051
if (!data || !data.session || !data.user) {
1050-
return {
1051-
data: { user: null, session: null },
1052-
error: new AuthInvalidTokenResponseError(),
1053-
}
1052+
const invalidTokenError = new AuthInvalidTokenResponseError()
1053+
return this._returnResult({ data: { user: null, session: null }, error: invalidTokenError })
10541054
}
10551055
if (data.session) {
10561056
await this._saveSession(data.session)
10571057
await this._notifyAllSubscribers('SIGNED_IN', data.session)
10581058
}
1059-
return { data: { ...data }, error }
1059+
return this._returnResult({ data: { ...data }, error })
10601060
} catch (error) {
10611061
if (isAuthError(error)) {
1062-
if (this.throwOnError) throw error
1063-
return { data: { user: null, session: null }, error }
1062+
return this._returnResult({ data: { user: null, session: null }, error })
10641063
}
10651064

10661065
throw error
@@ -1096,20 +1095,23 @@ export default class GoTrueClient {
10961095
throw error
10971096
}
10981097
if (!data || !data.session || !data.user) {
1099-
return {
1098+
const invalidTokenError = new AuthInvalidTokenResponseError()
1099+
return this._returnResult({
11001100
data: { user: null, session: null, redirectType: null },
1101-
error: new AuthInvalidTokenResponseError(),
1102-
}
1101+
error: invalidTokenError,
1102+
})
11031103
}
11041104
if (data.session) {
11051105
await this._saveSession(data.session)
11061106
await this._notifyAllSubscribers('SIGNED_IN', data.session)
11071107
}
1108-
return { data: { ...data, redirectType: redirectType ?? null }, error }
1108+
return this._returnResult({ data: { ...data, redirectType: redirectType ?? null }, error })
11091109
} catch (error) {
11101110
if (isAuthError(error)) {
1111-
if (this.throwOnError) throw error
1112-
return { data: { user: null, session: null, redirectType: null }, error }
1111+
return this._returnResult({
1112+
data: { user: null, session: null, redirectType: null },
1113+
error,
1114+
})
11131115
}
11141116

11151117
throw error
@@ -1138,25 +1140,19 @@ export default class GoTrueClient {
11381140

11391141
const { data, error } = res
11401142
if (error) {
1141-
if (this.throwOnError) throw error
1142-
return { data: { user: null, session: null }, error }
1143+
return this._returnResult({ data: { user: null, session: null }, error })
11431144
} else if (!data || !data.session || !data.user) {
11441145
const invalidTokenError = new AuthInvalidTokenResponseError()
1145-
if (this.throwOnError) throw invalidTokenError
1146-
return {
1147-
data: { user: null, session: null },
1148-
error: invalidTokenError,
1149-
}
1146+
return this._returnResult({ data: { user: null, session: null }, error: invalidTokenError })
11501147
}
11511148
if (data.session) {
11521149
await this._saveSession(data.session)
11531150
await this._notifyAllSubscribers('SIGNED_IN', data.session)
11541151
}
1155-
return { data, error }
1152+
return this._returnResult({ data, error })
11561153
} catch (error) {
11571154
if (isAuthError(error)) {
1158-
if (this.throwOnError) throw error
1159-
return { data: { user: null, session: null }, error }
1155+
return this._returnResult({ data: { user: null, session: null }, error })
11601156
}
11611157
throw error
11621158
}
@@ -1203,8 +1199,7 @@ export default class GoTrueClient {
12031199
},
12041200
redirectTo: options?.emailRedirectTo,
12051201
})
1206-
if (this.throwOnError && error) throw error
1207-
return { data: { user: null, session: null }, error }
1202+
return this._returnResult({ data: { user: null, session: null }, error })
12081203
}
12091204
if ('phone' in credentials) {
12101205
const { phone, options } = credentials
@@ -1218,14 +1213,15 @@ export default class GoTrueClient {
12181213
channel: options?.channel ?? 'sms',
12191214
},
12201215
})
1221-
if (this.throwOnError && error) throw error
1222-
return { data: { user: null, session: null, messageId: data?.message_id }, error }
1216+
return this._returnResult({
1217+
data: { user: null, session: null, messageId: data?.message_id },
1218+
error,
1219+
})
12231220
}
12241221
throw new AuthInvalidCredentialsError('You must provide either an email or phone number.')
12251222
} catch (error) {
12261223
if (isAuthError(error)) {
1227-
if (this.throwOnError) throw error
1228-
return { data: { user: null, session: null }, error }
1224+
return this._returnResult({ data: { user: null, session: null }, error })
12291225
}
12301226

12311227
throw error
@@ -1274,11 +1270,10 @@ export default class GoTrueClient {
12741270
)
12751271
}
12761272

1277-
return { data: { user, session }, error: null }
1273+
return this._returnResult({ data: { user, session }, error: null })
12781274
} catch (error) {
12791275
if (isAuthError(error)) {
1280-
if (this.throwOnError) throw error
1281-
return { data: { user: null, session: null }, error }
1276+
return this._returnResult({ data: { user: null, session: null }, error })
12821277
}
12831278

12841279
throw error
@@ -1310,7 +1305,7 @@ export default class GoTrueClient {
13101305
)
13111306
}
13121307

1313-
return await _request(this.fetch, 'POST', `${this.url}/sso`, {
1308+
const result = await _request(this.fetch, 'POST', `${this.url}/sso`, {
13141309
body: {
13151310
...('providerId' in params ? { provider_id: params.providerId } : null),
13161311
...('domain' in params ? { domain: params.domain } : null),
@@ -1325,10 +1320,10 @@ export default class GoTrueClient {
13251320
headers: this.headers,
13261321
xform: _ssoResponse,
13271322
})
1323+
return this._returnResult(result)
13281324
} catch (error) {
13291325
if (isAuthError(error)) {
1330-
if (this.throwOnError) throw error
1331-
return { data: null, error }
1326+
return this._returnResult({ data: null, error })
13321327
}
13331328
throw error
13341329
}
@@ -1360,13 +1355,11 @@ export default class GoTrueClient {
13601355
headers: this.headers,
13611356
jwt: session.access_token,
13621357
})
1363-
if (this.throwOnError && error) throw error
1364-
return { data: { user: null, session: null }, error }
1358+
return this._returnResult({ data: { user: null, session: null }, error })
13651359
})
13661360
} catch (error) {
13671361
if (isAuthError(error)) {
1368-
if (this.throwOnError) throw error
1369-
return { data: { user: null, session: null }, error }
1362+
return this._returnResult({ data: { user: null, session: null }, error })
13701363
}
13711364
throw error
13721365
}
@@ -1389,8 +1382,7 @@ export default class GoTrueClient {
13891382
},
13901383
redirectTo: options?.emailRedirectTo,
13911384
})
1392-
if (this.throwOnError && error) throw error
1393-
return { data: { user: null, session: null }, error }
1385+
return this._returnResult({ data: { user: null, session: null }, error })
13941386
} else if ('phone' in credentials) {
13951387
const { phone, type, options } = credentials
13961388
const { data, error } = await _request(this.fetch, 'POST', endpoint, {
@@ -1401,8 +1393,10 @@ export default class GoTrueClient {
14011393
gotrue_meta_security: { captcha_token: options?.captchaToken },
14021394
},
14031395
})
1404-
if (this.throwOnError && error) throw error
1405-
return { data: { user: null, session: null, messageId: data?.message_id }, error }
1396+
return this._returnResult({
1397+
data: { user: null, session: null, messageId: data?.message_id },
1398+
error,
1399+
})
14061400
}
14071401
throw new AuthInvalidCredentialsError(
14081402
'You must provide either an email or phone number and a type'
@@ -1782,18 +1776,16 @@ export default class GoTrueClient {
17821776
xform: _userResponse,
17831777
})
17841778
if (userError) {
1785-
if (this.throwOnError) throw userError
17861779
throw userError
17871780
}
17881781
session.user = data.user as User
17891782
await this._saveSession(session)
17901783
await this._notifyAllSubscribers('USER_UPDATED', session)
1791-
return { data: { user: session.user }, error: null }
1784+
return this._returnResult({ data: { user: session.user }, error: null })
17921785
})
17931786
} catch (error) {
17941787
if (isAuthError(error)) {
1795-
if (this.throwOnError) throw error
1796-
return { data: { user: null }, error }
1788+
return this._returnResult({ data: { user: null }, error })
17971789
}
17981790

17991791
throw error
@@ -1840,8 +1832,7 @@ export default class GoTrueClient {
18401832
currentSession.refresh_token
18411833
)
18421834
if (error) {
1843-
if (this.throwOnError) throw error
1844-
return { data: { user: null, session: null }, error: error }
1835+
return this._returnResult({ data: { user: null, session: null }, error: error })
18451836
}
18461837

18471838
if (!refreshedSession) {
@@ -1915,15 +1906,14 @@ export default class GoTrueClient {
19151906
}
19161907

19171908
if (!session) {
1918-
return { data: { user: null, session: null }, error: null }
1909+
return this._returnResult({ data: { user: null, session: null }, error: null })
19191910
}
19201911

1921-
return { data: { user: session.user, session }, error: null }
1912+
return this._returnResult({ data: { user: session.user, session }, error: null })
19221913
})
19231914
} catch (error) {
19241915
if (isAuthError(error)) {
1925-
if (this.throwOnError) throw error
1926-
return { data: { user: null, session: null }, error }
1916+
return this._returnResult({ data: { user: null, session: null }, error })
19271917
}
19281918

19291919
throw error

0 commit comments

Comments
 (0)