Add self-hosted auth, admin API, and owner web dashboard
- Replace Clerk with self-hosted JWT auth (register/login/verify, bcrypt passwords, Gmail OTP with console fallback) - Add lib/db.ts pg pool + transaction helpers; seed script migrates legacy Clerk-era schema (drop clerk_id, enforce UUID ids and unique email) - Add owner-gated admin API: stats, users, drivers CRUD, rides - Add dashboard/ Vite React owner dashboard (login, overview, users, fleet, rides) with dev-server proxy to avoid Expo CORS middleware - Add scripts/set-owner.mjs for role management
This commit is contained in:
+21
-19
@@ -1,4 +1,3 @@
|
||||
import { useSignIn } from "@clerk/clerk-expo";
|
||||
import { Link, useRouter } from "expo-router";
|
||||
import { useCallback, useState } from "react";
|
||||
import { Alert, Image, ScrollView, Text, View } from "react-native";
|
||||
@@ -7,42 +6,45 @@ import { CustomButton } from "@/components/custom-button";
|
||||
import { InputField } from "@/components/input-field";
|
||||
import { OAuth } from "@/components/oauth";
|
||||
import { icons, images } from "@/constants";
|
||||
import { fetchAPI } from "@/lib/fetch";
|
||||
import { useSession } from "@/lib/session";
|
||||
|
||||
const SignIn = () => {
|
||||
const router = useRouter();
|
||||
const { signIn, setActive, isLoaded } = useSignIn();
|
||||
const { isLoaded, setSession } = useSession();
|
||||
const [form, setForm] = useState({
|
||||
email: "",
|
||||
password: "",
|
||||
});
|
||||
|
||||
const onSignInPress = useCallback(async () => {
|
||||
if (!isLoaded) return;
|
||||
|
||||
try {
|
||||
const signInAttempt = await signIn.create({
|
||||
identifier: form.email,
|
||||
password: form.password,
|
||||
const response = await fetchAPI("/(api)/auth/login", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
email: form.email,
|
||||
password: form.password,
|
||||
}),
|
||||
});
|
||||
|
||||
if (signInAttempt.status === "complete") {
|
||||
await setActive({ session: signInAttempt.createdSessionId });
|
||||
router.replace("/");
|
||||
} else {
|
||||
Alert.alert("Error", "Invalid email or password.");
|
||||
setForm((prevForm) => ({
|
||||
...prevForm,
|
||||
password: "",
|
||||
}));
|
||||
}
|
||||
await setSession(response.data);
|
||||
router.replace("/");
|
||||
} catch (err: any) {
|
||||
Alert.alert("Error", err?.errors[0]?.longMessage);
|
||||
const status = String(err?.message ?? "");
|
||||
const message = status.includes("403")
|
||||
? "Please verify your email first."
|
||||
: status.includes("401")
|
||||
? "Invalid email or password."
|
||||
: "Could not sign in. Please try again.";
|
||||
|
||||
Alert.alert("Error", message);
|
||||
setForm((prevForm) => ({
|
||||
...prevForm,
|
||||
password: "",
|
||||
}));
|
||||
}
|
||||
}, [isLoaded, signIn, form.email, form.password, setActive, router]);
|
||||
}, [isLoaded, form.email, form.password, setSession, router]);
|
||||
|
||||
return (
|
||||
<ScrollView className="flex-1 bg-white">
|
||||
|
||||
+54
-38
@@ -1,4 +1,3 @@
|
||||
import { useSignUp } from "@clerk/clerk-expo";
|
||||
import { Link, router } from "expo-router";
|
||||
import { useState } from "react";
|
||||
import { Alert, Image, ScrollView, Text, View } from "react-native";
|
||||
@@ -9,13 +8,15 @@ import { InputField } from "@/components/input-field";
|
||||
import { OAuth } from "@/components/oauth";
|
||||
import { icons, images } from "@/constants";
|
||||
import { fetchAPI } from "@/lib/fetch";
|
||||
import { useSession } from "@/lib/session";
|
||||
|
||||
const SignUp = () => {
|
||||
const { isLoaded, signUp, setActive } = useSignUp();
|
||||
const { setSession } = useSession();
|
||||
|
||||
const [form, setForm] = useState({
|
||||
name: "",
|
||||
email: "",
|
||||
phone: "",
|
||||
password: "",
|
||||
});
|
||||
|
||||
@@ -26,18 +27,34 @@ const SignUp = () => {
|
||||
});
|
||||
|
||||
const onSignUpPress = async () => {
|
||||
if (!isLoaded) return;
|
||||
if (!form.name.trim() || !form.email.trim() || !form.password) {
|
||||
Alert.alert(
|
||||
"Missing information",
|
||||
"Please fill in your name, email and password.",
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (form.phone.trim() && !/^[0-9\s\-()+.]+$/.test(form.phone)) {
|
||||
Alert.alert(
|
||||
"Invalid phone number",
|
||||
"Enter a valid Lebanese number, e.g. 70 123 456.",
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await signUp.create({
|
||||
firstName: form.name,
|
||||
lastName: "",
|
||||
emailAddress: form.email,
|
||||
password: form.password,
|
||||
await fetchAPI("/(api)/auth/register", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
name: form.name,
|
||||
email: form.email,
|
||||
phone: form.phone.trim(),
|
||||
password: form.password,
|
||||
}),
|
||||
});
|
||||
|
||||
await signUp.prepareEmailAddressVerification({ strategy: "email_code" });
|
||||
|
||||
setVerification((prevVerification) => ({
|
||||
...prevVerification,
|
||||
state: "pending",
|
||||
@@ -52,44 +69,29 @@ const SignUp = () => {
|
||||
...prevForm,
|
||||
password: "",
|
||||
}));
|
||||
Alert.alert("Error", err?.errors[0]?.longMessage);
|
||||
Alert.alert("Error", err?.message ?? "Could not create your account.");
|
||||
}
|
||||
};
|
||||
|
||||
const onPressVerify = async () => {
|
||||
if (!isLoaded) return;
|
||||
|
||||
try {
|
||||
const completeSignUp = await signUp.attemptEmailAddressVerification({
|
||||
code: verification.code,
|
||||
const response = await fetchAPI("/(api)/auth/verify", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ email: form.email, code: verification.code }),
|
||||
});
|
||||
|
||||
if (completeSignUp.status === "complete") {
|
||||
await fetchAPI("/(api)/user", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
name: form.name,
|
||||
email: form.email,
|
||||
clerkId: completeSignUp.createdUserId,
|
||||
}),
|
||||
});
|
||||
|
||||
await setActive({ session: completeSignUp.createdSessionId });
|
||||
setVerification((prevVerification) => ({
|
||||
...prevVerification,
|
||||
state: "success",
|
||||
}));
|
||||
} else {
|
||||
setVerification((prevVerification) => ({
|
||||
...prevVerification,
|
||||
error: "Verification failed.",
|
||||
state: "failed",
|
||||
}));
|
||||
}
|
||||
await setSession(response.data);
|
||||
setVerification((prevVerification) => ({
|
||||
...prevVerification,
|
||||
state: "success",
|
||||
}));
|
||||
} catch (err: any) {
|
||||
setVerification((prevVerification) => ({
|
||||
...prevVerification,
|
||||
error: err?.errors[0]?.longMessage,
|
||||
error: err?.message?.includes("400")
|
||||
? "Invalid or expired verification code."
|
||||
: err?.message ?? "Verification failed.",
|
||||
state: "failed",
|
||||
}));
|
||||
}
|
||||
@@ -140,6 +142,20 @@ const SignUp = () => {
|
||||
keyboardType="email-address"
|
||||
/>
|
||||
|
||||
<InputField
|
||||
label="Phone (optional)"
|
||||
placeholder="70 123 456"
|
||||
icon={icons.chat}
|
||||
value={form.phone}
|
||||
onChangeText={(value) =>
|
||||
setForm((prevForm) => ({
|
||||
...prevForm,
|
||||
phone: value,
|
||||
}))
|
||||
}
|
||||
keyboardType="phone-pad"
|
||||
/>
|
||||
|
||||
<InputField
|
||||
label="Password"
|
||||
placeholder="••••••••"
|
||||
|
||||
Reference in New Issue
Block a user