talizen 0.2.15 → 0.2.17
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/README.md +11 -24
- package/auth.d.ts +0 -4
- package/auth.js +6 -13
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -111,42 +111,29 @@ When a `File` object appears in the payload, `submitForm()` will:
|
|
|
111
111
|
|
|
112
112
|
### Login users
|
|
113
113
|
|
|
114
|
-
```
|
|
114
|
+
```tsx
|
|
115
115
|
import {
|
|
116
|
-
currentUser,
|
|
117
116
|
listAuthProviders,
|
|
118
|
-
login,
|
|
119
117
|
loginWithOAuth,
|
|
120
|
-
logout,
|
|
121
|
-
register,
|
|
122
|
-
updateProfile,
|
|
123
118
|
useAuth,
|
|
124
119
|
} from "talizen/auth";
|
|
125
120
|
|
|
126
|
-
await register({
|
|
127
|
-
account: "alice",
|
|
128
|
-
password: "secret",
|
|
129
|
-
name: "Alice",
|
|
130
|
-
email: "hi@talizen.com",
|
|
131
|
-
});
|
|
132
|
-
await login({ account: "alice", password: "secret" });
|
|
133
|
-
|
|
134
|
-
const user = await currentUser();
|
|
135
|
-
await updateProfile({
|
|
136
|
-
address: "No. 1 Example Road",
|
|
137
|
-
});
|
|
138
|
-
await logout();
|
|
139
|
-
|
|
140
121
|
const providers = await listAuthProviders();
|
|
141
122
|
console.log(providers.map((provider) => provider.key));
|
|
142
123
|
|
|
143
124
|
await loginWithOAuth("github", { redirectUrl: "/account" });
|
|
144
125
|
|
|
145
126
|
function AccountBadge() {
|
|
146
|
-
const { user, loading, logout } = useAuth();
|
|
127
|
+
const { user, loading, login, register, logout, updateProfile } = useAuth();
|
|
147
128
|
if (loading) return <span>Loading...</span>;
|
|
148
|
-
if (!user) return <
|
|
149
|
-
return
|
|
129
|
+
if (!user) return <button onClick={() => login("alice", "secret")}>Sign in</button>;
|
|
130
|
+
return (
|
|
131
|
+
<div>
|
|
132
|
+
<button onClick={() => register("bob", "secret", "Bob")}>Create account</button>
|
|
133
|
+
<button onClick={() => updateProfile({ address: "No. 1 Example Road" })}>Update profile</button>
|
|
134
|
+
<button onClick={() => logout()}>{user.name ?? user.account ?? "Logout"}</button>
|
|
135
|
+
</div>
|
|
136
|
+
);
|
|
150
137
|
}
|
|
151
138
|
```
|
|
152
139
|
|
|
@@ -231,7 +218,7 @@ Func HTTP responses use the HTTP status code rather than a top-level `ok` field.
|
|
|
231
218
|
## Package Layout
|
|
232
219
|
|
|
233
220
|
- `talizen/core`: shared runtime config, request helpers, and base data types.
|
|
234
|
-
- `talizen/auth`: project user register,
|
|
221
|
+
- `talizen/auth`: project user register, current user helpers, and the React `useAuth()` state hook for login/logout flows.
|
|
235
222
|
- `talizen/cms`: CMS content types and content query APIs.
|
|
236
223
|
- `talizen/form`: form submission helpers and related types.
|
|
237
224
|
- `talizen/func`: custom function invocation helpers such as `invoke`.
|
package/auth.d.ts
CHANGED
|
@@ -54,7 +54,6 @@ export declare class TalizenAuthError extends Error {
|
|
|
54
54
|
constructor(message: string);
|
|
55
55
|
}
|
|
56
56
|
export declare function register(input: AuthRegisterInput, options?: TalizenRequestOptions): Promise<AuthUser>;
|
|
57
|
-
export declare function login(input: AuthPasswordInput, options?: TalizenRequestOptions): Promise<AuthUser>;
|
|
58
57
|
export interface UseAuthResult {
|
|
59
58
|
user: AuthUser | null;
|
|
60
59
|
loading: boolean;
|
|
@@ -76,10 +75,7 @@ export interface UseAuthResult {
|
|
|
76
75
|
* config, which lets preview auth headers update the page without remounting.
|
|
77
76
|
*/
|
|
78
77
|
export declare function useAuth(options?: TalizenRequestOptions): UseAuthResult;
|
|
79
|
-
export declare function logout(options?: TalizenRequestOptions): Promise<void>;
|
|
80
|
-
export declare function currentUser(options?: TalizenRequestOptions): Promise<AuthUser | null>;
|
|
81
78
|
export declare function updateProfile(profile: AuthProfile, options?: TalizenRequestOptions): Promise<AuthUser>;
|
|
82
|
-
export declare function requireUser(options?: TalizenRequestOptions): Promise<AuthUser>;
|
|
83
79
|
export declare function listAuthProviders(options?: TalizenRequestOptions): Promise<AuthProvider[]>;
|
|
84
80
|
export declare function getOAuthLoginUrl(provider: string, options?: AuthOAuthLoginURLOptions): Promise<string>;
|
|
85
81
|
export declare function loginWithOAuth(provider: string, options?: AuthOAuthLoginURLOptions): Promise<void>;
|
package/auth.js
CHANGED
|
@@ -12,7 +12,7 @@ export async function register(input, options) {
|
|
|
12
12
|
body: JSON.stringify(input),
|
|
13
13
|
}, options);
|
|
14
14
|
}
|
|
15
|
-
|
|
15
|
+
async function loginRequest(input, options) {
|
|
16
16
|
return requestJson("/auth/login", {
|
|
17
17
|
method: "POST",
|
|
18
18
|
body: JSON.stringify(input),
|
|
@@ -83,7 +83,7 @@ async function refreshAuthState(options) {
|
|
|
83
83
|
authErrorState = null;
|
|
84
84
|
emitAuthChange();
|
|
85
85
|
try {
|
|
86
|
-
const user = await
|
|
86
|
+
const user = await fetchCurrentUser(options);
|
|
87
87
|
if (authRequestId === requestId) {
|
|
88
88
|
currentUserState = user;
|
|
89
89
|
authErrorState = null;
|
|
@@ -135,7 +135,7 @@ export function useAuth(options) {
|
|
|
135
135
|
void refresh().catch(() => { });
|
|
136
136
|
}, [refresh]);
|
|
137
137
|
const loginAndRefresh = React.useCallback(async (input, password) => {
|
|
138
|
-
const nextUser = await
|
|
138
|
+
const nextUser = await loginRequest(normalizeLoginInput(input, password), optionsRef.current);
|
|
139
139
|
setAuthUser(nextUser);
|
|
140
140
|
return nextUser;
|
|
141
141
|
}, []);
|
|
@@ -145,7 +145,7 @@ export function useAuth(options) {
|
|
|
145
145
|
return nextUser;
|
|
146
146
|
}, []);
|
|
147
147
|
const logoutAndRefresh = React.useCallback(async () => {
|
|
148
|
-
await
|
|
148
|
+
await logoutRequest(optionsRef.current);
|
|
149
149
|
setAuthUser(null);
|
|
150
150
|
}, []);
|
|
151
151
|
const updateProfileAndRefresh = React.useCallback(async (profile) => {
|
|
@@ -162,13 +162,13 @@ export function useAuth(options) {
|
|
|
162
162
|
updateProfile: updateProfileAndRefresh,
|
|
163
163
|
};
|
|
164
164
|
}
|
|
165
|
-
|
|
165
|
+
async function logoutRequest(options) {
|
|
166
166
|
await requestJson("/auth/logout", {
|
|
167
167
|
method: "POST",
|
|
168
168
|
body: JSON.stringify({}),
|
|
169
169
|
}, options);
|
|
170
170
|
}
|
|
171
|
-
|
|
171
|
+
async function fetchCurrentUser(options) {
|
|
172
172
|
return requestJson("/auth/me", { method: "GET" }, options);
|
|
173
173
|
}
|
|
174
174
|
export async function updateProfile(profile, options) {
|
|
@@ -177,13 +177,6 @@ export async function updateProfile(profile, options) {
|
|
|
177
177
|
body: JSON.stringify({ profile }),
|
|
178
178
|
}, options);
|
|
179
179
|
}
|
|
180
|
-
export async function requireUser(options) {
|
|
181
|
-
const user = await currentUser(options);
|
|
182
|
-
if (!user) {
|
|
183
|
-
throw new TalizenAuthError("Login required.");
|
|
184
|
-
}
|
|
185
|
-
return user;
|
|
186
|
-
}
|
|
187
180
|
export async function listAuthProviders(options) {
|
|
188
181
|
const response = await requestJson("/auth/provider_list", { method: "GET" }, options);
|
|
189
182
|
return response.list ?? [];
|