feat: change mail and change password
This commit is contained in:
@@ -3,13 +3,13 @@ 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 { Routes } from '../../navigation'
|
||||
import { navigate } from '../../navigation/NavigationService'
|
||||
import { goBack } from '../../navigation/NavigationService'
|
||||
import { gutters, Palette } from '../../styles'
|
||||
import { FONT_FAMILY } from '../../styles/Fonts'
|
||||
import EditInput from './components/EditInput'
|
||||
@@ -17,26 +17,55 @@ 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
|
||||
|
||||
const credential = firebase.auth.EmailAuthProvider.credential(user.email, oldVal)
|
||||
await user.reauthenticateWithCredential(credential)
|
||||
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' })
|
||||
navigate(Routes.Settings)
|
||||
goBack()
|
||||
} catch (e) {
|
||||
console.log('ChangePassword error', e?.message)
|
||||
const msg = e?.message || 'Mise à jour impossible'
|
||||
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)
|
||||
@@ -45,7 +74,7 @@ const ChangePassword = () => {
|
||||
return (
|
||||
<Page
|
||||
headerType="NAVIGATE"
|
||||
title="Paramètres"
|
||||
title="Modifier mon mot de passe"
|
||||
backgroundImg={background.profileBG}
|
||||
contentContainerStyle={{
|
||||
paddingBottom: gutters * 4,
|
||||
@@ -81,8 +110,20 @@ const ChangePassword = () => {
|
||||
label="Nouveau mot de passe"
|
||||
type="password"
|
||||
value={newPassword}
|
||||
setValue={setNewPassword}
|
||||
setValue={onChangeNewPassword}
|
||||
/>
|
||||
{!!passwordError && (
|
||||
<Text
|
||||
style={{
|
||||
fontSize: 12,
|
||||
color: Palette.red,
|
||||
fontFamily: FONT_FAMILY.InterRegular,
|
||||
marginTop: 6,
|
||||
}}
|
||||
>
|
||||
{passwordError}
|
||||
</Text>
|
||||
)}
|
||||
<Pressable>
|
||||
<Text
|
||||
style={{
|
||||
@@ -103,7 +144,7 @@ const ChangePassword = () => {
|
||||
alignSelf: 'center',
|
||||
}}
|
||||
onPress={onSave}
|
||||
disabled={loading || !oldPassword?.trim() || !newPassword?.trim()}
|
||||
disabled={loading || !oldPassword?.trim() || !newPassword?.trim() || !!passwordError}
|
||||
/>
|
||||
</Page>
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user