@rpcbase/client 0.174.0 → 0.174.1-pagerouterauth.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/auth/ForgotPassword/index.js +1 -1
- package/auth/SetNewPassword/index.js +1 -1
- package/auth/SignIn/SignInEmailForm.js +3 -3
- package/auth/SignUp/SignUpEmailForm.js +3 -3
- package/auth/helpers/{redirect_sign_in.native.js → redirectSignIn.native.js} +1 -3
- package/auth/helpers/{redirect_sign_in.web.js → redirectSignIn.web.js} +1 -3
- package/auth/index.js +39 -39
- package/auth/sign_out.js +3 -3
- package/package.json +1 -1
|
@@ -8,7 +8,7 @@ import isEmail from "validator/lib/isEmail"
|
|
|
8
8
|
import page from "../../page"
|
|
9
9
|
import post from "../../helpers/post"
|
|
10
10
|
import {reconnect as rts_reconnect} from "../../rts/rts"
|
|
11
|
-
import {
|
|
11
|
+
import {setIsSignedIn, setUid, setTenantId} from "../index"
|
|
12
12
|
|
|
13
13
|
|
|
14
14
|
const log = debug("rb:auth:signin")
|
|
@@ -47,8 +47,8 @@ const SignInEmailForm = ({onSuccess, onSuccessRedirect}) => {
|
|
|
47
47
|
|
|
48
48
|
setUid(user_id)
|
|
49
49
|
const tenantId = user_id.slice(8, 16)
|
|
50
|
-
|
|
51
|
-
|
|
50
|
+
setTenantId(tenantId)
|
|
51
|
+
setIsSignedIn(true)
|
|
52
52
|
|
|
53
53
|
// we must now reconnect on the websocket as we now have a new cookie
|
|
54
54
|
rts_reconnect(tenantId)
|
|
@@ -4,7 +4,7 @@ import {useState} from "react"
|
|
|
4
4
|
import page from "../../page"
|
|
5
5
|
import {reconnect as rts_reconnect} from "../../rts/rts"
|
|
6
6
|
|
|
7
|
-
import {
|
|
7
|
+
import {setIsSignedIn, setUid, setTenantId} from "../index"
|
|
8
8
|
import post from "../../helpers/post"
|
|
9
9
|
|
|
10
10
|
|
|
@@ -27,8 +27,8 @@ const SignUpEmailForm = ({onSuccessRedirect, onSuccess}) => {
|
|
|
27
27
|
setUid(user_id)
|
|
28
28
|
|
|
29
29
|
const tenantId = user_id.slice(8, 16)
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
setTenantId(tenantId)
|
|
31
|
+
setIsSignedIn(true)
|
|
32
32
|
|
|
33
33
|
// we must now reconnect on the websocket as we have a new cookie
|
|
34
34
|
rts_reconnect(tenantId)
|
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
/* @flow */
|
|
2
2
|
|
|
3
|
-
const
|
|
3
|
+
export const redirectSignIn = (ctx) => {
|
|
4
4
|
console.log("REDIRECT SIGN IN NYI NATIVE")
|
|
5
5
|
console.log("CTX", ctx)
|
|
6
6
|
|
|
7
7
|
// const redirect_path = ctx.canonicalPath
|
|
8
8
|
// page.redirect(`/signin?redirect=${encodeURIComponent(redirect_path)}`)
|
|
9
9
|
}
|
|
10
|
-
|
|
11
|
-
export default redirect_sign_in
|
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
/* @flow */
|
|
2
2
|
import page from "../../page"
|
|
3
3
|
|
|
4
|
-
const
|
|
4
|
+
export const redirectSignIn = (ctx) => {
|
|
5
5
|
const redirect_path = ctx.canonicalPath
|
|
6
6
|
page.redirect(`/signin?redirect=${encodeURIComponent(redirect_path)}`)
|
|
7
7
|
}
|
|
8
|
-
|
|
9
|
-
export default redirect_sign_in
|
package/auth/index.js
CHANGED
|
@@ -4,26 +4,26 @@ import {Platform} from "react-native"
|
|
|
4
4
|
import isHexadecimal from "validator/lib/isHexadecimal"
|
|
5
5
|
|
|
6
6
|
import storage from "../storage"
|
|
7
|
-
import {disconnect as
|
|
7
|
+
import {disconnect as rtsDisconnect} from "../rts/rts"
|
|
8
8
|
import post from "../helpers/post"
|
|
9
9
|
|
|
10
|
-
import
|
|
10
|
+
import {redirectSignIn} from "./helpers/redirectSignIn"
|
|
11
11
|
|
|
12
12
|
|
|
13
13
|
const LAST_TENANT_KEY = "rb.last_tenant_id"
|
|
14
|
-
const
|
|
14
|
+
const uidStorageKey = (tenant_id) => `rb.${tenant_id}.user_id`
|
|
15
15
|
|
|
16
16
|
const log = debug("rb:auth")
|
|
17
17
|
|
|
18
18
|
// TODO: this should be refactored to AuthContext + Provider
|
|
19
19
|
|
|
20
|
-
let
|
|
20
|
+
let __isAuthenticated = null
|
|
21
21
|
|
|
22
|
-
let
|
|
23
|
-
let
|
|
22
|
+
let __tenantId = typeof storage !== "undefined" && storage.getItem(LAST_TENANT_KEY)
|
|
23
|
+
let __userId = typeof storage !== "undefined" && __tenantId && storage.getItem(uidStorageKey(__tenantId))
|
|
24
24
|
|
|
25
25
|
|
|
26
|
-
const
|
|
26
|
+
const getQueryStringAuthId = () => {
|
|
27
27
|
const url = new URL(window.location.href)
|
|
28
28
|
const val = url.searchParams.get("auth_id")
|
|
29
29
|
if (!val) return null
|
|
@@ -34,14 +34,14 @@ const get_querystring_auth_id = () => {
|
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
|
|
37
|
-
const
|
|
37
|
+
const runSessionCheck = async() => {
|
|
38
38
|
// TODO: TMP: we're skipping the session check on mobile for now,
|
|
39
39
|
// this assumes having a token on mobile means we're authenticated
|
|
40
40
|
if (Platform.OS !== "web") {
|
|
41
41
|
return
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
const auth_user_id =
|
|
44
|
+
const auth_user_id = getQueryStringAuthId()
|
|
45
45
|
|
|
46
46
|
if (auth_user_id) {
|
|
47
47
|
log("check_session with auth user_id", auth_user_id)
|
|
@@ -51,27 +51,27 @@ const run_session_check = async() => {
|
|
|
51
51
|
|
|
52
52
|
log("check_session response", res)
|
|
53
53
|
|
|
54
|
-
|
|
54
|
+
__isAuthenticated = res.is_signed_in
|
|
55
55
|
|
|
56
56
|
|
|
57
|
-
// if not authenticated, clear
|
|
58
|
-
if (!
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
57
|
+
// if not authenticated, clear __userId + __tenantId and disconnect socket
|
|
58
|
+
if (!__isAuthenticated) {
|
|
59
|
+
__userId = null
|
|
60
|
+
__tenantId = null
|
|
61
|
+
rtsDisconnect()
|
|
62
62
|
return
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
|
|
66
|
-
|
|
65
|
+
__userId = res.user_id
|
|
66
|
+
__tenantId = __userId.slice(8, 16)
|
|
67
67
|
|
|
68
68
|
// save tenant id
|
|
69
|
-
if (
|
|
70
|
-
storage.setItem(LAST_TENANT_KEY,
|
|
69
|
+
if (__tenantId) {
|
|
70
|
+
storage.setItem(LAST_TENANT_KEY, __tenantId)
|
|
71
71
|
|
|
72
72
|
// cache user_id in storage
|
|
73
|
-
if (
|
|
74
|
-
storage.setItem(
|
|
73
|
+
if (__userId) {
|
|
74
|
+
storage.setItem(uidStorageKey(__tenantId), __userId)
|
|
75
75
|
}
|
|
76
76
|
}
|
|
77
77
|
}
|
|
@@ -79,54 +79,54 @@ const run_session_check = async() => {
|
|
|
79
79
|
|
|
80
80
|
// Force trigger run first session check (when we are not in a webworker (or mobile))
|
|
81
81
|
const isJest = !!process?.env?.JEST_WORKER_ID
|
|
82
|
-
if (typeof window !== "undefined" && !isJest)
|
|
82
|
+
if (typeof window !== "undefined" && !isJest) runSessionCheck()
|
|
83
83
|
|
|
84
84
|
|
|
85
|
-
export const
|
|
86
|
-
log("session_restrict:
|
|
85
|
+
export const getSessionRestrictMiddleware = () => (ctx, next) => {
|
|
86
|
+
log("session_restrict:__isAuthenticated", __isAuthenticated)
|
|
87
87
|
|
|
88
|
-
if (typeof
|
|
88
|
+
if (typeof __isAuthenticated !== "boolean") {
|
|
89
89
|
log("session_restrict: will run session check")
|
|
90
90
|
|
|
91
|
-
|
|
91
|
+
runSessionCheck()
|
|
92
92
|
.then(() => {
|
|
93
|
-
if (
|
|
93
|
+
if (__isAuthenticated) {
|
|
94
94
|
next()
|
|
95
95
|
} else {
|
|
96
|
-
|
|
96
|
+
redirectSignIn(ctx)
|
|
97
97
|
}
|
|
98
98
|
})
|
|
99
99
|
.catch((err) => {
|
|
100
100
|
console.log("warning error in check session request", err)
|
|
101
101
|
throw err
|
|
102
102
|
})
|
|
103
|
-
} else if (
|
|
103
|
+
} else if (__isAuthenticated) {
|
|
104
104
|
next()
|
|
105
105
|
} else {
|
|
106
|
-
log("AUTH", JSON.stringify(
|
|
106
|
+
log("AUTH", JSON.stringify(__isAuthenticated))
|
|
107
107
|
log("NOT AUTHENTICATED, REDIRECT HERE")
|
|
108
|
-
|
|
108
|
+
redirectSignIn(ctx)
|
|
109
109
|
// next()
|
|
110
110
|
}
|
|
111
111
|
}
|
|
112
112
|
|
|
113
113
|
|
|
114
|
-
export const privateGetUid = () =>
|
|
114
|
+
export const privateGetUid = () => __userId
|
|
115
115
|
export const setUid = (val) => {
|
|
116
|
-
|
|
116
|
+
__userId = val
|
|
117
117
|
|
|
118
118
|
if (typeof val === "string") {
|
|
119
119
|
const tenant_id_prefix = val.slice(8, 16)
|
|
120
|
-
storage.setItem(
|
|
120
|
+
storage.setItem(uidStorageKey(tenant_id_prefix), val)
|
|
121
121
|
}
|
|
122
122
|
}
|
|
123
123
|
|
|
124
|
-
export const getTenantId = () =>
|
|
125
|
-
export const
|
|
126
|
-
|
|
124
|
+
export const getTenantId = () => __tenantId
|
|
125
|
+
export const setTenantId = (val) => {
|
|
126
|
+
__tenantId = val
|
|
127
127
|
storage.setItem(LAST_TENANT_KEY, val)
|
|
128
128
|
}
|
|
129
129
|
|
|
130
|
-
export const
|
|
130
|
+
export const setIsSignedIn = (val) => __isAuthenticated = val
|
|
131
131
|
|
|
132
|
-
export const
|
|
132
|
+
export const getIsSignedIn = () => __isAuthenticated
|
package/auth/sign_out.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/* @flow */
|
|
2
|
-
import {
|
|
2
|
+
import {setIsSignedIn, setUid, setTenantId} from "./index"
|
|
3
3
|
|
|
4
4
|
export const sign_out = () => {
|
|
5
5
|
setUid(null)
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
setTenantId(null)
|
|
7
|
+
setIsSignedIn(false)
|
|
8
8
|
localStorage.clear()
|
|
9
9
|
|
|
10
10
|
// TODO: clear DB
|