Fix auth: auto-create customer, display name flow, OTP auto-verify
- Backend: getOrCreateCustomer with phone fallback for re-login - Backend: PATCH /api/client/auth/profile for display name update - Client app: AuthNeedsDisplayNameData state + SetDisplayNameScreen - Client app: ApiClient.patch method - Both apps: handle verificationCompleted for auto-verify (test numbers) - Both apps: skip credential sign-in if already auto-verified - Remove debug prints from mitra auth + OTP screens - Fix ChatRequestNotifier.startListening skips when accepting Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,8 +1,18 @@
|
||||
import { authenticate } from '../../plugins/auth.js'
|
||||
import { getCustomerByFirebaseUid } from '../../services/customer.service.js'
|
||||
import { getOrCreateCustomer, getCustomerByFirebaseUid, updateCustomerDisplayName } from '../../services/customer.service.js'
|
||||
|
||||
export const clientAuthRoutes = async (app) => {
|
||||
app.post('/verify', { preHandler: authenticate }, async (request, reply) => {
|
||||
const { uid, phone_number, name } = request.firebaseUser
|
||||
const customer = await getOrCreateCustomer({
|
||||
firebase_uid: uid,
|
||||
phone: phone_number || null,
|
||||
display_name: name || null,
|
||||
})
|
||||
return reply.send({ success: true, data: customer })
|
||||
})
|
||||
|
||||
app.patch('/profile', { preHandler: authenticate }, async (request, reply) => {
|
||||
const customer = await getCustomerByFirebaseUid(request.firebaseUser.uid)
|
||||
if (!customer) {
|
||||
return reply.code(404).send({
|
||||
@@ -10,6 +20,14 @@ export const clientAuthRoutes = async (app) => {
|
||||
error: { code: 'NOT_FOUND', message: 'Customer account not found' },
|
||||
})
|
||||
}
|
||||
return reply.send({ success: true, data: customer })
|
||||
const { display_name } = request.body || {}
|
||||
if (!display_name || typeof display_name !== 'string' || display_name.trim().length === 0) {
|
||||
return reply.code(422).send({
|
||||
success: false,
|
||||
error: { code: 'VALIDATION_ERROR', message: 'display_name is required' },
|
||||
})
|
||||
}
|
||||
const updated = await updateCustomerDisplayName(customer.id, display_name.trim())
|
||||
return reply.send({ success: true, data: updated })
|
||||
})
|
||||
}
|
||||
|
||||
@@ -48,3 +48,40 @@ export const getCustomerByFirebaseUid = async (firebase_uid) => {
|
||||
`
|
||||
return customer
|
||||
}
|
||||
|
||||
export const getOrCreateCustomer = async ({ firebase_uid, phone, display_name }) => {
|
||||
// Return existing customer if already linked to this Firebase UID
|
||||
const existing = await getCustomerByFirebaseUid(firebase_uid)
|
||||
if (existing) return existing
|
||||
|
||||
// Check if a customer with this phone already exists (re-login with new Firebase UID)
|
||||
if (phone) {
|
||||
const [byPhone] = await sql`
|
||||
SELECT id, display_name, is_anonymous, phone, created_at
|
||||
FROM customers WHERE phone = ${phone}
|
||||
`
|
||||
if (byPhone) {
|
||||
// Link the new Firebase UID to the existing phone-based customer
|
||||
await sql`UPDATE customers SET firebase_uid = ${firebase_uid} WHERE id = ${byPhone.id}`
|
||||
return { ...byPhone, firebase_uid }
|
||||
}
|
||||
}
|
||||
|
||||
// Auto-create a registered (non-anonymous) customer for phone/social login
|
||||
// display_name is null — user must set it on first login
|
||||
const [customer] = await sql`
|
||||
INSERT INTO customers (firebase_uid, phone, display_name, is_anonymous)
|
||||
VALUES (${firebase_uid}, ${phone || null}, ${display_name || null}, false)
|
||||
RETURNING id, display_name, is_anonymous, phone, created_at
|
||||
`
|
||||
return customer
|
||||
}
|
||||
|
||||
export const updateCustomerDisplayName = async (customerId, displayName) => {
|
||||
const [customer] = await sql`
|
||||
UPDATE customers SET display_name = ${displayName}
|
||||
WHERE id = ${customerId}
|
||||
RETURNING id, display_name, is_anonymous, phone, created_at
|
||||
`
|
||||
return customer
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ const notifyMitra = async (mitraId, data) => {
|
||||
// Send notification to customer via WebSocket, fall back to FCM if offline
|
||||
const notifyCustomer = async (customerId, data) => {
|
||||
const sent = sendToUser(UserType.CUSTOMER, customerId, data)
|
||||
console.log(`[notifyCustomer] customerId=${customerId} type=${data.type} ws_sent=${sent}`)
|
||||
if (!sent) {
|
||||
if (data.type === WsMessage.PAIRED) {
|
||||
await sendPushNotification(UserType.CUSTOMER, customerId, {
|
||||
|
||||
Reference in New Issue
Block a user