react-native-nitro-auth 0.5.4 → 0.5.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +60 -30
- package/android/src/main/cpp/JniOnLoad.cpp +3 -1
- package/android/src/main/cpp/PlatformAuth+Android.cpp +11 -11
- package/android/src/main/java/com/auth/AuthAdapter.kt +100 -116
- package/android/src/main/java/com/auth/NitroAuthModule.kt +8 -1
- package/ios/AuthAdapter.swift +62 -28
- package/lib/commonjs/index.js +23 -1
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/service.js +31 -6
- package/lib/commonjs/service.js.map +1 -1
- package/lib/commonjs/use-auth.js +11 -22
- package/lib/commonjs/use-auth.js.map +1 -1
- package/lib/commonjs/utils/auth-error.js +37 -0
- package/lib/commonjs/utils/auth-error.js.map +1 -0
- package/lib/module/index.js +1 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/service.js +31 -6
- package/lib/module/service.js.map +1 -1
- package/lib/module/use-auth.js +11 -22
- package/lib/module/use-auth.js.map +1 -1
- package/lib/module/utils/auth-error.js +30 -0
- package/lib/module/utils/auth-error.js.map +1 -0
- package/lib/typescript/commonjs/index.d.ts +1 -0
- package/lib/typescript/commonjs/index.d.ts.map +1 -1
- package/lib/typescript/commonjs/service.d.ts.map +1 -1
- package/lib/typescript/commonjs/use-auth.d.ts +2 -1
- package/lib/typescript/commonjs/use-auth.d.ts.map +1 -1
- package/lib/typescript/commonjs/utils/auth-error.d.ts +16 -0
- package/lib/typescript/commonjs/utils/auth-error.d.ts.map +1 -0
- package/lib/typescript/module/index.d.ts +1 -0
- package/lib/typescript/module/index.d.ts.map +1 -1
- package/lib/typescript/module/service.d.ts.map +1 -1
- package/lib/typescript/module/use-auth.d.ts +2 -1
- package/lib/typescript/module/use-auth.d.ts.map +1 -1
- package/lib/typescript/module/utils/auth-error.d.ts +16 -0
- package/lib/typescript/module/utils/auth-error.d.ts.map +1 -0
- package/nitrogen/generated/android/NitroAuthOnLoad.cpp +22 -17
- package/nitrogen/generated/android/NitroAuthOnLoad.hpp +13 -4
- package/package.json +7 -7
- package/src/index.ts +1 -0
- package/src/service.ts +32 -6
- package/src/use-auth.ts +21 -86
- package/src/utils/auth-error.ts +49 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["useState","useEffect","useCallback","useMemo","AuthService","areScopesEqual","left","right","length","
|
|
1
|
+
{"version":3,"names":["useState","useEffect","useCallback","useMemo","AuthService","AuthError","areScopesEqual","left","right","length","i","useAuth","state","setState","user","currentUser","scopes","grantedScopes","loading","error","undefined","syncStateFromService","nextLoading","nextError","nextUser","nextScopes","prev","login","provider","options","e","from","logout","requestScopes","newScopes","revokeScopes","scopesToRevoke","getAccessToken","refreshToken","tokens","silentRestore","unsubscribe","onAuthStateChanged","hasPlayServices"],"sourceRoot":"../../src","sources":["use-auth.ts"],"mappings":";;AAAA,SAASA,QAAQ,EAAEC,SAAS,EAAEC,WAAW,EAAEC,OAAO,QAAQ,OAAO;AAOjE,SAASC,WAAW,QAAQ,WAAW;AACvC,SAASC,SAAS,QAAQ,uBAAoB;AAS9C,MAAMC,cAAc,GAAGA,CAACC,IAAc,EAAEC,KAAe,KAAc;EACnE,IAAID,IAAI,CAACE,MAAM,KAAKD,KAAK,CAACC,MAAM,EAAE,OAAO,KAAK;EAC9C,KAAK,IAAIC,CAAC,GAAG,CAAC,EAAEA,CAAC,GAAGH,IAAI,CAACE,MAAM,EAAEC,CAAC,IAAI,CAAC,EAAE;IACvC,IAAIH,IAAI,CAACG,CAAC,CAAC,KAAKF,KAAK,CAACE,CAAC,CAAC,EAAE,OAAO,KAAK;EACxC;EACA,OAAO,IAAI;AACb,CAAC;AAaD,OAAO,SAASC,OAAOA,CAAA,EAAkB;EACvC,MAAM,CAACC,KAAK,EAAEC,QAAQ,CAAC,GAAGb,QAAQ,CAAY;IAC5Cc,IAAI,EAAEV,WAAW,CAACW,WAAW;IAC7BC,MAAM,EAAEZ,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AAS,CAAC0B,IAAI,CAACD,CAAC,CAAC;MAC/BjB,QAAQ,CAAEa,IAAI,KAAM;QAAE,GAAGA,IAAI;QAAER,OAAO,EAAE,KAAK;QAAEC;MAAM,CAAC,CAAC,CAAC;MACxD,MAAMA,KAAK;IACb;EACF,CAAC,EAAE,CAACE,oBAAoB,CAAC,CAAC;EAE1BpB,SAAS,CAAC,MAAM;IACd,MAAMwC,WAAW,GAAGrC,WAAW,CAACsC,kBAAkB,CAAE3B,WAAW,IAAK;MAClE,MAAMU,UAAU,GAAGrB,WAAW,CAACa,aAAa;MAC5CJ,QAAQ,CAAEa,IAAI,IAAK;QACjB,IACEA,IAAI,CAACZ,IAAI,KAAKC,WAAW,IACzBT,cAAc,CAACoB,IAAI,CAACV,MAAM,EAAES,UAAU,CAAC,IACvCC,IAAI,CAACR,OAAO,KAAK,KAAK,EACtB;UACA,OAAOQ,IAAI;QACb;QACA,OAAO;UAAE,GAAGA,IAAI;UAAEZ,IAAI,EAAEC,WAAW;UAAEC,MAAM,EAAES,UAAU;UAAEP,OAAO,EAAE;QAAM,CAAC;MAC3E,CAAC,CAAC;IACJ,CAAC,CAAC;IACF,OAAOuB,WAAW;EACpB,CAAC,EAAE,EAAE,CAAC;EAEN,OAAOtC,OAAO,CACZ,OAAO;IACL,GAAGS,KAAK;IACR+B,eAAe,EAAEvC,WAAW,CAACuC,eAAe;IAC5ChB,KAAK;IACLK,MAAM;IACNC,aAAa;IACbE,YAAY;IACZE,cAAc;IACdC,YAAY;IACZE;EACF,CAAC,CAAC,EACF,CAAC5B,KAAK,EAAEe,KAAK,EAAEK,MAAM,EAAEC,aAAa,EAAEE,YAAY,EAAEE,cAAc,EAAEC,YAAY,EAAEE,aAAa,CACjG,CAAC;AACH","ignoreList":[]}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const AUTH_ERROR_CODES = new Set(["cancelled", "timeout", "popup_blocked", "network_error", "configuration_error", "unsupported_provider", "invalid_state", "invalid_nonce", "token_error", "no_id_token", "parse_error", "refresh_failed", "unknown"]);
|
|
4
|
+
export function isAuthErrorCode(value) {
|
|
5
|
+
return AUTH_ERROR_CODES.has(value);
|
|
6
|
+
}
|
|
7
|
+
export function toAuthErrorCode(raw) {
|
|
8
|
+
return isAuthErrorCode(raw) ? raw : "unknown";
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Typed error thrown by all AuthService operations.
|
|
13
|
+
*
|
|
14
|
+
* - `code` — always a valid `AuthErrorCode`, safe to switch on
|
|
15
|
+
* - `underlyingMessage` — the raw platform message when it differs from `code`
|
|
16
|
+
*/
|
|
17
|
+
export class AuthError extends Error {
|
|
18
|
+
constructor(raw) {
|
|
19
|
+
const message = raw instanceof Error ? raw.message : String(raw);
|
|
20
|
+
const code = toAuthErrorCode(message);
|
|
21
|
+
super(code);
|
|
22
|
+
this.name = "AuthError";
|
|
23
|
+
this.code = code;
|
|
24
|
+
this.underlyingMessage = code !== message ? message : undefined;
|
|
25
|
+
}
|
|
26
|
+
static from(e) {
|
|
27
|
+
return e instanceof AuthError ? e : new AuthError(e);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=auth-error.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["AUTH_ERROR_CODES","Set","isAuthErrorCode","value","has","toAuthErrorCode","raw","AuthError","Error","constructor","message","String","code","name","underlyingMessage","undefined","from","e"],"sourceRoot":"../../../src","sources":["utils/auth-error.ts"],"mappings":";;AAEA,MAAMA,gBAAqC,GAAG,IAAIC,GAAG,CAAgB,CACnE,WAAW,EACX,SAAS,EACT,eAAe,EACf,eAAe,EACf,qBAAqB,EACrB,sBAAsB,EACtB,eAAe,EACf,eAAe,EACf,aAAa,EACb,aAAa,EACb,aAAa,EACb,gBAAgB,EAChB,SAAS,CACV,CAAC;AAEF,OAAO,SAASC,eAAeA,CAACC,KAAa,EAA0B;EACrE,OAAOH,gBAAgB,CAACI,GAAG,CAACD,KAAK,CAAC;AACpC;AAEA,OAAO,SAASE,eAAeA,CAACC,GAAW,EAAiB;EAC1D,OAAOJ,eAAe,CAACI,GAAG,CAAC,GAAGA,GAAG,GAAG,SAAS;AAC/C;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMC,SAAS,SAASC,KAAK,CAAC;EAInCC,WAAWA,CAACH,GAAY,EAAE;IACxB,MAAMI,OAAO,GAAGJ,GAAG,YAAYE,KAAK,GAAGF,GAAG,CAACI,OAAO,GAAGC,MAAM,CAACL,GAAG,CAAC;IAChE,MAAMM,IAAI,GAAGP,eAAe,CAACK,OAAO,CAAC;IACrC,KAAK,CAACE,IAAI,CAAC;IACX,IAAI,CAACC,IAAI,GAAG,WAAW;IACvB,IAAI,CAACD,IAAI,GAAGA,IAAI;IAChB,IAAI,CAACE,iBAAiB,GAAGF,IAAI,KAAKF,OAAO,GAAGA,OAAO,GAAGK,SAAS;EACjE;EAEA,OAAOC,IAAIA,CAACC,CAAU,EAAa;IACjC,OAAOA,CAAC,YAAYV,SAAS,GAAGU,CAAC,GAAG,IAAIV,SAAS,CAACU,CAAC,CAAC;EACtD;AACF","ignoreList":[]}
|
|
@@ -2,4 +2,5 @@ export * from "./Auth.nitro";
|
|
|
2
2
|
export * from "./ui/social-button";
|
|
3
3
|
export { useAuth, type UseAuthReturn } from "./use-auth";
|
|
4
4
|
export { AuthService } from "./service";
|
|
5
|
+
export { AuthError, isAuthErrorCode, toAuthErrorCode } from "./utils/auth-error";
|
|
5
6
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,oBAAoB,CAAC;AACnC,OAAO,EAAE,OAAO,EAAE,KAAK,aAAa,EAAE,MAAM,YAAY,CAAC;AACzD,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,oBAAoB,CAAC;AACnC,OAAO,EAAE,OAAO,EAAE,KAAK,aAAa,EAAE,MAAM,YAAY,CAAC;AACzD,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AACxC,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"service.d.ts","sourceRoot":"","sources":["../../../src/service.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,IAAI,EAKL,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"service.d.ts","sourceRoot":"","sources":["../../../src/service.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,IAAI,EAKL,MAAM,cAAc,CAAC;AAKtB,eAAO,MAAM,WAAW,EAAE,IA0FzB,CAAC"}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import type { AuthUser, AuthProvider, LoginOptions, AuthTokens } from "./Auth.nitro";
|
|
2
|
+
import { AuthError } from "./utils/auth-error";
|
|
2
3
|
type AuthState = {
|
|
3
4
|
user: AuthUser | undefined;
|
|
4
5
|
scopes: string[];
|
|
5
6
|
loading: boolean;
|
|
6
|
-
error:
|
|
7
|
+
error: AuthError | undefined;
|
|
7
8
|
};
|
|
8
9
|
export type UseAuthReturn = AuthState & {
|
|
9
10
|
hasPlayServices: boolean;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-auth.d.ts","sourceRoot":"","sources":["../../../src/use-auth.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,QAAQ,EACR,YAAY,EACZ,YAAY,EACZ,UAAU,EACX,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"use-auth.d.ts","sourceRoot":"","sources":["../../../src/use-auth.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,QAAQ,EACR,YAAY,EACZ,YAAY,EACZ,UAAU,EACX,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAE/C,KAAK,SAAS,GAAG;IACf,IAAI,EAAE,QAAQ,GAAG,SAAS,CAAC;IAC3B,MAAM,EAAE,MAAM,EAAE,CAAC;IACjB,OAAO,EAAE,OAAO,CAAC;IACjB,KAAK,EAAE,SAAS,GAAG,SAAS,CAAC;CAC9B,CAAC;AAUF,MAAM,MAAM,aAAa,GAAG,SAAS,GAAG;IACtC,eAAe,EAAE,OAAO,CAAC;IACzB,KAAK,EAAE,CAAC,QAAQ,EAAE,YAAY,EAAE,OAAO,CAAC,EAAE,YAAY,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IACzE,MAAM,EAAE,MAAM,IAAI,CAAC;IACnB,aAAa,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IACnD,YAAY,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAClD,cAAc,EAAE,MAAM,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAAC;IAClD,YAAY,EAAE,MAAM,OAAO,CAAC,UAAU,CAAC,CAAC;IACxC,aAAa,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CACpC,CAAC;AAEF,wBAAgB,OAAO,IAAI,aAAa,CAuIvC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { AuthErrorCode } from "../Auth.nitro";
|
|
2
|
+
export declare function isAuthErrorCode(value: string): value is AuthErrorCode;
|
|
3
|
+
export declare function toAuthErrorCode(raw: string): AuthErrorCode;
|
|
4
|
+
/**
|
|
5
|
+
* Typed error thrown by all AuthService operations.
|
|
6
|
+
*
|
|
7
|
+
* - `code` — always a valid `AuthErrorCode`, safe to switch on
|
|
8
|
+
* - `underlyingMessage` — the raw platform message when it differs from `code`
|
|
9
|
+
*/
|
|
10
|
+
export declare class AuthError extends Error {
|
|
11
|
+
readonly code: AuthErrorCode;
|
|
12
|
+
readonly underlyingMessage: string | undefined;
|
|
13
|
+
constructor(raw: unknown);
|
|
14
|
+
static from(e: unknown): AuthError;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=auth-error.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth-error.d.ts","sourceRoot":"","sources":["../../../../src/utils/auth-error.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAkBnD,wBAAgB,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,KAAK,IAAI,aAAa,CAErE;AAED,wBAAgB,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,aAAa,CAE1D;AAED;;;;;GAKG;AACH,qBAAa,SAAU,SAAQ,KAAK;IAClC,QAAQ,CAAC,IAAI,EAAE,aAAa,CAAC;IAC7B,QAAQ,CAAC,iBAAiB,EAAE,MAAM,GAAG,SAAS,CAAC;gBAEnC,GAAG,EAAE,OAAO;IASxB,MAAM,CAAC,IAAI,CAAC,CAAC,EAAE,OAAO,GAAG,SAAS;CAGnC"}
|
|
@@ -2,4 +2,5 @@ export * from "./Auth.nitro";
|
|
|
2
2
|
export * from "./ui/social-button";
|
|
3
3
|
export { useAuth, type UseAuthReturn } from "./use-auth";
|
|
4
4
|
export { AuthService } from "./service";
|
|
5
|
+
export { AuthError, isAuthErrorCode, toAuthErrorCode } from "./utils/auth-error";
|
|
5
6
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,oBAAoB,CAAC;AACnC,OAAO,EAAE,OAAO,EAAE,KAAK,aAAa,EAAE,MAAM,YAAY,CAAC;AACzD,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,oBAAoB,CAAC;AACnC,OAAO,EAAE,OAAO,EAAE,KAAK,aAAa,EAAE,MAAM,YAAY,CAAC;AACzD,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AACxC,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"service.d.ts","sourceRoot":"","sources":["../../../src/service.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,IAAI,EAKL,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"service.d.ts","sourceRoot":"","sources":["../../../src/service.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,IAAI,EAKL,MAAM,cAAc,CAAC;AAKtB,eAAO,MAAM,WAAW,EAAE,IA0FzB,CAAC"}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import type { AuthUser, AuthProvider, LoginOptions, AuthTokens } from "./Auth.nitro";
|
|
2
|
+
import { AuthError } from "./utils/auth-error";
|
|
2
3
|
type AuthState = {
|
|
3
4
|
user: AuthUser | undefined;
|
|
4
5
|
scopes: string[];
|
|
5
6
|
loading: boolean;
|
|
6
|
-
error:
|
|
7
|
+
error: AuthError | undefined;
|
|
7
8
|
};
|
|
8
9
|
export type UseAuthReturn = AuthState & {
|
|
9
10
|
hasPlayServices: boolean;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-auth.d.ts","sourceRoot":"","sources":["../../../src/use-auth.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,QAAQ,EACR,YAAY,EACZ,YAAY,EACZ,UAAU,EACX,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"use-auth.d.ts","sourceRoot":"","sources":["../../../src/use-auth.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,QAAQ,EACR,YAAY,EACZ,YAAY,EACZ,UAAU,EACX,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAE/C,KAAK,SAAS,GAAG;IACf,IAAI,EAAE,QAAQ,GAAG,SAAS,CAAC;IAC3B,MAAM,EAAE,MAAM,EAAE,CAAC;IACjB,OAAO,EAAE,OAAO,CAAC;IACjB,KAAK,EAAE,SAAS,GAAG,SAAS,CAAC;CAC9B,CAAC;AAUF,MAAM,MAAM,aAAa,GAAG,SAAS,GAAG;IACtC,eAAe,EAAE,OAAO,CAAC;IACzB,KAAK,EAAE,CAAC,QAAQ,EAAE,YAAY,EAAE,OAAO,CAAC,EAAE,YAAY,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IACzE,MAAM,EAAE,MAAM,IAAI,CAAC;IACnB,aAAa,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IACnD,YAAY,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAClD,cAAc,EAAE,MAAM,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAAC;IAClD,YAAY,EAAE,MAAM,OAAO,CAAC,UAAU,CAAC,CAAC;IACxC,aAAa,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CACpC,CAAC;AAEF,wBAAgB,OAAO,IAAI,aAAa,CAuIvC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { AuthErrorCode } from "../Auth.nitro";
|
|
2
|
+
export declare function isAuthErrorCode(value: string): value is AuthErrorCode;
|
|
3
|
+
export declare function toAuthErrorCode(raw: string): AuthErrorCode;
|
|
4
|
+
/**
|
|
5
|
+
* Typed error thrown by all AuthService operations.
|
|
6
|
+
*
|
|
7
|
+
* - `code` — always a valid `AuthErrorCode`, safe to switch on
|
|
8
|
+
* - `underlyingMessage` — the raw platform message when it differs from `code`
|
|
9
|
+
*/
|
|
10
|
+
export declare class AuthError extends Error {
|
|
11
|
+
readonly code: AuthErrorCode;
|
|
12
|
+
readonly underlyingMessage: string | undefined;
|
|
13
|
+
constructor(raw: unknown);
|
|
14
|
+
static from(e: unknown): AuthError;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=auth-error.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth-error.d.ts","sourceRoot":"","sources":["../../../../src/utils/auth-error.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAkBnD,wBAAgB,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,KAAK,IAAI,aAAa,CAErE;AAED,wBAAgB,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,aAAa,CAE1D;AAED;;;;;GAKG;AACH,qBAAa,SAAU,SAAQ,KAAK;IAClC,QAAQ,CAAC,IAAI,EAAE,aAAa,CAAC;IAC7B,QAAQ,CAAC,iBAAiB,EAAE,MAAM,GAAG,SAAS,CAAC;gBAEnC,GAAG,EAAE,OAAO;IASxB,MAAM,CAAC,IAAI,CAAC,CAAC,EAAE,OAAO,GAAG,SAAS;CAGnC"}
|
|
@@ -20,25 +20,30 @@
|
|
|
20
20
|
namespace margelo::nitro::NitroAuth {
|
|
21
21
|
|
|
22
22
|
int initialize(JavaVM* vm) {
|
|
23
|
+
return facebook::jni::initialize(vm, []() {
|
|
24
|
+
::margelo::nitro::NitroAuth::registerAllNatives();
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
void registerAllNatives() {
|
|
23
31
|
using namespace margelo::nitro;
|
|
24
32
|
using namespace margelo::nitro::NitroAuth;
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
}
|
|
40
|
-
);
|
|
41
|
-
});
|
|
33
|
+
|
|
34
|
+
// Register native JNI methods
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
// Register Nitro Hybrid Objects
|
|
38
|
+
HybridObjectRegistry::registerHybridObjectConstructor(
|
|
39
|
+
"Auth",
|
|
40
|
+
[]() -> std::shared_ptr<HybridObject> {
|
|
41
|
+
static_assert(std::is_default_constructible_v<HybridAuth>,
|
|
42
|
+
"The HybridObject \"HybridAuth\" is not default-constructible! "
|
|
43
|
+
"Create a public constructor that takes zero arguments to be able to autolink this HybridObject.");
|
|
44
|
+
return std::make_shared<HybridAuth>();
|
|
45
|
+
}
|
|
46
|
+
);
|
|
42
47
|
}
|
|
43
48
|
|
|
44
49
|
} // namespace margelo::nitro::NitroAuth
|
|
@@ -6,20 +6,29 @@
|
|
|
6
6
|
///
|
|
7
7
|
|
|
8
8
|
#include <jni.h>
|
|
9
|
+
#include <functional>
|
|
9
10
|
#include <NitroModules/NitroDefines.hpp>
|
|
10
11
|
|
|
11
12
|
namespace margelo::nitro::NitroAuth {
|
|
12
13
|
|
|
14
|
+
[[deprecated("Use registerNatives() instead.")]]
|
|
15
|
+
int initialize(JavaVM* vm);
|
|
16
|
+
|
|
13
17
|
/**
|
|
14
|
-
*
|
|
15
|
-
* Call this in your `JNI_OnLoad` function (probably inside `cpp-adapter.cpp`)
|
|
18
|
+
* Register the native (C++) part of NitroAuth, and autolinks all Hybrid Objects.
|
|
19
|
+
* Call this in your `JNI_OnLoad` function (probably inside `cpp-adapter.cpp`),
|
|
20
|
+
* inside a `facebook::jni::initialize(vm, ...)` call.
|
|
16
21
|
* Example:
|
|
17
22
|
* ```cpp (cpp-adapter.cpp)
|
|
18
23
|
* JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void*) {
|
|
19
|
-
* return
|
|
24
|
+
* return facebook::jni::initialize(vm, []() {
|
|
25
|
+
* // register all NitroAuth HybridObjects
|
|
26
|
+
* margelo::nitro::NitroAuth::registerNatives();
|
|
27
|
+
* // any other custom registrations go here.
|
|
28
|
+
* });
|
|
20
29
|
* }
|
|
21
30
|
* ```
|
|
22
31
|
*/
|
|
23
|
-
|
|
32
|
+
void registerAllNatives();
|
|
24
33
|
|
|
25
34
|
} // namespace margelo::nitro::NitroAuth
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-nitro-auth",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.5",
|
|
4
4
|
"description": "High-performance authentication library for React Native with Google Sign-In, Apple Sign-In, and Microsoft Sign-In support, powered by Nitro Modules (JSI)",
|
|
5
5
|
"main": "lib/commonjs/index.js",
|
|
6
6
|
"module": "lib/module/index.js",
|
|
@@ -81,21 +81,21 @@
|
|
|
81
81
|
"access": "public"
|
|
82
82
|
},
|
|
83
83
|
"devDependencies": {
|
|
84
|
-
"@expo/config-plugins": "^
|
|
85
|
-
"@react-native/babel-preset": "^0.
|
|
84
|
+
"@expo/config-plugins": "^55.0.6",
|
|
85
|
+
"@react-native/babel-preset": "^0.83.0",
|
|
86
86
|
"@testing-library/react": "^16.3.2",
|
|
87
87
|
"@types/node": "^22.19.11",
|
|
88
88
|
"jest-environment-jsdom": "^29.7.0",
|
|
89
|
-
"react": "19.
|
|
90
|
-
"react-native": "0.
|
|
91
|
-
"react-native-nitro-modules": "^0.
|
|
89
|
+
"react": "19.2.0",
|
|
90
|
+
"react-native": "0.83.2",
|
|
91
|
+
"react-native-nitro-modules": "^0.35.0",
|
|
92
92
|
"react-native-web": "^0.21.2",
|
|
93
93
|
"typescript": "^5.9.3"
|
|
94
94
|
},
|
|
95
95
|
"peerDependencies": {
|
|
96
96
|
"react": "*",
|
|
97
97
|
"react-native": ">=0.75.0",
|
|
98
|
-
"react-native-nitro-modules": ">=0.
|
|
98
|
+
"react-native-nitro-modules": ">=0.35.0"
|
|
99
99
|
},
|
|
100
100
|
"react-native-builder-bob": {
|
|
101
101
|
"source": "src",
|
package/src/index.ts
CHANGED
package/src/service.ts
CHANGED
|
@@ -6,8 +6,10 @@ import type {
|
|
|
6
6
|
LoginOptions,
|
|
7
7
|
AuthUser,
|
|
8
8
|
} from "./Auth.nitro";
|
|
9
|
+
import { AuthError } from "./utils/auth-error";
|
|
9
10
|
|
|
10
11
|
const nitroAuth = NitroModules.createHybridObject<Auth>("Auth");
|
|
12
|
+
|
|
11
13
|
export const AuthService: Auth = {
|
|
12
14
|
get name() {
|
|
13
15
|
return nitroAuth.name;
|
|
@@ -26,23 +28,43 @@ export const AuthService: Auth = {
|
|
|
26
28
|
},
|
|
27
29
|
|
|
28
30
|
async login(provider: AuthProvider, options?: LoginOptions) {
|
|
29
|
-
|
|
31
|
+
try {
|
|
32
|
+
return await nitroAuth.login(provider, options);
|
|
33
|
+
} catch (e) {
|
|
34
|
+
throw AuthError.from(e);
|
|
35
|
+
}
|
|
30
36
|
},
|
|
31
37
|
|
|
32
38
|
async requestScopes(scopes: string[]) {
|
|
33
|
-
|
|
39
|
+
try {
|
|
40
|
+
return await nitroAuth.requestScopes(scopes);
|
|
41
|
+
} catch (e) {
|
|
42
|
+
throw AuthError.from(e);
|
|
43
|
+
}
|
|
34
44
|
},
|
|
35
45
|
|
|
36
46
|
async revokeScopes(scopes: string[]) {
|
|
37
|
-
|
|
47
|
+
try {
|
|
48
|
+
return await nitroAuth.revokeScopes(scopes);
|
|
49
|
+
} catch (e) {
|
|
50
|
+
throw AuthError.from(e);
|
|
51
|
+
}
|
|
38
52
|
},
|
|
39
53
|
|
|
40
54
|
async getAccessToken() {
|
|
41
|
-
|
|
55
|
+
try {
|
|
56
|
+
return await nitroAuth.getAccessToken();
|
|
57
|
+
} catch (e) {
|
|
58
|
+
throw AuthError.from(e);
|
|
59
|
+
}
|
|
42
60
|
},
|
|
43
61
|
|
|
44
62
|
async refreshToken() {
|
|
45
|
-
|
|
63
|
+
try {
|
|
64
|
+
return await nitroAuth.refreshToken();
|
|
65
|
+
} catch (e) {
|
|
66
|
+
throw AuthError.from(e);
|
|
67
|
+
}
|
|
46
68
|
},
|
|
47
69
|
|
|
48
70
|
logout() {
|
|
@@ -50,7 +72,11 @@ export const AuthService: Auth = {
|
|
|
50
72
|
},
|
|
51
73
|
|
|
52
74
|
async silentRestore() {
|
|
53
|
-
|
|
75
|
+
try {
|
|
76
|
+
return await nitroAuth.silentRestore();
|
|
77
|
+
} catch (e) {
|
|
78
|
+
throw AuthError.from(e);
|
|
79
|
+
}
|
|
54
80
|
},
|
|
55
81
|
|
|
56
82
|
onAuthStateChanged(callback: (user: AuthUser | undefined) => void) {
|
package/src/use-auth.ts
CHANGED
|
@@ -6,38 +6,23 @@ import type {
|
|
|
6
6
|
AuthTokens,
|
|
7
7
|
} from "./Auth.nitro";
|
|
8
8
|
import { AuthService } from "./service";
|
|
9
|
+
import { AuthError } from "./utils/auth-error";
|
|
9
10
|
|
|
10
11
|
type AuthState = {
|
|
11
12
|
user: AuthUser | undefined;
|
|
12
13
|
scopes: string[];
|
|
13
14
|
loading: boolean;
|
|
14
|
-
error:
|
|
15
|
+
error: AuthError | undefined;
|
|
15
16
|
};
|
|
16
17
|
|
|
17
18
|
const areScopesEqual = (left: string[], right: string[]): boolean => {
|
|
18
|
-
if (left.length !== right.length)
|
|
19
|
-
|
|
19
|
+
if (left.length !== right.length) return false;
|
|
20
|
+
for (let i = 0; i < left.length; i += 1) {
|
|
21
|
+
if (left[i] !== right[i]) return false;
|
|
20
22
|
}
|
|
21
|
-
|
|
22
|
-
for (let index = 0; index < left.length; index += 1) {
|
|
23
|
-
if (left[index] !== right[index]) {
|
|
24
|
-
return false;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
23
|
return true;
|
|
29
24
|
};
|
|
30
25
|
|
|
31
|
-
class AuthHookError extends Error {
|
|
32
|
-
public readonly underlyingError?: string;
|
|
33
|
-
|
|
34
|
-
constructor(message: string, underlyingError?: string) {
|
|
35
|
-
super(message);
|
|
36
|
-
this.name = "AuthHookError";
|
|
37
|
-
this.underlyingError = underlyingError;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
26
|
export type UseAuthReturn = AuthState & {
|
|
42
27
|
hasPlayServices: boolean;
|
|
43
28
|
login: (provider: AuthProvider, options?: LoginOptions) => Promise<void>;
|
|
@@ -58,7 +43,7 @@ export function useAuth(): UseAuthReturn {
|
|
|
58
43
|
});
|
|
59
44
|
|
|
60
45
|
const syncStateFromService = useCallback(
|
|
61
|
-
(nextLoading: boolean, nextError:
|
|
46
|
+
(nextLoading: boolean, nextError: AuthError | undefined) => {
|
|
62
47
|
const nextUser = AuthService.currentUser;
|
|
63
48
|
const nextScopes = AuthService.grantedScopes;
|
|
64
49
|
setState((prev) => {
|
|
@@ -70,13 +55,7 @@ export function useAuth(): UseAuthReturn {
|
|
|
70
55
|
) {
|
|
71
56
|
return prev;
|
|
72
57
|
}
|
|
73
|
-
|
|
74
|
-
return {
|
|
75
|
-
user: nextUser,
|
|
76
|
-
scopes: nextScopes,
|
|
77
|
-
loading: nextLoading,
|
|
78
|
-
error: nextError,
|
|
79
|
-
};
|
|
58
|
+
return { user: nextUser, scopes: nextScopes, loading: nextLoading, error: nextError };
|
|
80
59
|
});
|
|
81
60
|
},
|
|
82
61
|
[],
|
|
@@ -89,12 +68,8 @@ export function useAuth(): UseAuthReturn {
|
|
|
89
68
|
await AuthService.login(provider, options);
|
|
90
69
|
syncStateFromService(false, undefined);
|
|
91
70
|
} catch (e) {
|
|
92
|
-
const error =
|
|
93
|
-
setState((prev) => ({
|
|
94
|
-
...prev,
|
|
95
|
-
loading: false,
|
|
96
|
-
error,
|
|
97
|
-
}));
|
|
71
|
+
const error = AuthError.from(e);
|
|
72
|
+
setState((prev) => ({ ...prev, loading: false, error }));
|
|
98
73
|
throw error;
|
|
99
74
|
}
|
|
100
75
|
},
|
|
@@ -103,12 +78,7 @@ export function useAuth(): UseAuthReturn {
|
|
|
103
78
|
|
|
104
79
|
const logout = useCallback(() => {
|
|
105
80
|
AuthService.logout();
|
|
106
|
-
setState({
|
|
107
|
-
user: undefined,
|
|
108
|
-
scopes: [],
|
|
109
|
-
loading: false,
|
|
110
|
-
error: undefined,
|
|
111
|
-
});
|
|
81
|
+
setState({ user: undefined, scopes: [], loading: false, error: undefined });
|
|
112
82
|
}, []);
|
|
113
83
|
|
|
114
84
|
const requestScopes = useCallback(
|
|
@@ -118,12 +88,8 @@ export function useAuth(): UseAuthReturn {
|
|
|
118
88
|
await AuthService.requestScopes(newScopes);
|
|
119
89
|
syncStateFromService(false, undefined);
|
|
120
90
|
} catch (e) {
|
|
121
|
-
const error =
|
|
122
|
-
setState((prev) => ({
|
|
123
|
-
...prev,
|
|
124
|
-
loading: false,
|
|
125
|
-
error,
|
|
126
|
-
}));
|
|
91
|
+
const error = AuthError.from(e);
|
|
92
|
+
setState((prev) => ({ ...prev, loading: false, error }));
|
|
127
93
|
throw error;
|
|
128
94
|
}
|
|
129
95
|
},
|
|
@@ -137,12 +103,8 @@ export function useAuth(): UseAuthReturn {
|
|
|
137
103
|
await AuthService.revokeScopes(scopesToRevoke);
|
|
138
104
|
syncStateFromService(false, undefined);
|
|
139
105
|
} catch (e) {
|
|
140
|
-
const error =
|
|
141
|
-
setState((prev) => ({
|
|
142
|
-
...prev,
|
|
143
|
-
loading: false,
|
|
144
|
-
error,
|
|
145
|
-
}));
|
|
106
|
+
const error = AuthError.from(e);
|
|
107
|
+
setState((prev) => ({ ...prev, loading: false, error }));
|
|
146
108
|
throw error;
|
|
147
109
|
}
|
|
148
110
|
},
|
|
@@ -158,17 +120,9 @@ export function useAuth(): UseAuthReturn {
|
|
|
158
120
|
syncStateFromService(false, undefined);
|
|
159
121
|
return tokens;
|
|
160
122
|
} catch (e) {
|
|
161
|
-
const
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
AuthService.currentUser?.underlyingError,
|
|
165
|
-
);
|
|
166
|
-
setState((prev) => ({
|
|
167
|
-
...prev,
|
|
168
|
-
loading: false,
|
|
169
|
-
error: authError,
|
|
170
|
-
}));
|
|
171
|
-
throw authError;
|
|
123
|
+
const error = AuthError.from(e);
|
|
124
|
+
setState((prev) => ({ ...prev, loading: false, error }));
|
|
125
|
+
throw error;
|
|
172
126
|
}
|
|
173
127
|
}, [syncStateFromService]);
|
|
174
128
|
|
|
@@ -178,12 +132,8 @@ export function useAuth(): UseAuthReturn {
|
|
|
178
132
|
await AuthService.silentRestore();
|
|
179
133
|
syncStateFromService(false, undefined);
|
|
180
134
|
} catch (e) {
|
|
181
|
-
const error =
|
|
182
|
-
setState((prev) => ({
|
|
183
|
-
...prev,
|
|
184
|
-
loading: false,
|
|
185
|
-
error,
|
|
186
|
-
}));
|
|
135
|
+
const error = AuthError.from(e);
|
|
136
|
+
setState((prev) => ({ ...prev, loading: false, error }));
|
|
187
137
|
throw error;
|
|
188
138
|
}
|
|
189
139
|
}, [syncStateFromService]);
|
|
@@ -199,13 +149,7 @@ export function useAuth(): UseAuthReturn {
|
|
|
199
149
|
) {
|
|
200
150
|
return prev;
|
|
201
151
|
}
|
|
202
|
-
|
|
203
|
-
return {
|
|
204
|
-
...prev,
|
|
205
|
-
user: currentUser,
|
|
206
|
-
scopes: nextScopes,
|
|
207
|
-
loading: false,
|
|
208
|
-
};
|
|
152
|
+
return { ...prev, user: currentUser, scopes: nextScopes, loading: false };
|
|
209
153
|
});
|
|
210
154
|
});
|
|
211
155
|
return unsubscribe;
|
|
@@ -223,15 +167,6 @@ export function useAuth(): UseAuthReturn {
|
|
|
223
167
|
refreshToken,
|
|
224
168
|
silentRestore,
|
|
225
169
|
}),
|
|
226
|
-
[
|
|
227
|
-
state,
|
|
228
|
-
login,
|
|
229
|
-
logout,
|
|
230
|
-
requestScopes,
|
|
231
|
-
revokeScopes,
|
|
232
|
-
getAccessToken,
|
|
233
|
-
refreshToken,
|
|
234
|
-
silentRestore,
|
|
235
|
-
],
|
|
170
|
+
[state, login, logout, requestScopes, revokeScopes, getAccessToken, refreshToken, silentRestore],
|
|
236
171
|
);
|
|
237
172
|
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { AuthErrorCode } from "../Auth.nitro";
|
|
2
|
+
|
|
3
|
+
const AUTH_ERROR_CODES: ReadonlySet<string> = new Set<AuthErrorCode>([
|
|
4
|
+
"cancelled",
|
|
5
|
+
"timeout",
|
|
6
|
+
"popup_blocked",
|
|
7
|
+
"network_error",
|
|
8
|
+
"configuration_error",
|
|
9
|
+
"unsupported_provider",
|
|
10
|
+
"invalid_state",
|
|
11
|
+
"invalid_nonce",
|
|
12
|
+
"token_error",
|
|
13
|
+
"no_id_token",
|
|
14
|
+
"parse_error",
|
|
15
|
+
"refresh_failed",
|
|
16
|
+
"unknown",
|
|
17
|
+
]);
|
|
18
|
+
|
|
19
|
+
export function isAuthErrorCode(value: string): value is AuthErrorCode {
|
|
20
|
+
return AUTH_ERROR_CODES.has(value);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function toAuthErrorCode(raw: string): AuthErrorCode {
|
|
24
|
+
return isAuthErrorCode(raw) ? raw : "unknown";
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Typed error thrown by all AuthService operations.
|
|
29
|
+
*
|
|
30
|
+
* - `code` — always a valid `AuthErrorCode`, safe to switch on
|
|
31
|
+
* - `underlyingMessage` — the raw platform message when it differs from `code`
|
|
32
|
+
*/
|
|
33
|
+
export class AuthError extends Error {
|
|
34
|
+
readonly code: AuthErrorCode;
|
|
35
|
+
readonly underlyingMessage: string | undefined;
|
|
36
|
+
|
|
37
|
+
constructor(raw: unknown) {
|
|
38
|
+
const message = raw instanceof Error ? raw.message : String(raw);
|
|
39
|
+
const code = toAuthErrorCode(message);
|
|
40
|
+
super(code);
|
|
41
|
+
this.name = "AuthError";
|
|
42
|
+
this.code = code;
|
|
43
|
+
this.underlyingMessage = code !== message ? message : undefined;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
static from(e: unknown): AuthError {
|
|
47
|
+
return e instanceof AuthError ? e : new AuthError(e);
|
|
48
|
+
}
|
|
49
|
+
}
|