@rpcbase/client 0.20.0 → 0.21.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.
@@ -2,6 +2,8 @@
2
2
  import {useState, useEffect} from "react"
3
3
  import {useSearchParam} from "react-use"
4
4
  import isEmail from "validator/lib/isEmail"
5
+ import page from "page"
6
+ import debug from "debug"
5
7
 
6
8
  import post from "../../helpers/post"
7
9
 
@@ -10,6 +12,10 @@ import Footer from "../Footer"
10
12
 
11
13
  import "./sign-in.scss"
12
14
 
15
+
16
+ const log = debug("rb:auth:signin")
17
+
18
+
13
19
  const SignIn = ({
14
20
  name,
15
21
  logo,
@@ -25,7 +31,6 @@ const SignIn = ({
25
31
  const redirect = useSearchParam("redirect")
26
32
 
27
33
  useEffect(() => {
28
- console.log("email changed", email)
29
34
  if (isEmail(email)) {
30
35
  setForgotUrlParam(btoa(email))
31
36
  } else {
@@ -39,15 +44,21 @@ const SignIn = ({
39
44
  const res = await post("/api/v1/auth/sign_in", {email, password})
40
45
  setIsLoading(false)
41
46
 
47
+ // success
42
48
  if (res.status === "ok") {
49
+ log("signed in res: ok", res)
50
+ log("redirect to:", redirect || onSuccessRedirect)
51
+
43
52
  set_is_signed_in(true)
44
53
  if (redirect) {
45
- window.location = redirect
46
- return
54
+ page(redirect)
55
+ } else {
56
+ page(onSuccessRedirect)
47
57
  }
48
- window.location = onSuccessRedirect
49
-
58
+ // errors
50
59
  } else {
60
+ log("sign in error", res)
61
+
51
62
  setErrors(res.errors)
52
63
  }
53
64
  }
package/auth/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  /* @flow */
2
2
  import page from "page"
3
+ import debug from "debug"
3
4
 
4
5
  import SignIn from "./SignIn"
5
6
  import SignUp from "./SignUp"
@@ -19,12 +20,16 @@ export {
19
20
 
20
21
  const UID_STORAGE_KEY = "rb::session-user_id"
21
22
 
23
+ const log = debug("rb:auth")
24
+
22
25
  let __is_authenticated = null
23
26
  let __user_id = localStorage.getItem(UID_STORAGE_KEY)
24
27
 
28
+
25
29
  const run_session_check = async() => {
26
30
  const res = await post("/api/v1/auth/check_session")
27
31
  const {is_signed_in, user_id} = res
32
+ log("check_session response", res)
28
33
  __is_authenticated = is_signed_in
29
34
  __user_id = user_id
30
35
  // cache user_id in localStorage
@@ -42,7 +47,11 @@ const redirect_sign_in = (ctx) => {
42
47
 
43
48
 
44
49
  export const session_restrict = (ctx, next) => {
50
+ log("session_restrict:__is_authenticated", __is_authenticated)
51
+
45
52
  if (typeof __is_authenticated !== "boolean") {
53
+ log("session_restrict: will run session check")
54
+
46
55
  run_session_check()
47
56
  .then(() => {
48
57
  if (__is_authenticated) {
@@ -58,8 +67,8 @@ export const session_restrict = (ctx, next) => {
58
67
  } else if (__is_authenticated) {
59
68
  next()
60
69
  } else {
61
- console.log("AUTH", JSON.stringify(__is_authenticated))
62
- console.log("NOT AUTHENTICATED, REDIRECT HERE")
70
+ log("AUTH", JSON.stringify(__is_authenticated))
71
+ log("NOT AUTHENTICATED, REDIRECT HERE")
63
72
  next()
64
73
  }
65
74
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rpcbase/client",
3
- "version": "0.20.0",
3
+ "version": "0.21.0",
4
4
  "scripts": {
5
5
  "test": "echo \"Error: no test specified\" && exit 0"
6
6
  },