fixed push notifications and auth modified
This commit is contained in:
@@ -6,6 +6,14 @@ function authHeader(token?: string) {
|
||||
return token ? { Authorization: `Bearer ${token}` } : {};
|
||||
}
|
||||
|
||||
let onUnauthorized: (() => void) | null = null;
|
||||
|
||||
// Called once from AuthProvider so api.tsx can trigger a logout/relogin
|
||||
// when the backend rejects a request due to a missing/expired/invalid token.
|
||||
export function setUnauthorizedHandler(handler: (() => void) | null) {
|
||||
onUnauthorized = handler;
|
||||
}
|
||||
|
||||
async function request<T>(
|
||||
method: string,
|
||||
path: string,
|
||||
@@ -30,6 +38,7 @@ async function request<T>(
|
||||
console.log('Failed URL:', res.url);
|
||||
console.log('Status:', res.status);
|
||||
console.log('Response:', errorText);
|
||||
if (res.status === 401 && token) onUnauthorized?.();
|
||||
throw new Error(errorText);
|
||||
}
|
||||
return res.json();
|
||||
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
onTokenRefresh,
|
||||
AuthorizationStatus,
|
||||
} from '@react-native-firebase/messaging';
|
||||
import notifee, { AndroidImportance } from '@notifee/react-native';
|
||||
import { post } from '@/services/api';
|
||||
|
||||
export async function registerDeviceToken(authToken: string): Promise<void> {
|
||||
@@ -32,4 +33,23 @@ export function listenForTokenRefresh(authToken: string): () => void {
|
||||
return onTokenRefresh(getMessaging(), async (newToken) => {
|
||||
await post('/device-tokens/register', { token: newToken }, authToken);
|
||||
});
|
||||
}
|
||||
|
||||
export async function displayLocalNotification(title?: string, body?: string) {
|
||||
if (!title && !body) return;
|
||||
|
||||
const channelId = await notifee.createChannel({
|
||||
id: 'default',
|
||||
name: 'General',
|
||||
importance: AndroidImportance.HIGH,
|
||||
});
|
||||
|
||||
await notifee.displayNotification({
|
||||
title,
|
||||
body,
|
||||
android: {
|
||||
channelId,
|
||||
pressAction: { id: 'default' },
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user