@rpcbase/server 0.249.0 → 0.251.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.
- package/boot/shared.js +2 -0
- package/index.js +3 -0
- package/package.json +1 -1
- package/src/auth/index.js +0 -6
- package/src/auth/sign_in.js +1 -0
- package/src/auth/sign_up.js +16 -25
- package/src/helpers/expect_ext.js +42 -0
package/boot/shared.js
CHANGED
package/index.js
CHANGED
|
@@ -5,6 +5,8 @@ const express = require("./express")
|
|
|
5
5
|
const firebase = require("./firebase")
|
|
6
6
|
const client_router = require("./src/client/client_router")
|
|
7
7
|
const rpc_router = require("./src/rpc/rpc_router")
|
|
8
|
+
const sign_up = require("./src/auth/sign_up")
|
|
9
|
+
|
|
8
10
|
|
|
9
11
|
module.exports = {
|
|
10
12
|
queue,
|
|
@@ -13,4 +15,5 @@ module.exports = {
|
|
|
13
15
|
firebase,
|
|
14
16
|
client_router,
|
|
15
17
|
rpc_router,
|
|
18
|
+
sign_up,
|
|
16
19
|
}
|
package/package.json
CHANGED
package/src/auth/index.js
CHANGED
|
@@ -9,11 +9,6 @@ const set_new_password = require("./set_new_password")
|
|
|
9
9
|
const check_session = require("./check_session")
|
|
10
10
|
|
|
11
11
|
|
|
12
|
-
const sign_up_handler = async(req, res) => {
|
|
13
|
-
const result = await sign_up(req.body, {req, res})
|
|
14
|
-
res.json(result)
|
|
15
|
-
}
|
|
16
|
-
|
|
17
12
|
const sign_in_handler = async(req, res) => {
|
|
18
13
|
const result = await sign_in(req.body, {req, res})
|
|
19
14
|
res.json(result)
|
|
@@ -40,7 +35,6 @@ const check_session_handler = async(req, res) => {
|
|
|
40
35
|
}
|
|
41
36
|
|
|
42
37
|
module.exports = (app) => {
|
|
43
|
-
app.post("/api/v1/auth/sign_up", async_wrapper(sign_up_handler))
|
|
44
38
|
app.post("/api/v1/auth/sign_in", async_wrapper(sign_in_handler))
|
|
45
39
|
app.post("/api/v1/auth/sign_out", async_wrapper(sign_out_handler))
|
|
46
40
|
|
package/src/auth/sign_in.js
CHANGED
package/src/auth/sign_up.js
CHANGED
|
@@ -5,38 +5,33 @@ const get_object_id = require("../../get_object_id")
|
|
|
5
5
|
|
|
6
6
|
const mongoose = require("../../mongoose")
|
|
7
7
|
|
|
8
|
-
const
|
|
8
|
+
const MIN_PASSWORD_LENGTH = 12
|
|
9
|
+
|
|
10
|
+
const sign_up = async(payload, ctx, session) => {
|
|
11
|
+
const {email, password} = payload
|
|
12
|
+
expect(email).toBeEmail()
|
|
13
|
+
expect(password.length).toBeGreaterThanOrEqual(MIN_PASSWORD_LENGTH)
|
|
14
|
+
|
|
9
15
|
const User = mongoose.model("User")
|
|
10
16
|
const Invite = mongoose.model("Invite")
|
|
11
17
|
|
|
12
|
-
const {req} = ctx
|
|
13
|
-
|
|
14
18
|
// check if the user already exists
|
|
15
|
-
const existing_user = await User.findOne({email}, null, {ctx})
|
|
19
|
+
const existing_user = await User.findOne({email}, null, {ctx, session})
|
|
16
20
|
|
|
17
21
|
if (existing_user) {
|
|
18
|
-
|
|
19
|
-
status: "error",
|
|
20
|
-
message: "User already exists"
|
|
21
|
-
}
|
|
22
|
+
throw new Error("User already exists")
|
|
22
23
|
}
|
|
23
24
|
|
|
24
25
|
// check if we have an invite for this user
|
|
25
|
-
const invite = await Invite.findOne({email}, null, {ctx})
|
|
26
|
+
const invite = await Invite.findOne({email}, null, {ctx, session})
|
|
26
27
|
|
|
27
|
-
// TODO: mark invite as accepted here
|
|
28
|
+
// TODO: mark invite as accepted here so it can't be reused
|
|
28
29
|
if (invite && !invite.is_ready) {
|
|
29
30
|
console.log("found an invite, but not ready", email)
|
|
30
|
-
|
|
31
|
-
status: "error",
|
|
32
|
-
message: "Your invite is still pending approval. Expect an email in the next weeks to activate your account."
|
|
33
|
-
}
|
|
31
|
+
throw new Error("Your invite is still pending approval. Expect an email in the next weeks to activate your account.")
|
|
34
32
|
} else if (!invite) {
|
|
35
33
|
console.log("no invite for signup email:", email)
|
|
36
|
-
|
|
37
|
-
status: "error",
|
|
38
|
-
message: "No invite was found for this email"
|
|
39
|
-
}
|
|
34
|
+
throw new Error("No invite was found for this email")
|
|
40
35
|
}
|
|
41
36
|
|
|
42
37
|
const hash = await hash_password(password)
|
|
@@ -49,15 +44,11 @@ const sign_up = async({email, password}, ctx) => {
|
|
|
49
44
|
|
|
50
45
|
// sign the user in
|
|
51
46
|
const user_id = user._id.toString()
|
|
52
|
-
req.session.user_id = user_id
|
|
53
47
|
|
|
54
|
-
//
|
|
55
|
-
await user.save({ctx})
|
|
48
|
+
// WARNING: it is now the responsibility of the app to add user.save with session
|
|
49
|
+
// await user.save({ctx})
|
|
56
50
|
|
|
57
|
-
return
|
|
58
|
-
status: "ok",
|
|
59
|
-
user_id,
|
|
60
|
-
}
|
|
51
|
+
return user
|
|
61
52
|
}
|
|
62
53
|
|
|
63
54
|
module.exports = sign_up
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/* @flow */
|
|
2
|
+
const {expect} = require("expect")
|
|
3
|
+
|
|
4
|
+
const isMongoId = require("validator/lib/isMongoId")
|
|
5
|
+
const isEmail = require("validator/lib/isEmail")
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
expect.extend({
|
|
9
|
+
toBeMongoId: (actual) => {
|
|
10
|
+
const pass = isMongoId(actual)
|
|
11
|
+
|
|
12
|
+
if (pass) {
|
|
13
|
+
return {
|
|
14
|
+
message: () => `expected ${this.utils.printReceived(actual)} not to be a valid mongoId`,
|
|
15
|
+
pass: true,
|
|
16
|
+
}
|
|
17
|
+
} else {
|
|
18
|
+
return {
|
|
19
|
+
message: () => `expected ${this.utils.printReceived(actual)} to be a valid mongoId`,
|
|
20
|
+
pass: false,
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
toBeEmail: (actual) => {
|
|
25
|
+
const pass = isEmail(actual)
|
|
26
|
+
|
|
27
|
+
if (pass) {
|
|
28
|
+
return {
|
|
29
|
+
message: () =>
|
|
30
|
+
`expected ${this.utils.printReceived(actual)} not to be a valid email address`,
|
|
31
|
+
pass: true,
|
|
32
|
+
}
|
|
33
|
+
} else {
|
|
34
|
+
return {
|
|
35
|
+
message: () => `expected ${this.utils.printReceived(actual)} to be a valid email address`,
|
|
36
|
+
pass: false,
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
global.expect = expect
|