import { BlurView } from 'expo-blur' import React, { useState } from 'react' import { Platform, Pressable, Text, View } from 'react-native' import { responsiveHeight } from 'react-native-responsive-dimensions' import { useGlobal } from 'reactn' import { checkIfPasswordIsStrongEnough } from '../../actions/signupActions' import { background } from '../../assets' import GradientButton from '../../components/GradientButton' import firebase from '../../config/firebase' import { isWeb } from '../../hooks/useLayoutType' import Page from '../../layouts/Page' import { goBack } from '../../navigation/NavigationService' import { gutters, Palette } from '../../styles' import { FONT_FAMILY } from '../../styles/Fonts' import EditInput from './components/EditInput' const ChangePassword = () => { const [oldPassword, setOldPassword] = useState('') const [newPassword, setNewPassword] = useState('') const [passwordError, setPasswordError] = useState('') const [loading, setLoading] = useState(false) const [, setTooltip] = useGlobal('_tooltip') const onChangeNewPassword = (val) => { setNewPassword(val) if (!val) { setPasswordError('') return } if (!checkIfPasswordIsStrongEnough({ password: val })) { setPasswordError( 'Le mot de passe doit contenir au moins 6 caractères, une majuscule et un chiffre.' ) } else { setPasswordError('') } } const onSave = async () => { const oldVal = (oldPassword || '').trim() const newVal = (newPassword || '').trim() if (!oldVal || !newVal) return if (passwordError) return try { setLoading(true) const user = firebase.auth().currentUser if (!user?.email) return try { const credential = firebase.auth.EmailAuthProvider.credential(user.email, oldVal) await user.reauthenticateWithCredential(credential) } catch (reauthErr) { throw { code: 'auth/wrong-password', message: 'Mot de passe actuel incorrect.', } } await user.updatePassword(newVal) setTooltip({ type: 'success', text: 'Mot de passe mis à jour' }) goBack() } catch (e) { console.log('ChangePassword error', e?.message) const msg = e?.code === 'auth/wrong-password' ? 'Mot de passe actuel incorrect.' : e?.message || 'Mise à jour impossible' setTooltip({ type: 'error', text: msg }) } finally { setLoading(false) } } return ( {!!passwordError && ( {passwordError} )} Mot de passe oublié? ) } export default ChangePassword