prostgles-client
Version:
Reactive client for Postgres
123 lines (122 loc) • 5.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.authRequest = exports.getAuthHandler = void 0;
const prostgles_types_1 = require("prostgles-types");
const prostgles_1 = require("./prostgles");
const getAuthHandler = ({ authData: authConfig, socket, onReload, endpoint, ...authOpts }) => {
var _a;
const urlWithEndpointAndSearch = (route) => {
const { search } = window.location;
let url = route + search;
if (endpoint)
url = `${endpoint}${url}`;
return url;
};
if ((authConfig === null || authConfig === void 0 ? void 0 : authConfig.pathGuard) && prostgles_1.isClientSide) {
const doReload = (res) => {
if (res === null || res === void 0 ? void 0 : res.shouldReload) {
if (onReload)
onReload();
else if (prostgles_1.isClientSide) {
console.log("prostgles page reload due to authguard", res);
setTimeout(() => {
window.location.reload();
}, 200);
}
}
};
socket.emit(prostgles_types_1.CHANNELS.AUTHGUARD, JSON.stringify(window.location), (_err, res) => {
doReload(res);
});
socket.removeAllListeners(prostgles_types_1.CHANNELS.AUTHGUARD);
socket.on(prostgles_types_1.CHANNELS.AUTHGUARD, (res) => {
doReload(res);
});
}
const loginSignupOptions = {
loginType: (_a = authConfig === null || authConfig === void 0 ? void 0 : authConfig.login) === null || _a === void 0 ? void 0 : _a.mode,
login: undefined,
preferredLogin: authConfig === null || authConfig === void 0 ? void 0 : authConfig.preferredLogin,
loginWithProvider: undefined,
signupWithEmailAndPassword: undefined,
confirmEmail: undefined,
providers: authConfig === null || authConfig === void 0 ? void 0 : authConfig.providers,
};
if (authConfig) {
const { providers, signupWithEmailAndPassword, login } = authConfig;
loginSignupOptions.loginWithProvider =
(0, prostgles_types_1.isEmpty)(providers) ? undefined : (providers &&
Object.entries(providers).reduce((acc, [provider, { url }]) => {
acc[provider] = () => {
window.location.assign(url);
};
return acc;
}, {}));
loginSignupOptions.login =
login &&
(async (params) => {
return (0, exports.authRequest)(urlWithEndpointAndSearch(login.loginRoute), params, "POST", authOpts);
});
loginSignupOptions.signupWithEmailAndPassword =
signupWithEmailAndPassword &&
((params) => {
return (0, exports.authRequest)(urlWithEndpointAndSearch(signupWithEmailAndPassword.url), params, "POST", authOpts);
});
loginSignupOptions.confirmEmail =
signupWithEmailAndPassword &&
((data) => {
return (0, exports.authRequest)(urlWithEndpointAndSearch(signupWithEmailAndPassword.emailConfirmationRoute), data, "POST", authOpts);
});
}
if (!(authConfig === null || authConfig === void 0 ? void 0 : authConfig.user)) {
return {
isLoggedin: false,
user: undefined,
...loginSignupOptions,
};
}
return {
isLoggedin: true,
user: authConfig.user,
logout: async () => {
var _a;
const { logoutRoute } = (_a = authConfig.login) !== null && _a !== void 0 ? _a : {};
if (!logoutRoute)
throw new Error("Unexpected");
return (0, exports.authRequest)(urlWithEndpointAndSearch(logoutRoute), {}, "POST", authOpts);
},
...loginSignupOptions,
};
};
exports.getAuthHandler = getAuthHandler;
const authRequest = async (path, data, method, { credentials, redirect }) => {
const rawResponse = await fetch(path, {
method: method,
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
...(method !== "GET" && { body: JSON.stringify(data) }),
credentials,
redirect,
});
if (!rawResponse.ok) {
const error = await rawResponse
.json()
.catch(() => rawResponse.text())
.catch(() => rawResponse.statusText);
if (typeof error === "string") {
return { success: false, code: "something-went-wrong", message: error };
}
return error;
}
const responseObject = await rawResponse
.json()
.catch(async () => ({ message: await rawResponse.text() }))
.catch(() => ({ message: rawResponse.statusText }));
if (rawResponse.redirected) {
return { ...responseObject, success: true, redirect_url: rawResponse.url };
}
return responseObject;
};
exports.authRequest = authRequest;