feat: associate device id with username when logging in
This commit is contained in:
@@ -3,7 +3,6 @@ import { env } from "hono/adapter";
|
||||
import mapKeys from "lodash.mapkeys";
|
||||
|
||||
import { Case, changeStringCase } from "~/libs/changeStringCase";
|
||||
import { TokenAlreadyExistsError } from "~/libs/errors/TokenAlreadyExists";
|
||||
import type { AdminSdkCredentials } from "~/libs/fcm/getGoogleAuthToken";
|
||||
import { verifyFcmToken } from "~/libs/fcm/verifyFcmToken";
|
||||
import { readEnvVariable } from "~/libs/readEnvVariable";
|
||||
@@ -21,7 +20,6 @@ const app = new OpenAPIHono<Env>();
|
||||
const SaveTokenRequest = z.object({
|
||||
token: z.string(),
|
||||
deviceId: z.string(),
|
||||
username: z.string().nullable(),
|
||||
});
|
||||
|
||||
const SaveTokenResponse = SuccessResponseSchema();
|
||||
@@ -70,8 +68,7 @@ const route = createRoute({
|
||||
});
|
||||
|
||||
app.openapi(route, async (c) => {
|
||||
const { token, deviceId, username } =
|
||||
await c.req.json<typeof SaveTokenRequest._type>();
|
||||
const { token, deviceId } = await c.req.json<typeof SaveTokenRequest._type>();
|
||||
|
||||
try {
|
||||
const isValidToken = await verifyFcmToken(
|
||||
@@ -85,12 +82,8 @@ app.openapi(route, async (c) => {
|
||||
return c.json(ErrorResponse, 401);
|
||||
}
|
||||
|
||||
await saveToken(env(c, "workerd"), deviceId, token, username);
|
||||
await saveToken(env(c, "workerd"), deviceId, token);
|
||||
} catch (error) {
|
||||
if (error instanceof TokenAlreadyExistsError) {
|
||||
return c.json(ErrorResponse, 412);
|
||||
}
|
||||
|
||||
console.error(new Error("Failed to save token", { cause: error }));
|
||||
return c.json(ErrorResponse, 500);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user