small improvement

This commit is contained in:
daniel31x13
2025-08-29 11:40:24 -04:00
parent 07665cee7e
commit 3dfa53cf01
2 changed files with 27 additions and 20 deletions

View File

@@ -71,9 +71,9 @@ export default function HomeScreen() {
}
setShowInstanceField(!showInstanceField);
}}
className="mx-8 mt-2 w-fit"
className="mx-8 mt-2 self-start"
>
<Text className="text-neutral-content text-sm w-fit">
<Text className="text-neutral-content text-sm">
{!showInstanceField ? "Change host" : "Use default host"}
</Text>
</TouchableOpacity>

View File

@@ -2,6 +2,7 @@ import { create } from "zustand";
import * as SecureStore from "expo-secure-store";
import { router } from "expo-router";
import { MobileAuth } from "@linkwarden/types";
import { Alert } from "react-native";
type AuthStore = {
auth: MobileAuth;
@@ -19,7 +20,7 @@ const useAuthStore = create<AuthStore>((set) => ({
auth: {
instance: "",
session: null,
status: "loading",
status: "loading" as const,
},
setAuth: async () => {
const session = await SecureStore.getItemAsync("TOKEN");
@@ -48,15 +49,27 @@ const useAuthStore = create<AuthStore>((set) => ({
console.log("Signing into", instance);
if (token) {
await SecureStore.setItemAsync("TOKEN", token);
// make a request to the API to validate the token (TODO)
router.replace("/(tabs)/dashboard");
set({
auth: {
session: token,
instance,
status: "authenticated",
// make a request to the API to validate the token
await fetch(instance + "/api/v1/users/me", {
method: "GET",
headers: {
Authorization: `Bearer ${token}`,
},
}).then(async (res) => {
if (res.ok) {
await SecureStore.setItemAsync("INSTANCE", instance);
await SecureStore.setItemAsync("TOKEN", token);
set({
auth: {
session: token,
instance,
status: "authenticated",
},
});
router.replace("/(tabs)/dashboard");
} else {
Alert.alert("Error", "Invalid token");
}
});
} else {
await fetch(instance + "/api/v1/session", {
@@ -81,24 +94,18 @@ const useAuthStore = create<AuthStore>((set) => ({
router.replace("/(tabs)/dashboard");
} else {
set({
auth: {
instance,
session: null,
status: "unauthenticated",
},
});
Alert.alert("Error", "Invalid credentials");
}
});
}
},
signOut: async () => {
await SecureStore.deleteItemAsync("TOKEN");
const instance = await SecureStore.getItemAsync("INSTANCE");
await SecureStore.deleteItemAsync("INSTANCE");
set({
auth: {
instance,
instance: "",
session: null,
status: "unauthenticated",
},