@stanlemon/app-template 0.1.2 → 0.2.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/app.js +23 -0
- package/db.json +10 -0
- package/index.html +1 -0
- package/package.json +7 -4
- package/src/App.less +3 -3
- package/src/App.test.tsx +1 -1
- package/src/App.tsx +92 -4
- package/src/Input.tsx +21 -16
- package/src/Login.tsx +78 -0
- package/src/Register.tsx +90 -0
package/app.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createAppServer,
|
|
3
|
+
asyncJsonHandler as handler,
|
|
4
|
+
SimpleUsersDao,
|
|
5
|
+
} from "@stanlemon/server-with-auth";
|
|
6
|
+
|
|
7
|
+
const users = new SimpleUsersDao([
|
|
8
|
+
{
|
|
9
|
+
username: "user",
|
|
10
|
+
password: "password",
|
|
11
|
+
},
|
|
12
|
+
]);
|
|
13
|
+
|
|
14
|
+
const app = createAppServer({
|
|
15
|
+
webpack: "http://localhost:8080",
|
|
16
|
+
secure: ["/api/"],
|
|
17
|
+
...users,
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
app.get(
|
|
21
|
+
"/api/users",
|
|
22
|
+
handler(() => ({ users: users.users }))
|
|
23
|
+
);
|
package/db.json
ADDED
package/index.html
CHANGED
package/package.json
CHANGED
|
@@ -1,19 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stanlemon/app-template",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "A template for creating apps using the webdev package.",
|
|
5
5
|
"author": "Stan Lemon <stanlemon@users.noreply.github.com>",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"scripts": {
|
|
9
|
-
"start": "
|
|
10
|
-
"build": "
|
|
9
|
+
"start": "node app.js",
|
|
10
|
+
"build": "npm run webpack:build",
|
|
11
|
+
"webpack:serve": "webpack serve",
|
|
12
|
+
"webpack:build": "NODE_ENV=production webpack",
|
|
11
13
|
"test": "jest",
|
|
12
14
|
"test:coverage": "jest --coverage",
|
|
13
15
|
"lint": "eslint --ext js,jsx,ts,tsx ./src/",
|
|
14
16
|
"lint:format": "eslint --fix --ext js,jsx,ts,tsx ./src/"
|
|
15
17
|
},
|
|
16
18
|
"dependencies": {
|
|
17
|
-
"@stanlemon/webdev": "*"
|
|
19
|
+
"@stanlemon/webdev": "*",
|
|
20
|
+
"@stanlemon/server-with-auth": "*"
|
|
18
21
|
}
|
|
19
22
|
}
|
package/src/App.less
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
@
|
|
1
|
+
@errorColor: "red";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
noscript {
|
|
4
|
+
background-color: @errorColor;
|
|
5
5
|
}
|
package/src/App.test.tsx
CHANGED
package/src/App.tsx
CHANGED
|
@@ -1,19 +1,107 @@
|
|
|
1
|
-
import { useState } from "react";
|
|
1
|
+
import { useState, useEffect, createContext } from "react";
|
|
2
2
|
import "./App.less";
|
|
3
3
|
import Header from "./Header";
|
|
4
4
|
import Input from "./Input";
|
|
5
|
+
import Login from "./Login";
|
|
6
|
+
import Register from "./Register";
|
|
7
|
+
|
|
8
|
+
export const SessionContext = createContext<{
|
|
9
|
+
session: Session | null;
|
|
10
|
+
setSession: React.Dispatch<React.SetStateAction<Session | null>>;
|
|
11
|
+
} | null>(null);
|
|
12
|
+
|
|
13
|
+
export type ErrorMessage = {
|
|
14
|
+
message: string;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export type FormErrors = {
|
|
18
|
+
errors: Record<string, string>;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type Session = {
|
|
22
|
+
token: string | null;
|
|
23
|
+
user: User | null;
|
|
24
|
+
};
|
|
25
|
+
export type User = {
|
|
26
|
+
name: string | null;
|
|
27
|
+
email: string | null;
|
|
28
|
+
username: string;
|
|
29
|
+
password: string;
|
|
30
|
+
};
|
|
5
31
|
|
|
6
32
|
export default function App() {
|
|
33
|
+
const [session, setSession] = useState<Session | null>(null);
|
|
34
|
+
const [value, setValue] = useState<string>("");
|
|
7
35
|
const [items, setItems] = useState<string[]>([]);
|
|
36
|
+
|
|
37
|
+
const contextValue = { session, setSession };
|
|
38
|
+
|
|
39
|
+
useEffect(() => {
|
|
40
|
+
fetch("/auth/session", {
|
|
41
|
+
headers: {
|
|
42
|
+
Authorization: `Bearer ${session?.token || ""}`,
|
|
43
|
+
Accept: "application/json",
|
|
44
|
+
"Content-Type": "application/json",
|
|
45
|
+
},
|
|
46
|
+
})
|
|
47
|
+
.then((response) => {
|
|
48
|
+
if (!response.ok) {
|
|
49
|
+
throw new Error(response.statusText);
|
|
50
|
+
}
|
|
51
|
+
return response;
|
|
52
|
+
})
|
|
53
|
+
.then((response) => response.json())
|
|
54
|
+
.then((session: Session) => {
|
|
55
|
+
setSession(session);
|
|
56
|
+
})
|
|
57
|
+
.catch((err) => {
|
|
58
|
+
console.error(err);
|
|
59
|
+
});
|
|
60
|
+
}, [session?.token]);
|
|
61
|
+
|
|
62
|
+
const addItem = () => {
|
|
63
|
+
setItems([...items, value]);
|
|
64
|
+
setValue("");
|
|
65
|
+
};
|
|
66
|
+
|
|
8
67
|
return (
|
|
9
|
-
<
|
|
68
|
+
<SessionContext.Provider value={contextValue}>
|
|
10
69
|
<Header />
|
|
11
|
-
<
|
|
70
|
+
<div>
|
|
71
|
+
{!session && (
|
|
72
|
+
<>
|
|
73
|
+
<p>
|
|
74
|
+
<em>You are not currently logged in.</em>
|
|
75
|
+
</p>
|
|
76
|
+
<Login />
|
|
77
|
+
<Spacer />
|
|
78
|
+
<Register />
|
|
79
|
+
<Spacer />
|
|
80
|
+
</>
|
|
81
|
+
)}
|
|
82
|
+
{session?.user && (
|
|
83
|
+
<p>
|
|
84
|
+
<em>You logged in as {session.user.username}.</em>
|
|
85
|
+
</p>
|
|
86
|
+
)}
|
|
87
|
+
</div>
|
|
88
|
+
<Input
|
|
89
|
+
label="Item"
|
|
90
|
+
value={value}
|
|
91
|
+
onChange={(value) => setValue(value)}
|
|
92
|
+
onEnter={addItem}
|
|
93
|
+
/>
|
|
94
|
+
<button onClick={addItem}>Add</button>
|
|
95
|
+
|
|
12
96
|
<ul>
|
|
13
97
|
{items.map((item, i) => (
|
|
14
98
|
<li key={i}>{item}</li>
|
|
15
99
|
))}
|
|
16
100
|
</ul>
|
|
17
|
-
</
|
|
101
|
+
</SessionContext.Provider>
|
|
18
102
|
);
|
|
19
103
|
}
|
|
104
|
+
|
|
105
|
+
function Spacer() {
|
|
106
|
+
return <div style={{ minHeight: "2em" }} />;
|
|
107
|
+
}
|
package/src/Input.tsx
CHANGED
|
@@ -1,37 +1,42 @@
|
|
|
1
|
-
import { useState } from "react";
|
|
2
|
-
|
|
3
1
|
export default function Input({
|
|
4
|
-
|
|
2
|
+
type = "text",
|
|
3
|
+
value = "",
|
|
4
|
+
label,
|
|
5
|
+
placeholder,
|
|
6
|
+
onChange = () => {
|
|
7
|
+
/* noop */
|
|
8
|
+
},
|
|
9
|
+
onEnter = () => {
|
|
10
|
+
/* noop */
|
|
11
|
+
},
|
|
5
12
|
}: {
|
|
6
|
-
|
|
13
|
+
type?: string;
|
|
14
|
+
value?: string;
|
|
15
|
+
label?: string;
|
|
16
|
+
placeholder?: string;
|
|
17
|
+
onChange?: (value: string) => void;
|
|
18
|
+
onEnter?: () => void;
|
|
7
19
|
}) {
|
|
8
|
-
const [value, setValue] = useState("");
|
|
9
|
-
|
|
10
20
|
const handleChange = (e: React.ChangeEvent<HTMLInputElement>): void => {
|
|
11
|
-
|
|
21
|
+
onChange(e.currentTarget.value);
|
|
12
22
|
};
|
|
13
|
-
|
|
14
23
|
const handleKeyPress = (e: React.KeyboardEvent<HTMLInputElement>): void => {
|
|
15
24
|
if (e.key === "Enter") {
|
|
16
25
|
e.preventDefault();
|
|
17
|
-
|
|
26
|
+
onEnter();
|
|
18
27
|
}
|
|
19
28
|
};
|
|
20
29
|
|
|
21
|
-
const handleClick = () => {
|
|
22
|
-
onClick(value);
|
|
23
|
-
setValue("");
|
|
24
|
-
};
|
|
25
|
-
|
|
26
30
|
return (
|
|
27
31
|
<>
|
|
32
|
+
{label && <label>{label}</label>}
|
|
28
33
|
<input
|
|
29
|
-
type=
|
|
34
|
+
type={type}
|
|
30
35
|
onChange={handleChange}
|
|
31
36
|
onKeyPress={handleKeyPress}
|
|
37
|
+
placeholder={placeholder}
|
|
32
38
|
value={value}
|
|
33
39
|
/>
|
|
34
|
-
<button onClick={handleClick}>Add</button>
|
|
35
40
|
</>
|
|
36
41
|
);
|
|
37
42
|
}
|
package/src/Login.tsx
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { useState, useContext } from "react";
|
|
2
|
+
import { Session, User, SessionContext, ErrorMessage } from "./App";
|
|
3
|
+
import Input from "./Input";
|
|
4
|
+
|
|
5
|
+
export default function Login() {
|
|
6
|
+
const [error, setError] = useState<string | null>(null);
|
|
7
|
+
const [values, setValues] = useState<User>({
|
|
8
|
+
name: "",
|
|
9
|
+
email: "",
|
|
10
|
+
username: "",
|
|
11
|
+
password: "",
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
const { setSession } = useContext(SessionContext) || {
|
|
15
|
+
setSession: () => {},
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const onSubmit = () => {
|
|
19
|
+
setError(null);
|
|
20
|
+
fetch("/auth/login", {
|
|
21
|
+
headers: {
|
|
22
|
+
Accept: "application/json",
|
|
23
|
+
"Content-Type": "application/json",
|
|
24
|
+
},
|
|
25
|
+
method: "POST",
|
|
26
|
+
body: JSON.stringify(values),
|
|
27
|
+
})
|
|
28
|
+
.then((response) =>
|
|
29
|
+
response.json().then((data: Record<string, unknown>) => ({
|
|
30
|
+
ok: response.ok,
|
|
31
|
+
status: response.status,
|
|
32
|
+
data,
|
|
33
|
+
}))
|
|
34
|
+
)
|
|
35
|
+
.then(
|
|
36
|
+
({
|
|
37
|
+
ok,
|
|
38
|
+
status,
|
|
39
|
+
data,
|
|
40
|
+
}: {
|
|
41
|
+
ok: boolean;
|
|
42
|
+
status: number;
|
|
43
|
+
data: Record<string, unknown>;
|
|
44
|
+
}) => {
|
|
45
|
+
if (ok) {
|
|
46
|
+
setSession(data as Session);
|
|
47
|
+
} else {
|
|
48
|
+
setError((data as ErrorMessage).message);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
)
|
|
52
|
+
.catch((err) => {
|
|
53
|
+
console.error("error", err);
|
|
54
|
+
});
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
return (
|
|
58
|
+
<div>
|
|
59
|
+
{error && (
|
|
60
|
+
<div>
|
|
61
|
+
<strong>{error}</strong>
|
|
62
|
+
</div>
|
|
63
|
+
)}
|
|
64
|
+
<Input
|
|
65
|
+
label="Username"
|
|
66
|
+
value={values.username}
|
|
67
|
+
onChange={(value) => setValues({ ...values, username: value })}
|
|
68
|
+
/>
|
|
69
|
+
<Input
|
|
70
|
+
type="password"
|
|
71
|
+
label="Password"
|
|
72
|
+
value={values.password}
|
|
73
|
+
onChange={(value) => setValues({ ...values, password: value })}
|
|
74
|
+
/>
|
|
75
|
+
<button onClick={onSubmit}>Login</button>
|
|
76
|
+
</div>
|
|
77
|
+
);
|
|
78
|
+
}
|
package/src/Register.tsx
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { useState, useContext } from "react";
|
|
2
|
+
import { Session, User, SessionContext, FormErrors } from "./App";
|
|
3
|
+
import Input from "./Input";
|
|
4
|
+
|
|
5
|
+
// eslint-disable-next-line max-lines-per-function
|
|
6
|
+
export default function Register() {
|
|
7
|
+
const [error, setError] = useState<string | null>(null);
|
|
8
|
+
const [values, setValues] = useState<User>({
|
|
9
|
+
name: "",
|
|
10
|
+
email: "",
|
|
11
|
+
username: "",
|
|
12
|
+
password: "",
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
const { setSession } = useContext(SessionContext) || {
|
|
16
|
+
setSession: () => {},
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const onSubmit = () => {
|
|
20
|
+
setError(null);
|
|
21
|
+
fetch("/auth/register", {
|
|
22
|
+
headers: {
|
|
23
|
+
Accept: "application/json",
|
|
24
|
+
"Content-Type": "application/json",
|
|
25
|
+
},
|
|
26
|
+
method: "POST",
|
|
27
|
+
body: JSON.stringify(values),
|
|
28
|
+
})
|
|
29
|
+
.then((response) =>
|
|
30
|
+
response.json().then((data: Record<string, unknown>) => ({
|
|
31
|
+
ok: response.ok,
|
|
32
|
+
status: response.status,
|
|
33
|
+
data,
|
|
34
|
+
}))
|
|
35
|
+
)
|
|
36
|
+
.then(
|
|
37
|
+
({
|
|
38
|
+
ok,
|
|
39
|
+
status,
|
|
40
|
+
data,
|
|
41
|
+
}: {
|
|
42
|
+
ok: boolean;
|
|
43
|
+
status: number;
|
|
44
|
+
data: Record<string, unknown>;
|
|
45
|
+
}) => {
|
|
46
|
+
console.log(ok, status, data);
|
|
47
|
+
|
|
48
|
+
if (ok) {
|
|
49
|
+
setSession(data as Session);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
)
|
|
53
|
+
.catch((err) => {
|
|
54
|
+
console.error("error", err);
|
|
55
|
+
});
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
return (
|
|
59
|
+
<div>
|
|
60
|
+
{error && (
|
|
61
|
+
<div>
|
|
62
|
+
<strong>{error}</strong>
|
|
63
|
+
</div>
|
|
64
|
+
)}
|
|
65
|
+
<Input
|
|
66
|
+
label="Name"
|
|
67
|
+
value={values.name || ""}
|
|
68
|
+
onChange={(value) => setValues({ ...values, name: value })}
|
|
69
|
+
/>
|
|
70
|
+
<Input
|
|
71
|
+
type="email"
|
|
72
|
+
label="Email"
|
|
73
|
+
value={values.email || ""}
|
|
74
|
+
onChange={(value) => setValues({ ...values, email: value })}
|
|
75
|
+
/>
|
|
76
|
+
<Input
|
|
77
|
+
label="Username"
|
|
78
|
+
value={values.username}
|
|
79
|
+
onChange={(value) => setValues({ ...values, username: value })}
|
|
80
|
+
/>
|
|
81
|
+
<Input
|
|
82
|
+
type="password"
|
|
83
|
+
label="Password"
|
|
84
|
+
value={values.password}
|
|
85
|
+
onChange={(value) => setValues({ ...values, password: value })}
|
|
86
|
+
/>
|
|
87
|
+
<button onClick={onSubmit}>Register</button>
|
|
88
|
+
</div>
|
|
89
|
+
);
|
|
90
|
+
}
|