⚡️⚡️⚡️ Self-hosted collaborative bookmark manager to collect, read, annotate, and fully preserve what matters, all in one place.
TypeScript bookmarkbookmark-managercollaborationnextjsreact-nativeread-it-laterself-hostedtypescript
show device name when signing in from the mobile app
daniel31x1312f10eea2b91
diff --git a/apps/mobile/app/verify-email.tsx b/apps/mobile/app/verify-email.tsxindex 47c3fb12..f8c0a31e 100644--- a/apps/mobile/app/verify-email.tsx+++ b/apps/mobile/app/verify-email.tsx@@ -2,6 +2,7 @@ import { useEffect, useRef } from "react";import { ActivityIndicator, Alert, Text, View } from "react-native";import { router, useLocalSearchParams } from "expo-router";import useAuthStore from "@/store/auth";+import { getSessionName } from "@/lib/sessionName";const cloudInstance = "https://cloud.linkwarden.app";@@ -43,7 +44,11 @@ export default function VerifyEmail() {try {const res = await fetch(`${instance}/api/v1/auth/verify-email-token`, {method: "POST",- body: JSON.stringify({ token, email }),+ body: JSON.stringify({+ token,+ email,+ sessionName: getSessionName(),+ }),headers: { "Content-Type": "application/json" },});const data = await res.json().catch(() => null);diff --git a/apps/mobile/lib/sessionName.ts b/apps/mobile/lib/sessionName.tsnew file mode 100644index 00000000..8b2f5ff6--- /dev/null+++ b/apps/mobile/lib/sessionName.ts@@ -0,0 +1,30 @@+import Constants from "expo-constants";+import { Platform } from "react-native";++const MAX_LENGTH = 50;++const osLabel = () => {+ const name = Platform.select({ ios: "iOS", android: "Android" }) ?? "";+ const version =+ Platform.OS === "android"+ ? Platform.constants.Release+ : String(Platform.Version || "");++ return [name, version].filter(Boolean).join(" ").trim();+};++const truncate = (name: string) =>+ name.length > MAX_LENGTH+ ? `${name.slice(0, MAX_LENGTH - 1).trimEnd()}…`+ : name;++export const getSessionName = () => {+ const device = Constants.deviceName?.trim();+ const os = osLabel();++ const name = [device && os ? `${device} (${os})` : "", device, os].find(+ (candidate) => candidate && candidate.length <= MAX_LENGTH+ );++ return name || truncate(device || os || "Mobile Device");+};diff --git a/apps/mobile/store/auth.ts b/apps/mobile/store/auth.tsindex 38e89c7b..153831ec 100644--- a/apps/mobile/store/auth.ts+++ b/apps/mobile/store/auth.ts@@ -20,6 +20,7 @@ import { useOfflineSyncStore } from "@/lib/offlineSync";import { hasInactiveSubscription } from "@/lib/subscription";import { ensureCloudIsReachable } from "@/lib/ensureCloudIsReachable";import { loadCustomHeaders, setCustomHeaders } from "@/lib/customHeaders";+import { getSessionName } from "@/lib/sessionName";const cloudInstance = "https://cloud.linkwarden.app";const cloudConfig: Config = {@@ -393,7 +394,11 @@ const useAuthStore = create<AuthStore>((set, get) => ({const res = await Promise.race([fetch(`${instance}/api/v1/session`, {method: "POST",- body: JSON.stringify({ username, password }),+ body: JSON.stringify({+ username,+ password,+ sessionName: getSessionName(),+ }),headers: { "Content-Type": "application/json" },}),new Promise<Response>((_, reject) =>@@ -477,6 +482,7 @@ const useAuthStore = create<AuthStore>((set, get) => ({body: JSON.stringify({identityToken: credential.identityToken,name: name || undefined,+ sessionName: getSessionName(),}),headers: { "Content-Type": "application/json" },}),@@ -546,6 +552,7 @@ const useAuthStore = create<AuthStore>((set, get) => ({body: JSON.stringify({identityToken: idToken,name: displayName || undefined,+ sessionName: getSessionName(),}),headers: { "Content-Type": "application/json" },}),diff --git a/apps/web/lib/api/controllers/session/createSession.ts b/apps/web/lib/api/controllers/session/createSession.tsindex ebcf6650..9238a9c7 100644--- a/apps/web/lib/api/controllers/session/createSession.ts+++ b/apps/web/lib/api/controllers/session/createSession.ts@@ -1,4 +1,5 @@import { prisma } from "@linkwarden/prisma";+import { PostTokenSchema } from "@linkwarden/lib/schemaValidation";import crypto from "crypto";import { decode, encode } from "next-auth/jwt";@@ -6,6 +7,19 @@ export default async function createSession(userId: number,sessionName?: string) {+ const dataValidation = PostTokenSchema.shape.name+ .optional()+ .safeParse(sessionName);++ if (!dataValidation.success) {+ return {+ response: `Error: ${dataValidation.error.issues[0].message} [sessionName]`,+ status: 400,+ };+ }++ const name = dataValidation.data || "Unknown Device";+const now = Date.now();const expiryDate = new Date();const oneDayInSeconds = 86400;@@ -31,7 +45,7 @@ export default async function createSession(await prisma.accessToken.create({data: {- name: sessionName || "Unknown Device",+ name,userId,token: tokenBody?.jti as string,isSession: true,