@userfrosting/sprinkle-account
Version:
Account Sprinkle for UserFrosting
480 lines (479 loc) • 12.1 kB
JavaScript
import { ref as s } from "vue";
import m from "axios";
import { a as p, c as N } from "./useAxiosInterceptor-DiL_D9c_.js";
import { u as X } from "./useAxiosInterceptor-DiL_D9c_.js";
import { Severity as d } from "@userfrosting/sprinkle-core/interfaces";
import { useConfigStore as v, useAlertsStore as E, useTranslator as w } from "@userfrosting/sprinkle-core/stores";
import { useRuleSchemaAdapter as f, useCsrf as R } from "@userfrosting/sprinkle-core/composables";
import { u as D } from "./useAuthStore-DRHqdc4w.js";
import { a as Z } from "./useAuthStore-DRHqdc4w.js";
const b = {
user_name: { validators: {
length: {
label: "&USERNAME",
min: 1,
max: 50,
message: "VALIDATE.LENGTH_RANGE"
},
no_leading_whitespace: {
label: "&USERNAME",
message: "VALIDATE.NO_LEAD_WS"
},
no_trailing_whitespace: {
label: "&USERNAME",
message: "VALIDATE.NO_TRAIL_WS"
},
required: {
label: "&USERNAME",
message: "VALIDATE.REQUIRED"
},
username: {
label: "&USERNAME",
message: "VALIDATE.USERNAME"
}
} },
first_name: { validators: {
length: {
label: "&FIRST_NAME",
min: 1,
max: 20,
message: "VALIDATE.LENGTH_RANGE"
},
required: {
label: "&FIRST_NAME",
message: "VALIDATE.REQUIRED"
}
} },
last_name: { validators: { length: {
label: "&LAST_NAME",
min: 1,
max: 30,
message: "VALIDATE.LENGTH_RANGE"
} } },
email: { validators: {
required: {
label: "&EMAIL",
message: "VALIDATE.REQUIRED"
},
length: {
label: "&EMAIL",
min: 1,
max: 150,
message: "VALIDATE.LENGTH_RANGE"
},
email: { message: "VALIDATE.INVALID_EMAIL" }
} },
password: { validators: {
required: {
label: "&PASSWORD",
message: "VALIDATE.REQUIRED"
},
length: {
label: "&PASSWORD",
min: 12,
max: 100,
message: "VALIDATE.LENGTH_RANGE"
}
} },
passwordc: { validators: {
required: {
label: "&PASSWORD.CONFIRM",
message: "VALIDATE.REQUIRED"
},
matches: {
field: "password",
label: "&PASSWORD.CONFIRM",
message: "VALIDATE.PASSWORD_MISMATCH"
},
length: {
label: "&PASSWORD.CONFIRM",
min: 12,
max: 100,
message: "VALIDATE.LENGTH_RANGE"
}
} },
locale: { validators: { required: {
label: "&LOCALE",
domain: "server",
message: "VALIDATE.REQUIRED"
} } },
captcha: { validators: null }
};
function F() {
const a = s(!1), e = s(null), o = s(0), n = s(0), l = s(g()), r = v();
o.value = r.get("site.password.length.min"), n.value = r.get("site.password.length.max");
const { r$: t } = p(l, f().adapt(b)), i = N({
async validator(u) {
const c = await h(u);
return { $valid: c.available, $message: c.message };
},
message: (u) => u.$message
}), { r$: A } = p(l, {
user_name: { usernameRule: i }
});
function g() {
return {
first_name: "",
last_name: "",
email: "",
user_name: "",
password: "",
passwordc: "",
locale: v().get("site.registration.user_defaults.locale", "en_US"),
captcha: "",
spiderbro: "http://"
};
}
function I() {
return v().get("locales.available");
}
function S() {
return "/account/captcha";
}
async function T(u) {
return a.value = !0, e.value = null, m.post("/account/register", u).then((c) => {
E().push({
title: c.data.title,
description: c.data.description,
style: d.Success
});
}).catch((c) => {
var L;
throw e.value = {
...((L = c.response) == null ? void 0 : L.data) ?? { description: c.message },
style: d.Danger
}, e.value;
}).finally(() => {
a.value = !1;
});
}
async function _() {
return a.value = !0, e.value = null, m.get("/account/suggest-username").then((u) => u.data.user_name).catch((u) => {
var c;
throw e.value = {
...((c = u.response) == null ? void 0 : c.data) ?? { description: u.message },
style: d.Danger
}, e.value;
}).finally(() => {
a.value = !1;
});
}
async function h(u) {
return m.get("/account/check-username", {
params: {
user_name: u
}
}).then((c) => c.data);
}
return {
submitRegistration: T,
defaultRegistrationForm: g,
availableLocales: I,
suggestUsername: _,
validateUsername: h,
captchaUrl: S,
formData: l,
apiLoading: a,
apiError: e,
r$: t,
r$username: A,
passwordMinLength: o,
passwordMaxLength: n
};
}
const y = {
user_name: { validators: {
required: {
label: "&USERNAME",
message: "VALIDATE.REQUIRED"
},
no_leading_whitespace: {
label: "&USERNAME",
message: "VALIDATE.NO_LEAD_WS"
},
no_trailing_whitespace: {
label: "&USERNAME",
message: "VALIDATE.NO_TRAIL_WS"
}
} },
password: { validators: { required: {
label: "&PASSWORD",
message: "VALIDATE.REQUIRED"
} } },
rememberme: { default: !1 }
};
function H() {
const a = s(!1), e = s(null), o = s(l()), { r$: n } = p(o, f().adapt(y));
function l() {
return {
user_name: "",
password: "",
rememberme: !1
};
}
async function r(t) {
return a.value = !0, e.value = null, m.post("/account/login", t).then((i) => {
E().push({
title: i.data.message,
style: d.Success
}), D().setUser(i.data.user), R().updateFromHeaders(i.headers);
}).catch((i) => {
var A;
throw e.value = {
...((A = i.response) == null ? void 0 : A.data) ?? { description: i.message },
style: d.Danger
}, e.value;
}).finally(() => {
a.value = !1;
});
}
return {
submitLogin: r,
defaultFormData: l,
apiLoading: a,
apiError: e,
formData: o,
r$: n
};
}
function $() {
const a = s(!1), e = s(null);
async function o() {
return a.value = !0, e.value = null, m.get("/account/logout").then((n) => {
E().push({
title: n.data.title,
description: n.data.description,
style: d.Success
}), D().unsetUser(), R().updateFromHeaders(n.headers);
}).catch((n) => {
var l;
throw e.value = {
...((l = n.response) == null ? void 0 : l.data) ?? { description: n.message },
style: d.Danger
}, e.value;
}).finally(() => {
a.value = !1;
});
}
return {
submitLogout: o,
apiLoading: a,
apiError: e
};
}
const M = {
first_name: { validators: {
length: {
label: "&FIRST_NAME",
min: 1,
max: 20,
message: "VALIDATE.LENGTH_RANGE"
},
required: {
label: "&FIRST_NAME",
message: "VALIDATE.REQUIRED"
}
} },
last_name: { validators: {
length: {
label: "&LAST_NAME",
min: 1,
max: 30,
message: "VALIDATE.LENGTH_RANGE"
},
required: {
label: "&LAST_NAME",
message: "VALIDATE.REQUIRED"
}
} },
locale: { validators: { required: {
label: "&LOCALE",
domain: "server",
message: "VALIDATE.REQUIRED"
} } }
};
function Q() {
const a = s(!1), e = s(null), o = s({
first_name: "",
last_name: "",
locale: ""
}), { r$: n } = p(o, f().adapt(M));
async function l(r) {
return a.value = !0, e.value = null, m.post("/account/settings/profile", r).then((t) => {
w().load(), E().push({
title: t.data.title,
description: t.data.description,
style: d.Success
});
}).catch((t) => {
throw e.value = t.response.data, e.value;
}).finally(() => {
a.value = !1;
});
}
return { submitProfileEdit: l, apiLoading: a, apiError: e, formData: o, r$: n };
}
const V = {
passwordcheck: { validators: { required: { message: "PASSWORD.CONFIRM_CURRENT" } } },
password: { validators: { length: {
label: "&PASSWORD",
min: 12,
max: 100,
message: "VALIDATE.LENGTH_RANGE"
} } },
passwordc: { validators: {
matches: {
field: "password",
label: "&PASSWORD.CONFIRM",
message: "VALIDATE.PASSWORD_MISMATCH"
},
length: {
label: "&PASSWORD.CONFIRM",
min: 12,
max: 100,
message: "VALIDATE.LENGTH_RANGE"
}
} }
};
function k() {
const a = s(!1), e = s(null), o = s(0), n = s(0), l = s({
passwordcheck: "",
password: "",
passwordc: ""
}), r = v();
o.value = r.get("site.password.length.min"), n.value = r.get("site.password.length.max");
const { r$: t } = p(l, f().adapt(V));
async function i(A) {
return a.value = !0, e.value = null, m.post("/account/settings", A).then((g) => {
E().push({
title: g.data.title,
description: g.data.description,
style: d.Success
});
}).catch((g) => {
throw e.value = g.response.data, e.value;
}).finally(() => {
a.value = !1;
});
}
return {
submitPasswordEdit: i,
apiLoading: a,
apiError: e,
formData: l,
r$: t,
minLength: o,
maxLength: n
};
}
const U = {
passwordcheck: { validators: { required: { message: "PASSWORD.CONFIRM_CURRENT" } } },
email: { validators: {
required: {
label: "&EMAIL",
message: "VALIDATE.REQUIRED"
},
length: {
label: "&EMAIL",
min: 1,
max: 150,
message: "VALIDATE.LENGTH_RANGE"
},
email: { message: "VALIDATE.INVALID_EMAIL" }
} }
};
function z() {
const a = s(!1), e = s(null), o = s({
email: "",
passwordcheck: ""
}), { r$: n } = p(o, f().adapt(U));
async function l(r) {
return a.value = !0, e.value = null, m.post("/account/settings/email", r).then((t) => (E().push({
style: d.Success,
...t.data
}), t.data)).catch((t) => {
e.value = t.response.data;
}).finally(() => {
a.value = !1;
});
}
return { submitEmailEdit: l, apiLoading: a, apiError: e, formData: o, r$: n };
}
function j() {
const a = s(!1), e = s(null);
async function o(l) {
a.value = !0, e.value = null;
const r = {
email: l
};
return m.post("/account/verify/request", r).then((t) => t.data).catch((t) => {
var i;
e.value = ((i = t.response) == null ? void 0 : i.data) ?? { description: t.message };
}).finally(() => {
a.value = !1;
});
}
async function n(l, r) {
a.value = !0, e.value = null;
const t = {
email: l,
code: r
};
return m.post("/account/verify/email", t).then((i) => (E().push({
style: d.Success,
...i.data
}), i.data)).catch((i) => {
var A;
e.value = ((A = i.response) == null ? void 0 : A.data) ?? { description: i.message };
}).finally(() => {
a.value = !1;
});
}
return { requestVerificationCode: o, submitVerificationCode: n, apiLoading: a, apiError: e };
}
function B() {
const a = s(!1), e = s(null);
async function o(l) {
a.value = !0, e.value = null;
const r = {
email: l
};
return m.post("/account/forgot-password/request", r).then((t) => {
E().push({
title: t.data.title,
description: t.data.description,
style: d.Success
});
}).catch((t) => {
throw e.value = t.response.data, e.value;
}).finally(() => {
a.value = !1;
});
}
async function n(l) {
return a.value = !0, e.value = null, m.post("/account/forgot-password/set-password", l).then((r) => {
E().push({
title: r.data.title,
description: r.data.description,
style: d.Success
});
}).catch((r) => {
throw e.value = r.response.data, e.value;
}).finally(() => {
a.value = !1;
});
}
return { requestCode: o, setPassword: n, apiLoading: a, apiError: e };
}
export {
Z as useAuthorizationManager,
X as useAxiosInterceptor,
j as useEmailVerificationApi,
B as useForgotPasswordApi,
H as useLoginApi,
$ as useLogoutApi,
F as useRegisterApi,
z as useUserEmailEditApi,
k as useUserPasswordEditApi,
Q as useUserProfileEditApi
};