Files
waseel/lib/auth.ts
T
Krikorios 62dc5e9d53 Rebrand to Waseel, swap Stripe for Areeba, add phone-ready auth and DB seed
- Rename app: Waseel (name, slug, scheme waseel://, com.waseel.app ids, splash)
- Payments: replace Stripe with Areeba hosted checkout (create/verify API routes,
  lib/areeba.ts, WebBrowser-based payment flow)
- Maps: migrate address autocomplete to Places API (New), drop legacy library
- Web support: map stub for web (native maps are iOS/Android only)
- Auth: keep email + Google OAuth; fix OAuth redirect for Expo Go
- Add scripts/seed-db.mjs (schema + Lebanese driver seed)
- Pin Expo SDK 51 compatible package versions
2026-08-22 15:53:41 +03:00

98 lines
2.4 KiB
TypeScript

import type {
StartOAuthFlowParams,
StartOAuthFlowReturnType,
} from "@clerk/clerk-expo";
import * as Linking from "expo-linking";
import * as SecureStore from "expo-secure-store";
import { fetchAPI } from "./fetch";
export interface TokenCache {
getToken: (key: string) => Promise<string | undefined | null>;
saveToken: (key: string, token: string) => Promise<void>;
clearToken?: (key: string) => void;
}
export const tokenCache = {
async getToken(key: string) {
try {
const item = await SecureStore.getItemAsync(key);
return item;
} catch (error) {
console.error("GET_TOKEN_CACHE: ", error);
await SecureStore.deleteItemAsync(key);
return null;
}
},
async saveToken(key: string, value: string) {
try {
return SecureStore.setItemAsync(key, value);
} catch (err) {
console.error("SAVE_TOKEN_CACHE: ", err);
return;
}
},
};
type StartOAuthFlowType = (
startOAuthFlowParams?: StartOAuthFlowParams,
) => Promise<StartOAuthFlowReturnType>;
export const googleOAuth = async (startOAuthFlow: StartOAuthFlowType) => {
try {
// No explicit scheme: in Expo Go this resolves to exp://... so the
// browser can redirect back into the app; in a standalone build it
// automatically uses the app.json scheme ("waseel").
const { createdSessionId, signUp, setActive } = await startOAuthFlow({
redirectUrl: Linking.createURL("/(root)/(tabs)/home"),
});
if (createdSessionId) {
if (setActive) {
await setActive!({ session: createdSessionId });
if (signUp && signUp.createdUserId) {
await fetchAPI("/(api)/user", {
method: "POST",
body: JSON.stringify({
name: `${signUp.firstName} ${signUp.lastName}`,
email: signUp.emailAddress,
clerkId: signUp.createdUserId,
}),
});
}
return {
success: true,
code: "success",
message: "You are logged in.",
};
}
return {
success: false,
code: "failed",
message: "An error occured.",
};
} else {
// Use signIn or signUp for next steps such as MFA
}
} catch (err) {
console.log("[OAUTH]: ", err);
return {
success: false,
code: (
err as {
code?: string;
}
)?.code,
message: "Internal Server Error.",
};
}
};