react-redux-django-auth 1.4.5 โ 1.4.6
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 +77 -0
- package/dist/auth-core/actions/authActions.d.ts +57 -0
- package/dist/auth-core/actions/authActions.js +451 -0
- package/dist/auth-core/actions/types.d.ts +26 -0
- package/dist/auth-core/actions/types.js +22 -0
- package/dist/auth-core/apis/apis.d.ts +1 -0
- package/dist/auth-core/apis/apis.js +11 -0
- package/dist/auth-core/apis/config.d.ts +2 -0
- package/dist/auth-core/apis/config.js +10 -0
- package/dist/auth-core/hooks/useActivate.d.ts +9 -0
- package/dist/auth-core/hooks/useActivate.js +96 -0
- package/dist/auth-core/hooks/useChangePassword.d.ts +16 -0
- package/dist/auth-core/hooks/useChangePassword.js +121 -0
- package/dist/auth-core/hooks/useFacebookAuth.d.ts +11 -0
- package/dist/auth-core/hooks/useFacebookAuth.js +125 -0
- package/dist/auth-core/hooks/useGoogleAuth.d.ts +11 -0
- package/dist/auth-core/hooks/useGoogleAuth.js +125 -0
- package/dist/auth-core/hooks/useLoadUser.d.ts +12 -0
- package/dist/auth-core/hooks/useLoadUser.js +116 -0
- package/dist/auth-core/hooks/useLogin.d.ts +13 -0
- package/dist/auth-core/hooks/useLogin.js +119 -0
- package/dist/auth-core/hooks/useLogout.d.ts +3 -0
- package/dist/auth-core/hooks/useLogout.js +14 -0
- package/dist/auth-core/hooks/usePasswordConfirm.d.ts +17 -0
- package/dist/auth-core/hooks/usePasswordConfirm.js +117 -0
- package/dist/auth-core/hooks/useResetPassword.d.ts +11 -0
- package/dist/auth-core/hooks/useResetPassword.js +102 -0
- package/dist/auth-core/hooks/useSignup.d.ts +12 -0
- package/dist/auth-core/hooks/useSignup.js +115 -0
- package/dist/auth-core/reducers/authReducer.d.ts +10 -0
- package/dist/auth-core/reducers/authReducer.js +58 -0
- package/dist/auth-core/reducers/index.d.ts +7 -0
- package/dist/auth-core/reducers/index.js +6 -0
- package/dist/auth-core/setup/setupAuth.d.ts +4 -0
- package/dist/auth-core/setup/setupAuth.js +137 -0
- package/dist/auth-core/utils/ExtractErrorMsg.d.ts +33 -0
- package/dist/auth-core/utils/ExtractErrorMsg.js +38 -0
- package/dist/auth-native/AuthProvider.d.ts +7 -0
- package/dist/auth-native/AuthProvider.js +14 -0
- package/dist/auth-native/biometrics/biometricService.d.ts +4 -0
- package/dist/auth-native/biometrics/biometricService.js +74 -0
- package/dist/auth-native/hooks/useBiometricAuth.d.ts +9 -0
- package/dist/auth-native/hooks/useBiometricAuth.js +87 -0
- package/dist/auth-native/hooks/useGoogleAuth.d.ts +13 -0
- package/dist/auth-native/hooks/useGoogleAuth.js +145 -0
- package/dist/auth-native/index.d.ts +6 -0
- package/dist/auth-native/index.js +6 -0
- package/dist/auth-native/storage/nativeStorage.d.ts +18 -0
- package/dist/auth-native/storage/nativeStorage.js +111 -0
- package/dist/auth-native/store.d.ts +8 -0
- package/dist/auth-native/store.js +9 -0
- package/dist/auth-web/authProvider.d.ts +7 -0
- package/dist/auth-web/authProvider.js +14 -0
- package/dist/auth-web/hooks/useGoogleAuth.d.ts +11 -0
- package/dist/auth-web/hooks/useGoogleAuth.js +126 -0
- package/dist/auth-web/index.d.ts +6 -0
- package/dist/auth-web/index.js +6 -0
- package/dist/auth-web/oauth/google.d.ts +3 -0
- package/dist/auth-web/oauth/google.js +93 -0
- package/dist/auth-web/storage/webStorage.d.ts +5 -0
- package/dist/auth-web/storage/webStorage.js +13 -0
- package/dist/auth-web/store.d.ts +8 -0
- package/dist/auth-web/store.js +9 -0
- package/dist/index.d.mts +21 -140
- package/dist/index.d.ts +12 -150
- package/dist/index.js +12 -2114
- package/dist/index.mjs +72 -844
- package/package.json +42 -7
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
// import axios from 'axios';
|
|
2
|
+
// import { AuthStorage } from '../actions/types';
|
|
3
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
4
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
5
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
6
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
7
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
8
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
9
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
10
|
+
});
|
|
11
|
+
};
|
|
12
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
13
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
14
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
15
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
16
|
+
function step(op) {
|
|
17
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
18
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
19
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
20
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
21
|
+
switch (op[0]) {
|
|
22
|
+
case 0: case 1: t = op; break;
|
|
23
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
24
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
25
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
26
|
+
default:
|
|
27
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
28
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
29
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
30
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
31
|
+
if (t[2]) _.ops.pop();
|
|
32
|
+
_.trys.pop(); continue;
|
|
33
|
+
}
|
|
34
|
+
op = body.call(thisArg, _);
|
|
35
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
36
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
// export const setupAuth = (storage: AuthStorage) => {
|
|
40
|
+
// const initialize = async () => {
|
|
41
|
+
// const token = await storage.getAccessToken();
|
|
42
|
+
// if (token) {
|
|
43
|
+
// axios.defaults.headers.common['Authorization'] = `JWT ${token}`;
|
|
44
|
+
// }
|
|
45
|
+
// };
|
|
46
|
+
// // const setAuthToken = (token: string) => {
|
|
47
|
+
// // axios.defaults.headers.common['Authorization'] = `JWT ${token}`;
|
|
48
|
+
// // };
|
|
49
|
+
// // const clearAuthToken = () => {
|
|
50
|
+
// // delete axios.defaults.headers.common['Authorization'];
|
|
51
|
+
// // };
|
|
52
|
+
// return {
|
|
53
|
+
// initialize,
|
|
54
|
+
// // setAuthToken,
|
|
55
|
+
// // clearAuthToken,
|
|
56
|
+
// };
|
|
57
|
+
// };
|
|
58
|
+
import { createApi } from "../apis/apis";
|
|
59
|
+
import { getBaseURL } from "../apis/config";
|
|
60
|
+
export var setupAuth = function (storage) {
|
|
61
|
+
var isRefreshing = false;
|
|
62
|
+
var failedQueue = [];
|
|
63
|
+
var api = createApi(getBaseURL());
|
|
64
|
+
var processQueue = function (error, token) {
|
|
65
|
+
if (token === void 0) { token = null; }
|
|
66
|
+
failedQueue.forEach(function (prom) {
|
|
67
|
+
error ? prom.reject(error) : prom.resolve(token);
|
|
68
|
+
});
|
|
69
|
+
failedQueue = [];
|
|
70
|
+
};
|
|
71
|
+
var initialize = function () {
|
|
72
|
+
api.interceptors.request.use(function (config) { return __awaiter(void 0, void 0, void 0, function () {
|
|
73
|
+
var token;
|
|
74
|
+
return __generator(this, function (_a) {
|
|
75
|
+
switch (_a.label) {
|
|
76
|
+
case 0: return [4 /*yield*/, storage.getAccessToken()];
|
|
77
|
+
case 1:
|
|
78
|
+
token = _a.sent();
|
|
79
|
+
if (token)
|
|
80
|
+
config.headers.Authorization = "JWT ".concat(token);
|
|
81
|
+
return [2 /*return*/, config];
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
}); });
|
|
85
|
+
api.interceptors.response.use(function (res) { return res; }, function (error) { return __awaiter(void 0, void 0, void 0, function () {
|
|
86
|
+
var originalRequest, refresh, res, newAccess, err_1;
|
|
87
|
+
var _a, _b;
|
|
88
|
+
return __generator(this, function (_c) {
|
|
89
|
+
switch (_c.label) {
|
|
90
|
+
case 0:
|
|
91
|
+
originalRequest = error.config;
|
|
92
|
+
if (!(((_a = error.response) === null || _a === void 0 ? void 0 : _a.status) === 401 && !originalRequest._retry)) return [3 /*break*/, 8];
|
|
93
|
+
if (isRefreshing) {
|
|
94
|
+
return [2 /*return*/, new Promise(function (resolve, reject) {
|
|
95
|
+
failedQueue.push({ resolve: resolve, reject: reject });
|
|
96
|
+
}).then(function (token) {
|
|
97
|
+
originalRequest.headers.Authorization = "JWT ".concat(token);
|
|
98
|
+
return api(originalRequest);
|
|
99
|
+
})];
|
|
100
|
+
}
|
|
101
|
+
originalRequest._retry = true;
|
|
102
|
+
isRefreshing = true;
|
|
103
|
+
_c.label = 1;
|
|
104
|
+
case 1:
|
|
105
|
+
_c.trys.push([1, 5, 7, 8]);
|
|
106
|
+
return [4 /*yield*/, ((_b = storage.getRefreshToken) === null || _b === void 0 ? void 0 : _b.call(storage))];
|
|
107
|
+
case 2:
|
|
108
|
+
refresh = _c.sent();
|
|
109
|
+
if (!refresh)
|
|
110
|
+
throw new Error("NO_REFRESH_TOKEN");
|
|
111
|
+
return [4 /*yield*/, api.post("/auth/jwt/refresh/", { refresh: refresh })];
|
|
112
|
+
case 3:
|
|
113
|
+
res = _c.sent();
|
|
114
|
+
newAccess = res.data.access;
|
|
115
|
+
return [4 /*yield*/, storage.setTokens(newAccess, refresh)];
|
|
116
|
+
case 4:
|
|
117
|
+
_c.sent();
|
|
118
|
+
processQueue(null, newAccess);
|
|
119
|
+
originalRequest.headers.Authorization = "JWT ".concat(newAccess);
|
|
120
|
+
return [2 /*return*/, api(originalRequest)];
|
|
121
|
+
case 5:
|
|
122
|
+
err_1 = _c.sent();
|
|
123
|
+
processQueue(err_1, null);
|
|
124
|
+
return [4 /*yield*/, storage.clearTokens()];
|
|
125
|
+
case 6:
|
|
126
|
+
_c.sent();
|
|
127
|
+
return [2 /*return*/, Promise.reject(err_1)];
|
|
128
|
+
case 7:
|
|
129
|
+
isRefreshing = false;
|
|
130
|
+
return [7 /*endfinally*/];
|
|
131
|
+
case 8: return [2 /*return*/, Promise.reject(error)];
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
}); });
|
|
135
|
+
};
|
|
136
|
+
return { initialize: initialize };
|
|
137
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A type representing a single error message string.
|
|
3
|
+
*/
|
|
4
|
+
type ErrorMessage = string;
|
|
5
|
+
/**
|
|
6
|
+
* A type representing an array of error messages.
|
|
7
|
+
*/
|
|
8
|
+
type ErrorMessageArray = string[];
|
|
9
|
+
/**
|
|
10
|
+
* A type representing an object where keys map to either a single error message
|
|
11
|
+
* or an array of error messages.
|
|
12
|
+
*/
|
|
13
|
+
type ErrorMessageObject = {
|
|
14
|
+
[key: string]: ErrorMessage | ErrorMessageArray;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* A union type for all possible error message inputs.
|
|
18
|
+
*/
|
|
19
|
+
type ErrorMessageType = ErrorMessage | ErrorMessageArray | ErrorMessageObject;
|
|
20
|
+
/**
|
|
21
|
+
* Extracts and flattens various error message formats into a single array of strings.
|
|
22
|
+
*
|
|
23
|
+
* This function can handle:
|
|
24
|
+
* - A single string: Returns an array with that string.
|
|
25
|
+
* - An array of strings: Returns the array as is.
|
|
26
|
+
* - An object: Iterates through the object's values and extracts all strings
|
|
27
|
+
* and strings from arrays into a single flattened array.
|
|
28
|
+
*
|
|
29
|
+
* @param errorMsg The error message(s) to extract, which can be a string, array, or object.
|
|
30
|
+
* @returns A flattened array of error message strings.
|
|
31
|
+
*/
|
|
32
|
+
export declare const extractErrorMessages: (errorMsg: ErrorMessageType) => string[];
|
|
33
|
+
export {};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Extracts and flattens various error message formats into a single array of strings.
|
|
3
|
+
*
|
|
4
|
+
* This function can handle:
|
|
5
|
+
* - A single string: Returns an array with that string.
|
|
6
|
+
* - An array of strings: Returns the array as is.
|
|
7
|
+
* - An object: Iterates through the object's values and extracts all strings
|
|
8
|
+
* and strings from arrays into a single flattened array.
|
|
9
|
+
*
|
|
10
|
+
* @param errorMsg The error message(s) to extract, which can be a string, array, or object.
|
|
11
|
+
* @returns A flattened array of error message strings.
|
|
12
|
+
*/
|
|
13
|
+
export var extractErrorMessages = function (errorMsg) {
|
|
14
|
+
// If the error message is already a string, wrap it in an array and return it.
|
|
15
|
+
if (typeof errorMsg === "string") {
|
|
16
|
+
return [errorMsg];
|
|
17
|
+
}
|
|
18
|
+
// If the error message is already an array of strings, return it directly.
|
|
19
|
+
if (Array.isArray(errorMsg)) {
|
|
20
|
+
return errorMsg;
|
|
21
|
+
}
|
|
22
|
+
// If the error message is an object, iterate through its keys to extract messages.
|
|
23
|
+
if (typeof errorMsg === "object" && errorMsg !== null) {
|
|
24
|
+
var extractedMessages_1 = [];
|
|
25
|
+
Object.keys(errorMsg).forEach(function (key) {
|
|
26
|
+
var value = errorMsg[key];
|
|
27
|
+
if (Array.isArray(value)) {
|
|
28
|
+
extractedMessages_1.push.apply(extractedMessages_1, value);
|
|
29
|
+
}
|
|
30
|
+
else if (typeof value === "string") {
|
|
31
|
+
extractedMessages_1.push(value);
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
return extractedMessages_1;
|
|
35
|
+
}
|
|
36
|
+
// For any other type, return an empty array.
|
|
37
|
+
return [];
|
|
38
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import React, { useEffect } from "react";
|
|
2
|
+
import { Provider } from "react-redux";
|
|
3
|
+
import store from "./store";
|
|
4
|
+
import { setBaseURL } from "../auth-core/apis/config";
|
|
5
|
+
import { auth } from "./index";
|
|
6
|
+
var AuthProviderNative = function (_a) {
|
|
7
|
+
var children = _a.children, apiUrl = _a.apiUrl;
|
|
8
|
+
useEffect(function () {
|
|
9
|
+
setBaseURL(apiUrl);
|
|
10
|
+
auth.initialize(); // ๐ฅ attach token to axios on app load
|
|
11
|
+
}, [apiUrl]);
|
|
12
|
+
return React.createElement(Provider, { store: store }, children);
|
|
13
|
+
};
|
|
14
|
+
export default AuthProviderNative;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
11
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
12
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
13
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
14
|
+
function step(op) {
|
|
15
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
16
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
17
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
18
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
19
|
+
switch (op[0]) {
|
|
20
|
+
case 0: case 1: t = op; break;
|
|
21
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
22
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
23
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
24
|
+
default:
|
|
25
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
26
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
27
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
28
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
29
|
+
if (t[2]) _.ops.pop();
|
|
30
|
+
_.trys.pop(); continue;
|
|
31
|
+
}
|
|
32
|
+
op = body.call(thisArg, _);
|
|
33
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
34
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
import * as LocalAuthentication from "expo-local-authentication";
|
|
38
|
+
export var biometricService = {
|
|
39
|
+
isAvailable: function () {
|
|
40
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
41
|
+
var hasHardware, isEnrolled;
|
|
42
|
+
return __generator(this, function (_a) {
|
|
43
|
+
switch (_a.label) {
|
|
44
|
+
case 0: return [4 /*yield*/, LocalAuthentication.hasHardwareAsync()];
|
|
45
|
+
case 1:
|
|
46
|
+
hasHardware = _a.sent();
|
|
47
|
+
return [4 /*yield*/, LocalAuthentication.isEnrolledAsync()];
|
|
48
|
+
case 2:
|
|
49
|
+
isEnrolled = _a.sent();
|
|
50
|
+
return [2 /*return*/, hasHardware && isEnrolled];
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
},
|
|
55
|
+
authenticate: function () {
|
|
56
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
57
|
+
var result;
|
|
58
|
+
return __generator(this, function (_a) {
|
|
59
|
+
switch (_a.label) {
|
|
60
|
+
case 0: return [4 /*yield*/, LocalAuthentication.authenticateAsync({
|
|
61
|
+
promptMessage: "Authenticate to continue",
|
|
62
|
+
fallbackLabel: "Use Passcode",
|
|
63
|
+
})];
|
|
64
|
+
case 1:
|
|
65
|
+
result = _a.sent();
|
|
66
|
+
if (!result.success) {
|
|
67
|
+
throw new Error("BIOMETRIC_FAILED");
|
|
68
|
+
}
|
|
69
|
+
return [2 /*return*/, true];
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
},
|
|
74
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { AuthStorage } from "../../auth-core/actions/types";
|
|
2
|
+
interface Props {
|
|
3
|
+
storage: AuthStorage;
|
|
4
|
+
}
|
|
5
|
+
declare const useBiometricAuth: ({ storage }: Props) => {
|
|
6
|
+
authenticate: () => Promise<any>;
|
|
7
|
+
loading: boolean;
|
|
8
|
+
};
|
|
9
|
+
export default useBiometricAuth;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
11
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
12
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
13
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
14
|
+
function step(op) {
|
|
15
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
16
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
17
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
18
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
19
|
+
switch (op[0]) {
|
|
20
|
+
case 0: case 1: t = op; break;
|
|
21
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
22
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
23
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
24
|
+
default:
|
|
25
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
26
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
27
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
28
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
29
|
+
if (t[2]) _.ops.pop();
|
|
30
|
+
_.trys.pop(); continue;
|
|
31
|
+
}
|
|
32
|
+
op = body.call(thisArg, _);
|
|
33
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
34
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
import { useState, useCallback } from "react";
|
|
38
|
+
import { useDispatch } from "react-redux";
|
|
39
|
+
import { biometricService } from "../biometrics/biometricService";
|
|
40
|
+
import { refreshAccessToken } from "../../auth-core/actions/authActions";
|
|
41
|
+
var useBiometricAuth = function (_a) {
|
|
42
|
+
var storage = _a.storage;
|
|
43
|
+
var _b = useState(false), loading = _b[0], setLoading = _b[1];
|
|
44
|
+
var dispatch = useDispatch();
|
|
45
|
+
var authenticate = useCallback(function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
46
|
+
var available, access, newAccess, err_1;
|
|
47
|
+
return __generator(this, function (_a) {
|
|
48
|
+
switch (_a.label) {
|
|
49
|
+
case 0:
|
|
50
|
+
setLoading(true);
|
|
51
|
+
_a.label = 1;
|
|
52
|
+
case 1:
|
|
53
|
+
_a.trys.push([1, 6, 7, 8]);
|
|
54
|
+
return [4 /*yield*/, biometricService.isAvailable()];
|
|
55
|
+
case 2:
|
|
56
|
+
available = _a.sent();
|
|
57
|
+
if (!available)
|
|
58
|
+
throw new Error("BIOMETRIC_NOT_AVAILABLE");
|
|
59
|
+
// Prompt user for biometric
|
|
60
|
+
return [4 /*yield*/, biometricService.authenticate()];
|
|
61
|
+
case 3:
|
|
62
|
+
// Prompt user for biometric
|
|
63
|
+
_a.sent();
|
|
64
|
+
return [4 /*yield*/, storage.getAccessToken()];
|
|
65
|
+
case 4:
|
|
66
|
+
access = _a.sent();
|
|
67
|
+
if (access)
|
|
68
|
+
return [2 /*return*/, access];
|
|
69
|
+
return [4 /*yield*/, dispatch(refreshAccessToken(storage))];
|
|
70
|
+
case 5:
|
|
71
|
+
newAccess = _a.sent();
|
|
72
|
+
if (!newAccess)
|
|
73
|
+
throw new Error("REFRESH_FAILED: please login");
|
|
74
|
+
return [2 /*return*/, newAccess];
|
|
75
|
+
case 6:
|
|
76
|
+
err_1 = _a.sent();
|
|
77
|
+
throw err_1;
|
|
78
|
+
case 7:
|
|
79
|
+
setLoading(false);
|
|
80
|
+
return [7 /*endfinally*/];
|
|
81
|
+
case 8: return [2 /*return*/];
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
}); }, [storage, dispatch]);
|
|
85
|
+
return { authenticate: authenticate, loading: loading };
|
|
86
|
+
};
|
|
87
|
+
export default useBiometricAuth;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { AuthStorage } from "../../auth-core/actions/types";
|
|
2
|
+
interface UseGoogleAuthProps {
|
|
3
|
+
storage: AuthStorage;
|
|
4
|
+
}
|
|
5
|
+
declare const useGoogleAuthNative: ({ storage }: UseGoogleAuthProps) => {
|
|
6
|
+
loginWithGoogle: (payload?: Record<string, any>) => Promise<{
|
|
7
|
+
user: any;
|
|
8
|
+
tokens: any;
|
|
9
|
+
}>;
|
|
10
|
+
loading: boolean;
|
|
11
|
+
error: any;
|
|
12
|
+
};
|
|
13
|
+
export default useGoogleAuthNative;
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
var __assign = (this && this.__assign) || function () {
|
|
2
|
+
__assign = Object.assign || function(t) {
|
|
3
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
+
s = arguments[i];
|
|
5
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
+
t[p] = s[p];
|
|
7
|
+
}
|
|
8
|
+
return t;
|
|
9
|
+
};
|
|
10
|
+
return __assign.apply(this, arguments);
|
|
11
|
+
};
|
|
12
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
13
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
14
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
15
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
16
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
17
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
18
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
22
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
23
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
24
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
25
|
+
function step(op) {
|
|
26
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
27
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
28
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
29
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
30
|
+
switch (op[0]) {
|
|
31
|
+
case 0: case 1: t = op; break;
|
|
32
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
33
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
34
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
35
|
+
default:
|
|
36
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
37
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
38
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
39
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
40
|
+
if (t[2]) _.ops.pop();
|
|
41
|
+
_.trys.pop(); continue;
|
|
42
|
+
}
|
|
43
|
+
op = body.call(thisArg, _);
|
|
44
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
45
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
import { useState, useCallback } from "react";
|
|
49
|
+
import { GoogleSignin, statusCodes, } from "@react-native-google-signin/google-signin";
|
|
50
|
+
import { useDispatch } from "react-redux";
|
|
51
|
+
import axios from "axios";
|
|
52
|
+
import { getBaseURL } from "../../auth-core/apis/config";
|
|
53
|
+
var useGoogleAuthNative = function (_a) {
|
|
54
|
+
var storage = _a.storage;
|
|
55
|
+
var dispatch = useDispatch();
|
|
56
|
+
var _b = useState(false), loading = _b[0], setLoading = _b[1];
|
|
57
|
+
var _c = useState(null), error = _c[0], setError = _c[1];
|
|
58
|
+
var signInWithGoogle = function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
59
|
+
var userInfo, idToken, err_1;
|
|
60
|
+
var _a;
|
|
61
|
+
return __generator(this, function (_b) {
|
|
62
|
+
switch (_b.label) {
|
|
63
|
+
case 0:
|
|
64
|
+
_b.trys.push([0, 3, , 4]);
|
|
65
|
+
return [4 /*yield*/, GoogleSignin.hasPlayServices()];
|
|
66
|
+
case 1:
|
|
67
|
+
_b.sent();
|
|
68
|
+
return [4 /*yield*/, GoogleSignin.signIn()];
|
|
69
|
+
case 2:
|
|
70
|
+
userInfo = _b.sent();
|
|
71
|
+
idToken = (_a = userInfo === null || userInfo === void 0 ? void 0 : userInfo.data) === null || _a === void 0 ? void 0 : _a.idToken;
|
|
72
|
+
if (!idToken)
|
|
73
|
+
throw new Error("NO_ID_TOKEN");
|
|
74
|
+
return [2 /*return*/, idToken];
|
|
75
|
+
case 3:
|
|
76
|
+
err_1 = _b.sent();
|
|
77
|
+
if (err_1.code === statusCodes.SIGN_IN_CANCELLED) {
|
|
78
|
+
throw new Error("SIGN_IN_CANCELLED");
|
|
79
|
+
}
|
|
80
|
+
if (err_1.code === statusCodes.IN_PROGRESS) {
|
|
81
|
+
throw new Error("IN_PROGRESS");
|
|
82
|
+
}
|
|
83
|
+
if (err_1.code === statusCodes.PLAY_SERVICES_NOT_AVAILABLE) {
|
|
84
|
+
throw new Error("PLAY_SERVICES_NOT_AVAILABLE");
|
|
85
|
+
}
|
|
86
|
+
throw new Error("GOOGLE_SIGN_IN_FAILED");
|
|
87
|
+
case 4: return [2 /*return*/];
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
}); };
|
|
91
|
+
var loginWithGoogle = useCallback(function (payload) { return __awaiter(void 0, void 0, void 0, function () {
|
|
92
|
+
var idToken, res, _a, user, tokens, err_2;
|
|
93
|
+
var _b, _c, _d;
|
|
94
|
+
return __generator(this, function (_e) {
|
|
95
|
+
switch (_e.label) {
|
|
96
|
+
case 0:
|
|
97
|
+
setLoading(true);
|
|
98
|
+
setError(null);
|
|
99
|
+
_e.label = 1;
|
|
100
|
+
case 1:
|
|
101
|
+
_e.trys.push([1, 5, 6, 7]);
|
|
102
|
+
return [4 /*yield*/, signInWithGoogle()];
|
|
103
|
+
case 2:
|
|
104
|
+
idToken = _e.sent();
|
|
105
|
+
return [4 /*yield*/, axios.post("".concat(getBaseURL(), "/auth/o/google-oauth2/"), __assign({ access_token: idToken }, payload))];
|
|
106
|
+
case 3:
|
|
107
|
+
res = _e.sent();
|
|
108
|
+
_a = ((_b = res.data) === null || _b === void 0 ? void 0 : _b.data) || {}, user = _a.user, tokens = _a.tokens;
|
|
109
|
+
if (!(tokens === null || tokens === void 0 ? void 0 : tokens.access) || !user) {
|
|
110
|
+
throw new Error("INVALID_RESPONSE");
|
|
111
|
+
}
|
|
112
|
+
return [4 /*yield*/, storage.setTokens(tokens.access, tokens.refresh)];
|
|
113
|
+
case 4:
|
|
114
|
+
_e.sent();
|
|
115
|
+
dispatch({
|
|
116
|
+
type: "GOOGLE_AUTH_SUCCESS",
|
|
117
|
+
payload: {
|
|
118
|
+
access: tokens.access,
|
|
119
|
+
refresh: tokens.refresh,
|
|
120
|
+
status: res.status,
|
|
121
|
+
},
|
|
122
|
+
});
|
|
123
|
+
return [2 /*return*/, { user: user, tokens: tokens }];
|
|
124
|
+
case 5:
|
|
125
|
+
err_2 = _e.sent();
|
|
126
|
+
setError(((_c = err_2 === null || err_2 === void 0 ? void 0 : err_2.response) === null || _c === void 0 ? void 0 : _c.data) || err_2.message);
|
|
127
|
+
dispatch({
|
|
128
|
+
type: "GOOGLE_AUTH_FAIL",
|
|
129
|
+
payload: (_d = err_2 === null || err_2 === void 0 ? void 0 : err_2.response) === null || _d === void 0 ? void 0 : _d.data,
|
|
130
|
+
});
|
|
131
|
+
throw err_2;
|
|
132
|
+
case 6:
|
|
133
|
+
setLoading(false);
|
|
134
|
+
return [7 /*endfinally*/];
|
|
135
|
+
case 7: return [2 /*return*/];
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
}); }, [storage, dispatch]);
|
|
139
|
+
return {
|
|
140
|
+
loginWithGoogle: loginWithGoogle,
|
|
141
|
+
loading: loading,
|
|
142
|
+
error: error,
|
|
143
|
+
};
|
|
144
|
+
};
|
|
145
|
+
export default useGoogleAuthNative;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { setupAuth } from '../auth-core/setup/setupAuth';
|
|
2
|
+
import { storage } from './storage/nativeStorage';
|
|
3
|
+
export var auth = setupAuth(storage);
|
|
4
|
+
export { default as AuthProvider } from './AuthProvider';
|
|
5
|
+
export { default as store } from './store';
|
|
6
|
+
export { default as useGoogleAuthNative } from './hooks/useGoogleAuth';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export declare const storage: {
|
|
2
|
+
/**
|
|
3
|
+
* ๐ Access token (fast access)
|
|
4
|
+
*/
|
|
5
|
+
getAccessToken: () => Promise<string | null>;
|
|
6
|
+
/**
|
|
7
|
+
* ๐ Refresh token (SECURED with biometrics)
|
|
8
|
+
*/
|
|
9
|
+
getRefreshToken: () => Promise<string | null>;
|
|
10
|
+
/**
|
|
11
|
+
* ๐พ Save tokens
|
|
12
|
+
*/
|
|
13
|
+
setTokens: (access: string, refresh?: string) => Promise<void>;
|
|
14
|
+
/**
|
|
15
|
+
* ๐งน Clear tokens
|
|
16
|
+
*/
|
|
17
|
+
clearTokens: () => Promise<void>;
|
|
18
|
+
};
|