@rpcbase/client 0.9.0 → 0.12.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/Footer.js ADDED
@@ -0,0 +1,14 @@
1
+ /* @flow */
2
+
3
+ const year = (new Date).getFullYear()
4
+
5
+ const Footer = ({name}) => {
6
+
7
+ return (
8
+ <footer className="pt-5 my-5 text-muted border-top">
9
+ &copy; {year} <span className="ms-1">{name}</span>
10
+ </footer>
11
+ )
12
+ }
13
+
14
+ export default Footer
@@ -0,0 +1,134 @@
1
+ /* @flow */
2
+ import {useState, useEffect} from "react"
3
+ import {useSearchParam} from "react-use"
4
+
5
+ import post from "../../helpers/post"
6
+
7
+ import {set_is_signed_in} from "../index"
8
+ import Footer from "../Footer"
9
+ // import sign_in from "rpc!server/auth/sign_in"
10
+
11
+ import "./sign-in.scss"
12
+
13
+ const SignIn = ({name, logo}) => {
14
+ const [email, setEmail] = useState("")
15
+ const [password, setPassword] = useState("")
16
+
17
+ const [isLoading, setIsLoading] = useState(false)
18
+ const [errors, setErrors] = useState()
19
+
20
+ const redirect = useSearchParam("redirect")
21
+
22
+ const onSubmit = async(e) => {
23
+ e.preventDefault()
24
+ setIsLoading(true)
25
+ const res = await post("/api/v1/auth/sign_in", {email, password})
26
+ setIsLoading(false)
27
+
28
+ if (res.status === "ok") {
29
+ set_is_signed_in(true)
30
+ if (redirect) {
31
+ window.location = redirect
32
+ return
33
+ }
34
+ window.location = "/workspaces"
35
+
36
+ } else {
37
+ setErrors(res.errors)
38
+ }
39
+ }
40
+
41
+ const onChangeEmail = (e) => {
42
+ setEmail(e.target.value)
43
+ }
44
+
45
+ const onChangePassword = (e) => {
46
+ setPassword(e.target.value)
47
+ }
48
+
49
+ return (
50
+ <div id="sign-in-wrapper">
51
+ <div className="form-signin text-center px-4 py-4 shadow-lg">
52
+ <div>
53
+ <div className="d-flex align-items-center justify-content-center">
54
+ {logo}
55
+ <span className="ms-1 fs-4">{name}</span>
56
+ </div>
57
+
58
+ <hr />
59
+
60
+ <h1 className="h4 mt-3 mb-3 fw-normal">Sign In</h1>
61
+ <p className="text-start text-muted">
62
+ Use your email and password to sign in. Don't have an account ?
63
+ <a href="/signup" className="ms-1">Sign up here</a>
64
+ </p>
65
+
66
+ <form onSubmit={onSubmit}>
67
+
68
+ {errors?.form && (
69
+ <p className="text-danger">{errors.form}</p>
70
+ )}
71
+
72
+ <div className="form-floating">
73
+ <input type="email"
74
+ className="form-control"
75
+ id="input-email"
76
+ placeholder="name@example.com"
77
+ value={email}
78
+ onChange={onChangeEmail} />
79
+ <label htmlFor="input-email">Email Address</label>
80
+ </div>
81
+
82
+ <div className="form-floating">
83
+ <input type="password"
84
+ className="form-control"
85
+ id="input-password"
86
+ placeholder="Password"
87
+ value={password}
88
+ onChange={onChangePassword} />
89
+ <label htmlFor="input-password">Password</label>
90
+ </div>
91
+
92
+ <div className="checkbox mt-3 mb-3 text-start">
93
+ <label style={{cursor: "pointer", userSelect: "none"}}>
94
+ <input
95
+ type="checkbox"
96
+ value="remember-me"
97
+ defaultChecked /> Remember me
98
+ </label>
99
+ </div>
100
+
101
+ <button className="w-100 btn btn-lg btn-primary" type="submit" onClick={onSubmit}>Sign In</button>
102
+ </form>
103
+
104
+ <hr />
105
+ <p className="text-muted">OR</p>
106
+
107
+ <a href="/signup"
108
+ className="sign-in-github-btn p-2 btn-lg w-100"
109
+ style={{}}>
110
+ <div style={{}} className="me-2">
111
+ <svg height="28px" width="28px" viewBox="0 0 16 16" style={{fill: "currentColor"}}>
112
+ <path fillRule={"evenodd"} d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38
113
+ 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01
114
+ 1.08.58 1.23.82.72 1.21 1.87.87
115
+ 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12
116
+ 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08
117
+ 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0
118
+ .21.15.46.55.38A8.013 8.013 0 0016 8c0-4.42-3.58-8-8-8z"/>
119
+ </svg>
120
+ </div>
121
+ <div className="">
122
+ Sign in with GitHub
123
+ </div>
124
+ </a>
125
+
126
+ </div>
127
+ </div>
128
+
129
+ <Footer name={name} />
130
+ </div>
131
+ )
132
+ }
133
+
134
+ export default SignIn
@@ -0,0 +1,66 @@
1
+ @import "helpers";
2
+
3
+ #sign-in-wrapper {
4
+ height: 100%;
5
+ display: flex;
6
+ flex-direction: column;
7
+ align-items: center;
8
+ padding-top: 40px;
9
+ padding-bottom: 40px;
10
+ // background-color: var(--bs-secondary);
11
+ background-color: $gray-500;
12
+
13
+ hr {
14
+ background-color: $gray-500;
15
+ }
16
+
17
+ footer {
18
+ color: $light !important;
19
+ border-top: none !important;
20
+ padding-top: 0 !important;
21
+ margin-bottom: 0 !important;
22
+ }
23
+
24
+
25
+ .form-signin {
26
+ width: 100%;
27
+ max-width: 380px;
28
+ margin: auto;
29
+ background-color: $light;
30
+ border-radius: 22px;
31
+ border: 1px solid $gray-600;
32
+ }
33
+
34
+ .form-signin .checkbox {
35
+ font-weight: 400;
36
+ }
37
+
38
+ .form-signin .form-floating:focus-within {
39
+ z-index: 2;
40
+ }
41
+
42
+ .form-signin input[type="email"] {
43
+ margin-bottom: -1px;
44
+ border-bottom-right-radius: 0;
45
+ border-bottom-left-radius: 0;
46
+ }
47
+
48
+ .form-signin #input-password {
49
+ margin-bottom: 10px;
50
+ border-top-left-radius: 0;
51
+ border-top-right-radius: 0;
52
+ }
53
+
54
+ .sign-in-github-btn {
55
+ display: inline-flex;
56
+ align-items: center;
57
+ background-color: #24292E;
58
+ font-size: 1.25rem;
59
+ color: $gray-200;
60
+ text-decoration: none;
61
+
62
+ &:hover {
63
+ color: $white;
64
+ }
65
+ }
66
+ }
@@ -0,0 +1,35 @@
1
+ /* @flow */
2
+ import {useState, useEffect} from "react"
3
+ import {ActivityIndicator} from "react-native"
4
+ import page from "page"
5
+
6
+ import post from "../../helpers/post"
7
+ import {set_is_signed_in} from "../index"
8
+
9
+ const SignOut = () => {
10
+
11
+ useEffect(() => {
12
+ setTimeout(async() => {
13
+ const res = await post("/api/v1/auth/sign_out")
14
+ if (res.status === "ok") {
15
+ set_is_signed_in(false)
16
+ localStorage.clear()
17
+ page.redirect("/")
18
+ } else {
19
+ throw new Error("unable to sign out")
20
+ }
21
+ }, 60)
22
+
23
+ }, [])
24
+
25
+ return (
26
+ <div id="sign-out-wrapper">
27
+ <div className="mt-3 d-flex justify-content-center align-items-center">
28
+ <div className="me-2"><ActivityIndicator /></div>
29
+ <div className="">Signing out, you will be redirected shortly</div>
30
+ </div>
31
+ </div>
32
+ )
33
+ }
34
+
35
+ export default SignOut
@@ -0,0 +1,107 @@
1
+ /* @flow */
2
+ import {useState} from "react"
3
+
4
+ import post from "../../helpers/post"
5
+ import Footer from "../Footer"
6
+
7
+ import "./sign-up.scss"
8
+
9
+ const SignUp = ({logo, name}) => {
10
+ const [email, setEmail] = useState("")
11
+ const [password, setPassword] = useState("")
12
+ const [passwordConfirm, setPasswordConfirm] = useState("")
13
+ const [error, setError] = useState(null)
14
+
15
+ const onSubmit = async() => {
16
+ setError(null)
17
+ if (!password || password !== passwordConfirm) {
18
+ setError("Passwords do not match")
19
+ return
20
+ }
21
+ const res = await post("/api/v1/auth/sign_up", {email, password})
22
+ if (res.status === "ok") {
23
+ window.location = "/workspaces"
24
+ } else if (res.status === "error") {
25
+ setError(res.message)
26
+ }
27
+ }
28
+
29
+ const onChangeEmail = (e) => {
30
+ setEmail(e.target.value)
31
+ }
32
+
33
+ const onChangePassword = (e) => {
34
+ setPassword(e.target.value)
35
+ }
36
+
37
+ const onChangePasswordConfirm = (e) => {
38
+ setPasswordConfirm(e.target.value)
39
+ }
40
+
41
+
42
+ return (
43
+ <div id="sign-up-wrapper">
44
+ <div className="form-signup text-center px-4 py-4 shadow-lg">
45
+ <div>
46
+ <div className="d-flex align-items-center justify-content-center">
47
+ {logo}
48
+ <span className="ms-1 fs-4">{name}</span>
49
+ </div>
50
+
51
+ <hr />
52
+
53
+ <h1 className="h4 mt-3 mb-3 fw-normal">Sign Up</h1>
54
+ <p className="text-muted">
55
+ Already have an account ? <a href="/signin" className="ms-1">Sign in here</a>
56
+ </p>
57
+
58
+ {error && (
59
+ <div className="invalid-feedback d-flex text-start">
60
+ {error}
61
+ </div>
62
+ )}
63
+
64
+ <div className="form-floating">
65
+ <input type="email"
66
+ className="form-control"
67
+ id="input-email"
68
+ placeholder="name@example.com"
69
+ value={email}
70
+ onChange={onChangeEmail} />
71
+ <label htmlFor="input-email">Email Address</label>
72
+ </div>
73
+ <div className="form-floating">
74
+ <input type="password"
75
+ className="form-control"
76
+ id="input-password"
77
+ placeholder="Password"
78
+ value={password}
79
+ onChange={onChangePassword} />
80
+ <label htmlFor="input-password">Password</label>
81
+ </div>
82
+ <div className="form-floating">
83
+ <input type="password"
84
+ className="form-control"
85
+ id="input-password-confirm"
86
+ placeholder="Confirm Password"
87
+ value={passwordConfirm}
88
+ onChange={onChangePasswordConfirm} />
89
+ <label htmlFor="input-password-confirm">Confirm Password</label>
90
+ </div>
91
+
92
+ <div className="checkbox mt-3 mb-3 text-start">
93
+ <label style={{cursor: "pointer", userSelect: "none"}}>
94
+ <input type="checkbox" value="terms-accepted" /> I agree to the terms and privacy policy.
95
+ </label>
96
+ </div>
97
+ <button className="w-100 btn btn-lg btn-primary" type="submit" onClick={onSubmit}>Sign Up</button>
98
+
99
+ </div>
100
+ </div>
101
+
102
+ <Footer name={name} />
103
+ </div>
104
+ )
105
+ }
106
+
107
+ export default SignUp
@@ -0,0 +1,56 @@
1
+ @import "helpers";
2
+
3
+ #sign-up-wrapper {
4
+ height: 100%;
5
+ display: flex;
6
+ flex-direction: column;
7
+ align-items: center;
8
+ padding-top: 40px;
9
+ padding-bottom: 40px;
10
+ background-color: $gray-500;
11
+
12
+ hr {
13
+ background-color: $gray-500;
14
+ }
15
+
16
+ footer {
17
+ color: $light !important;
18
+ border-top: none !important;
19
+ padding-top: 0 !important;
20
+ margin-bottom: 0 !important;
21
+ }
22
+
23
+ .form-signup {
24
+ width: 100%;
25
+ max-width: 380px;
26
+ margin: auto;
27
+ background-color: $light;
28
+ border-radius: 22px;
29
+ border: 1px solid $gray-600;
30
+ }
31
+
32
+ .form-signup .checkbox {
33
+ font-weight: 400;
34
+ }
35
+
36
+ .form-signup .form-floating:focus-within {
37
+ z-index: 2;
38
+ }
39
+
40
+ .form-signup input[type="email"] {
41
+ margin-bottom: -1px;
42
+ border-bottom-right-radius: 0;
43
+ border-bottom-left-radius: 0;
44
+ }
45
+
46
+ .form-signup #input-password {
47
+ border-radius: 0;
48
+ }
49
+
50
+ .form-signup #input-password-confirm {
51
+ margin-bottom: 10px;
52
+ border-top-left-radius: 0;
53
+ border-top-right-radius: 0;
54
+ border-top: none;
55
+ }
56
+ }
package/auth/index.js ADDED
@@ -0,0 +1,64 @@
1
+ /* @flow */
2
+ import page from "page"
3
+
4
+ import SignIn from "./SignIn"
5
+ import SignUp from "./SignUp"
6
+ import SignOut from "./SignOut"
7
+
8
+ import post from "../helpers/post"
9
+
10
+ export {SignIn, SignUp, SignOut}
11
+
12
+ const UID_STORAGE_KEY = "rb::session-user_id"
13
+
14
+ let __is_authenticated = null
15
+ let __user_id = localStorage.getItem(UID_STORAGE_KEY)
16
+
17
+ const run_session_check = async() => {
18
+ const res = await post("/api/v1/auth/check_session")
19
+ const {is_signed_in, user_id} = res
20
+ __is_authenticated = is_signed_in
21
+ __user_id = user_id
22
+ // cache user_id in localStorage
23
+ if (user_id) {
24
+ localStorage.setItem(UID_STORAGE_KEY, user_id)
25
+ }
26
+ }
27
+
28
+ // manually run first check
29
+ // TODO: this should come from localStorage
30
+ const redirect_sign_in = (ctx) => {
31
+ const redirect_path = ctx.canonicalPath
32
+ page.redirect(`/signin?redirect=${encodeURIComponent(redirect_path)}`)
33
+ }
34
+
35
+
36
+ export const session_restrict = (ctx, next) => {
37
+ if (typeof __is_authenticated !== "boolean") {
38
+ run_session_check()
39
+ .then(() => {
40
+ if (__is_authenticated) {
41
+ next()
42
+ } else {
43
+ redirect_sign_in(ctx)
44
+ }
45
+ })
46
+ .catch((err) => {
47
+ console.log("warning error in check session request", err)
48
+ throw err
49
+ })
50
+ } else if (__is_authenticated) {
51
+ next()
52
+ } else {
53
+ console.log("AUTH", JSON.stringify(__is_authenticated))
54
+ console.log("NOT AUTHENTICATED, REDIRECT HERE")
55
+ next()
56
+ }
57
+ }
58
+
59
+
60
+ export const get_uid = () => __user_id
61
+
62
+ export const set_is_signed_in = (val) => __is_authenticated = val
63
+
64
+ export const is_signed_in = () => __is_authenticated
@@ -0,0 +1,20 @@
1
+ /* @flow */
2
+ import axios from "axios"
3
+
4
+ import config from "config"
5
+
6
+ const {BASE_URL} = config
7
+
8
+ const client = axios.create({
9
+ withCredentials: true,
10
+ headers: {
11
+ "Content-Type": "application/json"
12
+ },
13
+ })
14
+
15
+ const post = async(url, payload) => {
16
+ const res = await client.post(`${BASE_URL}${url}`, payload)
17
+ return res.data
18
+ }
19
+
20
+ export default post
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@rpcbase/client",
3
- "version": "0.9.0",
3
+ "version": "0.12.0",
4
4
  "scripts": {
5
5
  "test": "echo \"Error: no test specified\" && exit 0"
6
6
  },
7
7
  "dependencies": {
8
- "axios": "0.26.0"
8
+ "axios": "0.27.2"
9
9
  }
10
10
  }