mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 04:27:21 +00:00
feat: enhance API and AuthApi with credentials support and async storage handling
- Added `credentials` option to `ApiOptions` and `BaseModuleApiOptions` for better request handling. - Updated `AuthApi` to pass `verified` status during token updates. - Refactored storage handling in `Api` to support async operations using a Proxy. - Improved `Authenticator` to handle cookie domain configuration and JSON request detection. - Adjusted `useAuth` to ensure logout and verify methods return promises for better async handling. - Fixed navigation URL construction in `useNavigate` and updated context menu actions in `_data.root.tsx`.
This commit is contained in:
@@ -4,7 +4,7 @@ import type { AuthResponse, SafeUser, AuthStrategy } from "bknd";
|
||||
import { type BaseModuleApiOptions, ModuleApi } from "modules/ModuleApi";
|
||||
|
||||
export type AuthApiOptions = BaseModuleApiOptions & {
|
||||
onTokenUpdate?: (token?: string) => void | Promise<void>;
|
||||
onTokenUpdate?: (token?: string, verified?: boolean) => void | Promise<void>;
|
||||
credentials?: "include" | "same-origin" | "omit";
|
||||
};
|
||||
|
||||
@@ -17,23 +17,19 @@ export class AuthApi extends ModuleApi<AuthApiOptions> {
|
||||
}
|
||||
|
||||
async login(strategy: string, input: any) {
|
||||
const res = await this.post<AuthResponse>([strategy, "login"], input, {
|
||||
credentials: this.options.credentials,
|
||||
});
|
||||
const res = await this.post<AuthResponse>([strategy, "login"], input);
|
||||
|
||||
if (res.ok && res.body.token) {
|
||||
await this.options.onTokenUpdate?.(res.body.token);
|
||||
await this.options.onTokenUpdate?.(res.body.token, true);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
async register(strategy: string, input: any) {
|
||||
const res = await this.post<AuthResponse>([strategy, "register"], input, {
|
||||
credentials: this.options.credentials,
|
||||
});
|
||||
const res = await this.post<AuthResponse>([strategy, "register"], input);
|
||||
|
||||
if (res.ok && res.body.token) {
|
||||
await this.options.onTokenUpdate?.(res.body.token);
|
||||
await this.options.onTokenUpdate?.(res.body.token, true);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
@@ -71,6 +67,11 @@ export class AuthApi extends ModuleApi<AuthApiOptions> {
|
||||
}
|
||||
|
||||
async logout() {
|
||||
await this.options.onTokenUpdate?.(undefined);
|
||||
return this.get(["logout"], undefined, {
|
||||
headers: {
|
||||
// this way bknd detects a json request and doesn't redirect back
|
||||
Accept: "application/json",
|
||||
},
|
||||
}).then(() => this.options.onTokenUpdate?.(undefined, true));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,6 +44,7 @@ export interface UserPool {
|
||||
const defaultCookieExpires = 60 * 60 * 24 * 7; // 1 week in seconds
|
||||
export const cookieConfig = s
|
||||
.strictObject({
|
||||
domain: s.string().optional(),
|
||||
path: s.string({ default: "/" }),
|
||||
sameSite: s.string({ enum: ["strict", "lax", "none"], default: "lax" }),
|
||||
secure: s.boolean({ default: true }),
|
||||
@@ -290,6 +291,7 @@ export class Authenticator<
|
||||
|
||||
return {
|
||||
...cookieConfig,
|
||||
domain: cookieConfig.domain ?? undefined,
|
||||
expires: new Date(Date.now() + expires * 1000),
|
||||
};
|
||||
}
|
||||
@@ -354,7 +356,10 @@ export class Authenticator<
|
||||
|
||||
// @todo: move this to a server helper
|
||||
isJsonRequest(c: Context): boolean {
|
||||
return c.req.header("Content-Type") === "application/json";
|
||||
return (
|
||||
c.req.header("Content-Type") === "application/json" ||
|
||||
c.req.header("Accept") === "application/json"
|
||||
);
|
||||
}
|
||||
|
||||
async getBody(c: Context) {
|
||||
|
||||
Reference in New Issue
Block a user