Skip to content

Commit

Permalink
fix(login): enable login even when fcm is not enabled, but display wa…
Browse files Browse the repository at this point in the history
…rning (#515)

fixes: #513
  • Loading branch information
QcFe authored Jul 30, 2024
1 parent 7c84dd0 commit 7db8d40
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
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

0 comments on commit 7db8d40

Please sign in to comment.