Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(login): enable login even when fcm is not enabled, but display warning #515

Merged
merged 1 commit into from
Jul 30, 2024
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
3 changes: 2 additions & 1 deletion assets/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,8 @@
"title": "Log in with your polito.it credentials",
"unsupportedUserType": "User type not supported: only students can log in to PoliTO Students.",
"usernameLabel": "Student ID",
"usernameLabelAccessibility": "Enter your username here"
"usernameLabelAccessibility": "Enter your username here",
"fcmUnsupported": "An error occurred during push notifications setup. This problem can be caused by the lack of Google Play Services on this device. It is possible to keep using this app, however push notifications won't be received."
},
"messageScreen": {
"backTitle": "Archive",
Expand Down
3 changes: 2 additions & 1 deletion assets/translations/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,8 @@
"title": "Accedi con le tue credenziali polito.it",
"unsupportedUserType": "Tipo utente non supportato: PoliTO Students è accessibile solamente agli studenti.",
"usernameLabel": "Matricola",
"usernameLabelAccessibility": "Inserisci qui il tuo username"
"usernameLabelAccessibility": "Inserisci qui il tuo username",
"fcmUnsupported": "Si è verificato un errore durante l'attivazione delle notifiche push. Questo problema può essere causato dall'assenza dei servizi Google su questo dispositivo. È possibile continuare ad utilizzare l'app ma non si riceveranno notifiche push."
},
"messageScreen": {
"backTitle": "Archivio",
Expand Down
18 changes: 16 additions & 2 deletions src/core/queries/authHooks.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Platform } from 'react-native';
import { Alert, Platform } from 'react-native';
import DeviceInfo from 'react-native-device-info';
import Keychain from 'react-native-keychain';

import { AuthApi, LoginRequest, SwitchCareerRequest } from '@polito/api-client';
import messaging from '@react-native-firebase/messaging';
import { useMutation, useQueryClient } from '@tanstack/react-query';

import { t } from 'i18next';

import { isEnvProduction } from '../../utils/env';
import { pluckData } from '../../utils/queries';
import { useApiContext } from '../contexts/ApiContext';
Expand All @@ -17,6 +19,18 @@ const useAuthClient = (): AuthApi => {
return new AuthApi();
};

async function getFcmToken(): Promise<string | undefined> {
if (!isEnvProduction) return undefined;

try {
return await messaging().getToken();
} catch (_) {
Alert.alert(t('common.error'), t('loginScreen.fcmUnsupported'));
}

return undefined;
}

export const useLogin = () => {
const authClient = useAuthClient();
const { refreshContext } = useApiContext();
Expand All @@ -32,7 +46,7 @@ export const useLogin = () => {
DeviceInfo.getManufacturer(),
DeviceInfo.getBuildNumber(),
DeviceInfo.getVersion(),
isEnvProduction ? messaging().getToken() : undefined,
getFcmToken(),
])
.then(
([
Expand Down
Loading