talizen 0.2.16 → 0.2.18
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 +18 -25
- package/auth.d.ts +0 -2
- package/auth.js +2 -9
- package/package.json +1 -1
- package/server-runtime.d.ts +6 -0
package/README.md
CHANGED
|
@@ -111,38 +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
117
|
loginWithOAuth,
|
|
119
|
-
register,
|
|
120
|
-
updateProfile,
|
|
121
118
|
useAuth,
|
|
122
119
|
} from "talizen/auth";
|
|
123
120
|
|
|
124
|
-
await register({
|
|
125
|
-
account: "alice",
|
|
126
|
-
password: "secret",
|
|
127
|
-
name: "Alice",
|
|
128
|
-
email: "hi@talizen.com",
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
const user = await currentUser();
|
|
132
|
-
await updateProfile({
|
|
133
|
-
address: "No. 1 Example Road",
|
|
134
|
-
});
|
|
135
|
-
|
|
136
121
|
const providers = await listAuthProviders();
|
|
137
122
|
console.log(providers.map((provider) => provider.key));
|
|
138
123
|
|
|
139
124
|
await loginWithOAuth("github", { redirectUrl: "/account" });
|
|
140
125
|
|
|
141
126
|
function AccountBadge() {
|
|
142
|
-
const { user, loading, login, logout } = useAuth();
|
|
127
|
+
const { user, loading, login, register, logout, updateProfile } = useAuth();
|
|
143
128
|
if (loading) return <span>Loading...</span>;
|
|
144
129
|
if (!user) return <button onClick={() => login("alice", "secret")}>Sign in</button>;
|
|
145
|
-
return
|
|
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
|
+
);
|
|
146
137
|
}
|
|
147
138
|
```
|
|
148
139
|
|
|
@@ -166,21 +157,23 @@ await invoke("booking", { email: "hi@talizen.com" });
|
|
|
166
157
|
|
|
167
158
|
### Server-side page context
|
|
168
159
|
|
|
169
|
-
In `getServerSideProps`, use the injected server context for request metadata
|
|
170
|
-
and
|
|
171
|
-
|
|
160
|
+
In `getServerSideProps`, use the injected server context for request metadata,
|
|
161
|
+
cookies, and auth. Do not import `talizen/auth` or `talizen/func`, and do not
|
|
162
|
+
call Func from server-side page code:
|
|
172
163
|
|
|
173
164
|
```ts
|
|
174
165
|
import type { TalizenServerSideContext } from "talizen/server-runtime";
|
|
175
166
|
|
|
176
167
|
export async function getServerSideProps(ctx: TalizenServerSideContext) {
|
|
177
|
-
|
|
168
|
+
const user = ctx.auth.currentUser();
|
|
169
|
+
return { props: { path: ctx.request.path, user } };
|
|
178
170
|
}
|
|
179
171
|
```
|
|
180
172
|
|
|
181
|
-
Server-side context supports `ctx.request` and `ctx.
|
|
182
|
-
does not expose `ctx.
|
|
183
|
-
private business data
|
|
173
|
+
Server-side context supports `ctx.request`, `ctx.cookies`, and `ctx.auth`.
|
|
174
|
+
It intentionally does not expose `ctx.db`, `ctx.cache`, or `ctx.func`; put
|
|
175
|
+
login UI, private business data writes, and custom backend actions in
|
|
176
|
+
browser-side SDK/Func/API flows.
|
|
184
177
|
|
|
185
178
|
### Write function runtime code
|
|
186
179
|
|
package/auth.d.ts
CHANGED
|
@@ -75,9 +75,7 @@ export interface UseAuthResult {
|
|
|
75
75
|
* config, which lets preview auth headers update the page without remounting.
|
|
76
76
|
*/
|
|
77
77
|
export declare function useAuth(options?: TalizenRequestOptions): UseAuthResult;
|
|
78
|
-
export declare function currentUser(options?: TalizenRequestOptions): Promise<AuthUser | null>;
|
|
79
78
|
export declare function updateProfile(profile: AuthProfile, options?: TalizenRequestOptions): Promise<AuthUser>;
|
|
80
|
-
export declare function requireUser(options?: TalizenRequestOptions): Promise<AuthUser>;
|
|
81
79
|
export declare function listAuthProviders(options?: TalizenRequestOptions): Promise<AuthProvider[]>;
|
|
82
80
|
export declare function getOAuthLoginUrl(provider: string, options?: AuthOAuthLoginURLOptions): Promise<string>;
|
|
83
81
|
export declare function loginWithOAuth(provider: string, options?: AuthOAuthLoginURLOptions): Promise<void>;
|
package/auth.js
CHANGED
|
@@ -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;
|
|
@@ -168,7 +168,7 @@ async function logoutRequest(options) {
|
|
|
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 ?? [];
|
package/package.json
CHANGED
package/server-runtime.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { AuthUser } from "./auth.js";
|
|
1
2
|
export interface ServerReadonlyStringMap {
|
|
2
3
|
get(name: string): string | null;
|
|
3
4
|
}
|
|
@@ -27,6 +28,10 @@ export interface TalizenServerCookieRuntime {
|
|
|
27
28
|
ok?: boolean;
|
|
28
29
|
};
|
|
29
30
|
}
|
|
31
|
+
export interface TalizenServerAuthRuntime {
|
|
32
|
+
currentUser(): AuthUser | null;
|
|
33
|
+
requireUser(): AuthUser;
|
|
34
|
+
}
|
|
30
35
|
export interface TalizenServerSideContext {
|
|
31
36
|
query: Record<string, string | string[]>;
|
|
32
37
|
searchParams: Record<string, string | string[]>;
|
|
@@ -37,4 +42,5 @@ export interface TalizenServerSideContext {
|
|
|
37
42
|
request: TalizenServerRequestRuntime;
|
|
38
43
|
req: TalizenServerRequestRuntime;
|
|
39
44
|
cookies: TalizenServerCookieRuntime;
|
|
45
|
+
auth: TalizenServerAuthRuntime;
|
|
40
46
|
}
|