Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions formulus-formplayer/src/types/FormulusInterfaceDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* If you've checked out the monorepo use:
* cp ..\formulus\src\webview\FormulusInterfaceDefinition.ts .\src\FormulusInterfaceDefinition.ts
*
* Current Version: 1.0.17
* Current Version: 1.0.18
*/

/**
Expand Down Expand Up @@ -458,9 +458,13 @@ export interface FormulusInterface {

/**
* Get information about the currently authenticated user
* @returns {Promise<{username: string, displayName?: string}>} User information
* @returns {Promise<{username: string, displayName?: string, role?: 'read-only' | 'read-write' | 'admin'}>} User information including role
*/
getCurrentUser(): Promise<{ username: string; displayName?: string }>;
getCurrentUser(): Promise<{
username: string;
displayName?: string;
role?: 'read-only' | 'read-write' | 'admin';
}>;
}

/**
Expand Down
10 changes: 7 additions & 3 deletions formulus/src/webview/FormulusInterfaceDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* If you've checked out the monorepo use:
* cp ..\formulus\src\webview\FormulusInterfaceDefinition.ts .\src\FormulusInterfaceDefinition.ts
*
* Current Version: 1.0.17
* Current Version: 1.0.18
*/

/**
Expand Down Expand Up @@ -458,9 +458,13 @@ export interface FormulusInterface {

/**
* Get information about the currently authenticated user
* @returns {Promise<{username: string, displayName?: string}>} User information
* @returns {Promise<{username: string, displayName?: string, role?: 'read-only' | 'read-write' | 'admin'}>} User information including role
*/
getCurrentUser(): Promise<{ username: string; displayName?: string }>;
getCurrentUser(): Promise<{
username: string;
displayName?: string;
role?: 'read-only' | 'read-write' | 'admin';
}>;
}

/**
Expand Down
36 changes: 30 additions & 6 deletions formulus/src/webview/FormulusMessageHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { GeolocationService } from '../services/GeolocationService';
import { WebViewMessageEvent, WebView } from 'react-native-webview';
import RNFS from 'react-native-fs';
import * as Keychain from 'react-native-keychain';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { Alert } from 'react-native';
import * as ImagePicker from 'react-native-image-picker';
import {
Expand Down Expand Up @@ -962,17 +963,40 @@ export function createFormulusMessageHandlers(): FormulusMessageHandlers {
onGetCurrentUser: async (): Promise<{
username: string;
displayName?: string;
role?: 'read-only' | 'read-write' | 'admin';
}> => {
try {
const credentials = await Keychain.getGenericPassword();
if (credentials) {
return {
username: credentials.username,
displayName: credentials.username,
};
} else {
if (!credentials) {
throw new Error('No user credentials found');
}

// Retrieve role from stored user info (set during login)
let role: 'read-only' | 'read-write' | 'admin' | undefined;
try {
const userJson = await AsyncStorage.getItem('@user');
if (userJson) {
const userInfo = JSON.parse(userJson);
if (
userInfo.role === 'admin' ||
userInfo.role === 'read-write' ||
userInfo.role === 'read-only'
) {
role = userInfo.role;
}
}
} catch (roleError) {
console.warn(
'FormulusMessageHandlers: Failed to retrieve user role:',
roleError,
);
}

return {
username: credentials.username,
displayName: credentials.username,
role,
};
} catch (error) {
console.error(
'FormulusMessageHandlers: Failed to get current user:',
Expand Down
6 changes: 5 additions & 1 deletion formulus/src/webview/FormulusMessageHandlers.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ export interface FormulusMessageHandlers {
whereClause?: string | null;
}) => Promise<Observation[]>;
onOpenFormplayer?: (data: FormInitData) => Promise<FormCompletionResult>;
onGetCurrentUser?: () => Promise<{ username: string; displayName?: string }>;
onGetCurrentUser?: () => Promise<{
username: string;
displayName?: string;
role?: 'read-only' | 'read-write' | 'admin';
}>;
// Called when the Formplayer WebView signals that it has completed initialization
// via a `formplayerInitialized` message. Primarily used for logging/diagnostics.
onFormplayerInitialized?: (data: {
Expand Down
Loading