Skip to content

Commit

Permalink
fix(izly): auth via link
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-theret committed Dec 4, 2024
1 parent 1062095 commit ac5a4cf
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 58 deletions.
6 changes: 5 additions & 1 deletion src/router/screens/settings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ const settingsScreens = [
}),

createScreen("IzlyActivation", IzlyActivation, {
headerTitle: "Configuration de la cantine",
headerTitle: "Configuration de Izly",
presentation: "modal",
headerBackVisible: false,
gestureEnabled: false,

}),

createScreen("SettingsDevLogs", SettingsDevLogs, {
Expand Down
89 changes: 32 additions & 57 deletions src/views/settings/ExternalAccount/IzlyActivation.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React, {useState} from "react";
import React, {useState, useEffect} from "react";
import type {Screen} from "@/router/helpers/types";
import {useTheme} from "@react-navigation/native";
import {SafeAreaView, useSafeAreaInsets} from "react-native-safe-area-context";
import {Keyboard, KeyboardAvoidingView, StyleSheet, TextInput, TouchableWithoutFeedback, View} from "react-native";
import {Alert, Keyboard, KeyboardAvoidingView, StyleSheet, TextInput, TouchableWithoutFeedback, View} from "react-native";
import PapillonShineBubble from "@/components/FirstInstallation/PapillonShineBubble";
import {NativeItem, NativeList, NativeListHeader, NativeText,} from "@/components/Global/NativeComponents";
import ButtonCta from "@/components/FirstInstallation/ButtonCta";
Expand All @@ -12,26 +12,39 @@ import {AccountService, IzlyAccount} from "@/stores/account/types";
import {useAccounts, useCurrentAccount} from "@/stores/account";
import uuid from "@/utils/uuid-v4";

import * as Linking from "expo-linking";

const IzlyActivation: Screen<"IzlyActivation"> = ({ navigation, route }) => {
const theme = useTheme();
const { colors } = theme;
const insets = useSafeAreaInsets();

const linkExistingExternalAccount = useCurrentAccount(store => store.linkExistingExternalAccount);
const create = useAccounts(store => store.create);

const [activationURL, setActivationURL] = useState("");
const [error, setError] = useState("");
const [loading, setLoading] = useState(false);

const secret = route.params?.password;
const username = route.params?.username;

const handleActivation = async (): Promise<void> => {
useEffect(() => {
const handleDeepLink = (event: { url: string }) => {
const url = event.url;
const scheme = url.split(":")[0];
if (scheme === "izly") {
console.log("[IzlyActivation] Activation link received:", url);
handleActivation(url);
} else {
console.log("[IzlyActivation] Ignoring link:", url);
}
};
Linking.addEventListener("url", handleDeepLink);
}, []);

const handleActivation = async (activationLink: string): Promise<void> => {
Keyboard.dismiss();
try {
const URL = await extractActivationURL(activationURL);
const { identification, configuration } = await tokenize(URL);
setLoading(true);
const { identification, configuration } = await tokenize(activationLink);

const new_account: IzlyAccount = {
service: AccountService.Izly,
Expand All @@ -52,12 +65,14 @@ const IzlyActivation: Screen<"IzlyActivation"> = ({ navigation, route }) => {

navigation.pop();
navigation.pop();
navigation.pop();
navigation.pop();
} catch (error) {
if (error instanceof Error) {
setError(error.message);
Alert.alert("Erreur", error.message);
}
else {
setError("Une erreur est survenue lors de la connexion.");
Alert.alert("Erreur", "Une erreur est survenue lors de l'activation.");
} }
finally {
setLoading(false);
Expand All @@ -79,53 +94,12 @@ const IzlyActivation: Screen<"IzlyActivation"> = ({ navigation, route }) => {
style={styles.container}
>
<PapillonShineBubble
message={"Tu viens de recevoir un lien par SMS, peux tu me l'indiquer ?"}
message={loading ? "Activation en cours..." : "Tu viens de recevoir un lien par SMS, peux tu me cliquer sur le lien pour activer ton compte ?"}
width={270}
numberOfLines={2}
numberOfLines={loading ? 1 : 3}
offsetTop={insets.top}
/>



<View
style={styles.list}
>
<View
style={{ width: "100%" }}
>
{error && (
<NativeList
style={{
backgroundColor: "#eb403422",
}}
>
<NativeItem icon={<AlertTriangle />}>
<NativeText variant="subtitle">{error}</NativeText>
</NativeItem>
</NativeList>
)}
<NativeListHeader label="URL" />
<NativeList>
<NativeItem>
<TextInput
value={activationURL}
onChangeText={setActivationURL}
onPress={() => {setError("");}}
placeholder={"https://..."}
placeholderTextColor={theme.colors.text + "55"}
keyboardType={"url"}
autoCapitalize="none"
style={{
fontSize: 16,
fontFamily: "medium",
color: theme.colors.text,
}}
/>
</NativeItem>
</NativeList>
</View>
</View>

<NativeText
style={{
width: "100%",
Expand All @@ -138,13 +112,14 @@ const IzlyActivation: Screen<"IzlyActivation"> = ({ navigation, route }) => {
Papillon ne donnera jamais vos informations d'authentification à des tiers.
</NativeText>


<View style={styles.buttons}>
<ButtonCta
primary
value="Confirmer"
value="Annuler"
disabled={loading}
onPress={() => handleActivation()}
onPress={() => (Alert.alert("Annuler", "Êtes-vous sûr de vouloir annuler l'activation ?", [
{ text: "Continer l'activation", style: "cancel" },
{ text: "Confirmer", style: "destructive", onPress: () => navigation.pop() }
]))}
/>
</View>
</SafeAreaView>
Expand Down

0 comments on commit ac5a4cf

Please sign in to comment.