upd: proses login

nb: blm selesai
This commit is contained in:
amel
2025-04-11 17:54:42 +08:00
parent 1447144e2e
commit b6b23742d2
5 changed files with 207 additions and 33 deletions

View File

@@ -0,0 +1,70 @@
import Styles from "@/constants/Styles"
import { apiCheckPhoneLogin, apiSendOtp } from "@/lib/api"
import { useState } from "react"
import { Image, Text, ToastAndroid, View } from "react-native"
import { ButtonForm } from "../buttonForm"
import { InputForm } from "../inputForm"
import ModalLoading from "../modalLoading"
type Props = {
onValidate: ({ phone, otp }: { phone: string, otp: number }) => void
}
export default function ViewLogin({ onValidate }: Props) {
const [loadingLogin, setLoadingLogin] = useState(false)
const [disableLogin, setDisableLogin] = useState(true)
const [phone, setPhone] = useState('')
const handleCheckPhone = async () => {
try {
setLoadingLogin(true)
const response = await apiCheckPhoneLogin({ phone: `62${phone}` });
if (response.success) {
const otp = Math.floor(1000 + Math.random() * 9000)
const responseOtp = await apiSendOtp({ phone: `62${phone}`, otp })
console.log(responseOtp)
// return onValidate({ phone: `62${phone}`, otp })
}
return ToastAndroid.show(response.message, ToastAndroid.SHORT)
} catch (error) {
console.error('Error fetching data:', error);
return ToastAndroid.show('Terjadi kesalahan', ToastAndroid.SHORT)
} finally {
setLoadingLogin(false)
}
};
return (
<>
<View style={Styles.wrapLogin} >
<View style={{ alignItems: "center", marginVertical: 50 }}>
<Image
source={require("../../assets/images/splash-icon.png")}
style={{ width: 130, height: 130 }}
/>
<Text style={[Styles.textSubtitle]}>PERBEKEL DARMASABA</Text>
</View>
<InputForm
onChange={(val) => {
val == "" ? setDisableLogin(true) : setDisableLogin(false)
setPhone(val)
}}
type="numeric"
placeholder="XXX-XXX-XXXX"
round
itemLeft={<Text>+62</Text>}
info="Kami akan mengirim kode verifikasi melalui WhatsApp, guna mengonfirmasikan nomor Anda." />
<ButtonForm
text="MASUK"
onPress={() => { handleCheckPhone() }}
disabled={disableLogin}
/>
</View>
{
loadingLogin && <ModalLoading isVisible={true} setVisible={setLoadingLogin} />
}
</>
)
}

View File

@@ -0,0 +1,85 @@
import Styles from "@/constants/Styles";
import { useAuthSession } from "@/providers/AuthProvider";
import CryptoES from "crypto-es";
import { useState } from "react";
import { Image, Text, View } from "react-native";
import { CodeField, Cursor, useBlurOnFulfill, useClearByFocusCell } from "react-native-confirmation-code-field";
import { ButtonForm } from "../buttonForm";
type Props = {
phone: string
otp: number
}
export default function ViewVerification({ phone, otp }: Props) {
const [value, setValue] = useState('');
const ref = useBlurOnFulfill({ value, cellCount: 4 });
const [props, getCellOnLayoutHandler] = useClearByFocusCell({
value,
setValue,
});
const { signIn } = useAuthSession();
const login = (): void => {
const random: string = 'contohLoginMobileDarmasaba';
var mytexttoEncryption = "contohLoginMobileDarmasaba"
const encrypted = CryptoES.AES.encrypt(mytexttoEncryption, "your password").toString();
// var C = require("crypto-js");
// var Decrypted = C.AES.decrypt(encrypted, "your password");
// var result = Decrypted.toString(C.enc.Utf8);
signIn(encrypted);
}
const onCheckOtp = () => {
if (value === otp.toString()) {
}
}
const resendOtp = () => {
}
return (
<>
<View style={Styles.wrapLogin} >
<View style={{ alignItems: "center", marginVertical: 50 }}>
<Image
source={require("../../assets/images/splash-icon.png")}
style={{ width: 130, height: 130 }}
/>
<Text style={[Styles.textSubtitle]}>PERBEKEL DARMASABA</Text>
</View>
<View style={[Styles.mb30]}>
<Text style={[Styles.textDefaultSemiBold]}>Verifikasi Nomor Telepon</Text>
<Text style={[Styles.textMediumNormal]}>Masukkan kode yang kami kirimkan melalui WhatsApp</Text>
<Text style={[Styles.textMediumSemiBold]}>+{phone}</Text>
</View>
<CodeField
ref={ref}
{...props}
value={value}
rootStyle={{ width: '80%', alignSelf: 'center' }}
onChangeText={setValue}
cellCount={4}
keyboardType="number-pad"
renderCell={({ index, symbol, isFocused }) => (
<Text
key={index}
style={[Styles.verificationCell, isFocused && Styles.verificationFocusCell]}
onLayout={getCellOnLayoutHandler(index)}>
{symbol || (isFocused ? <Cursor /> : null)}
</Text>
)}
/>
<ButtonForm
text="SUBMIT"
// onPress={() => { router.push("/home") }}
onPress={login}
/>
<Text style={[Styles.textInformation, Styles.mt05, Styles.cDefault, { textAlign: 'center' }]}>
Tidak Menerima kode verifikasi? Kirim Ulang
</Text>
</View>
</>
)
}