@rpcbase/auth 0.33.0 → 0.35.0

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.
@@ -0,0 +1,18 @@
1
+ import { o as object, b as boolean } from "./index-DGbE-dXC.js";
2
+ const Route = "/api/rb/auth/sign-out";
3
+ object({});
4
+ object({
5
+ success: boolean()
6
+ });
7
+ const handleSignOut = async (_, ctx) => {
8
+ await new Promise((resolve) => ctx.req.session.destroy(() => resolve()));
9
+ return {
10
+ success: true
11
+ };
12
+ };
13
+ const handler = (api) => {
14
+ api.post(Route, handleSignOut);
15
+ };
16
+ export {
17
+ handler as default
18
+ };
@@ -0,0 +1,32 @@
1
+ import crypto from "crypto";
2
+ import { i as isEmail } from "./isEmail-IG0hXiQk.js";
3
+ import { loadModel } from "@rpcbase/api";
4
+ import { hashPassword } from "@rpcbase/server";
5
+ import { a as Route, c as requestSchema } from "./index-DGbE-dXC.js";
6
+ const signUp = async (payload, ctx) => {
7
+ const User = await loadModel("User", ctx);
8
+ const { email_or_phone, password } = requestSchema.parse(payload);
9
+ const is_email = isEmail(email_or_phone);
10
+ const query = is_email ? { email: email_or_phone } : { phone: email_or_phone };
11
+ const existingUser = await User.findOne(query);
12
+ if (existingUser) {
13
+ console.log("user with email or phone already exists", email_or_phone);
14
+ return { success: false };
15
+ }
16
+ const salt = crypto.randomBytes(16).toString("hex");
17
+ const derivedKey = await hashPassword(password, salt);
18
+ const hashedPassword = `${salt}:${derivedKey.toString("hex")}`;
19
+ const user = new User({
20
+ ...query,
21
+ password: hashedPassword
22
+ });
23
+ await user.save();
24
+ console.log("created new user", user._id.toString());
25
+ return { success: true };
26
+ };
27
+ const handler = (api) => {
28
+ api.post(Route, signUp);
29
+ };
30
+ export {
31
+ handler as default
32
+ };
@@ -0,0 +1,17 @@
1
+ import { i as isEmail } from "./isEmail-IG0hXiQk.js";
2
+ import { R as Route, r as requestSchema } from "./index-DGbE-dXC.js";
3
+ const signIn = async (payload, ctx) => {
4
+ const { email_or_phone } = requestSchema.parse(payload);
5
+ if (isEmail(email_or_phone)) {
6
+ console.log("is valid email");
7
+ } else {
8
+ console.log("is not valid email");
9
+ }
10
+ return { success: true };
11
+ };
12
+ const handler = (api) => {
13
+ api.post(Route, signIn);
14
+ };
15
+ export {
16
+ handler as default
17
+ };