Skip to content

Commit

Permalink
change passsword done
Browse files Browse the repository at this point in the history
  • Loading branch information
milon27 committed Jul 24, 2021
1 parent dcfca8e commit 2bf9d02
Show file tree
Hide file tree
Showing 15 changed files with 251 additions and 29 deletions.
8 changes: 5 additions & 3 deletions src/components/layouts/form/Input.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import Icon from './../icon/Icon';

/**
*
* @param {{ref,type,returnKeyType,returnKeyLabel,label,icon,error,style,onSubmitEditing,nextRef}} props
* @param {{ref,type,returnKeyType,returnKeyLabel,label,icon,error,style,nextRef}} props
*/
function Input({
function RnInput({
type = "default",
returnKeyType = "done",
returnKeyLabel = "done",
Expand Down Expand Up @@ -73,7 +73,9 @@ function Input({
)
}

export default React.forwardRef(Input)
const Input = React.forwardRef(RnInput)

export default Input

const styles = StyleSheet.create({
container: {
Expand Down
2 changes: 1 addition & 1 deletion src/components/layouts/modal/AreYouSureAlert.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default function AreYouSureAlert(msg, dothis) {
text: "Yes",
onPress: () => {
dothis()
console.log("okkkk");
// console.log("okkkk");
},
},
// The "No" button
Expand Down
23 changes: 22 additions & 1 deletion src/components/layouts/modal/PasswordModal.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useContext, useState } from 'react'
import React, { useContext, useState, useEffect } from 'react'
import { View, Text, StyleSheet } from 'react-native'
import MModal from './MModal'
import Theme from './../../../utils/helpers/Theme';
Expand All @@ -7,11 +7,31 @@ import { DispatchContext } from './../../../utils/context/MainContext';
import { useNavigation } from '@react-navigation/native';
import URL from './../../../utils/helpers/URL';
import AreYouSureAlert from './AreYouSureAlert';
import Helper from './../../../utils/helpers/Helper';
import { useIsFocused } from '@react-navigation/native';


export default function PasswordModal({ open, setOpen, item }) {
const nav = useNavigation()
const { passDispatch } = useContext(DispatchContext)
const isFocused = useIsFocused();
//local
const [pass, setPass] = useState("")

useEffect(() => {
const load = async () => {
try {
const password = await Helper.decryptPass(item?.password)
setPass(password)
} catch (e) {
console.log("model: ", e);
}
}
if (isFocused) {
load()
}

}, [isFocused])

const onSubmit = () => {
setOpen(false)
Expand All @@ -31,6 +51,7 @@ export default function PasswordModal({ open, setOpen, item }) {
<Text style={styles.text}>URL: {item?.url} </Text>
<Text style={styles.text}>Username: {item?.username} </Text>
<Text style={styles.text}>Email: {item?.email}</Text>
<Text selectable={true} style={styles.text}>Password: {pass}</Text>
</MModal>
)
}
Expand Down
4 changes: 1 addition & 3 deletions src/components/navigation/AppNavContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ export default function AppNavContainer() {
if (ck) {
//ck in localstorage
const u = await Helper.getUser()
//console.log("test: ", u)
//const u2 = await Helper.getPass()
//console.log("passsss:", u2)
// console.log("test: ", u)
if (!u) {
//not logged in
await authAc.Logout()
Expand Down
2 changes: 2 additions & 0 deletions src/components/navigation/DashStackNav.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import DefineIcon from '../layouts/icon/DefineIcon';
import CreateCategory from './../screens/dashboard/CreateCategory';
import Fav from './../screens/favourite/Fav';
import AllCat from './../screens/category/AllCat';
import ChangePassword from './../screens/password/ChangePassword';

const dashStack = createStackNavigator()
/**
Expand Down Expand Up @@ -57,6 +58,7 @@ export default function DashStackNav() {
<dashStack.Screen name={URL.ALL_CATEGORY} component={AllCat} />
<dashStack.Screen name={URL.CREATE_CATEGORY} component={CreateCategory} />
<dashStack.Screen name={URL.FAV_PASSWORD} component={Fav} />
<dashStack.Screen name={URL.CHANGE_PASSWORD} component={ChangePassword} />
<dashStack.Screen name={URL.ABOUT} component={About} />
<dashStack.Screen name={URL.SIGN_IN} component={SignIn} />
</dashStack.Navigator >
Expand Down
5 changes: 3 additions & 2 deletions src/components/navigation/DrawerContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ export default function DrawerContent({ navigation: nav }) {
const items = [
{ title: "Home", icon: <Icon type={DefineIcon.Feather} name="home" />, onPress: () => { nav.navigate(URL.HOME_SCREEN) } },
{ title: "Create Password", icon: <Icon type={DefineIcon.Feather} name="lock" />, onPress: () => { nav.navigate(URL.CREATE_PASSWORD) } },
{ title: "All Category", icon: <Icon type={DefineIcon.Feather} name="list" />, onPress: () => { nav.navigate(URL.ALL_CATEGORY) } },
{ title: "Create Category", icon: <Icon type={DefineIcon.Feather} name="paperclip" />, onPress: () => { nav.navigate(URL.CREATE_CATEGORY) } },
{ title: "All Category", icon: <Icon type={DefineIcon.Feather} name="paperclip" />, onPress: () => { nav.navigate(URL.ALL_CATEGORY) } },
{ title: "Create Category", icon: <Icon type={DefineIcon.MaterialIcon} name="playlist-add" />, onPress: () => { nav.navigate(URL.CREATE_CATEGORY) } },
{ title: "Change Password", icon: <Icon type={DefineIcon.MaterialCommunityIcon} name="circle-edit-outline" />, onPress: () => { nav.navigate(URL.CHANGE_PASSWORD) } },
{ title: "About", icon: <Icon type={DefineIcon.AntDesign} name="customerservice" />, onPress: () => { nav.navigate(URL.ABOUT) } },
{ title: "Log Out", icon: <Icon type={DefineIcon.Feather} name="log-out" />, onPress: logoutNow }
]
Expand Down
6 changes: 6 additions & 0 deletions src/components/screens/dashboard/About.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ export default function About() {
<Container style={{ justifyContent: "center" }}>
<Text selectable={true} selectionColor='orange' style={{ color: Theme.COLOR_BLACK, textAlign: "center" }}>Developed By: milon27 </Text>
<Text onPress={onpress} selectable={true} selectionColor='orange' style={{ color: Theme.COLOR_BLACK, textAlign: "center" }}>Visit: https://milon27.web.app/ </Text>

<Text selectable={true} selectionColor='orange' style={{ color: Theme.COLOR_BLACK, textAlign: "center", marginTop: 30 }}>

We use 256-bit AES encryption to protect the contents,
Your data is encrypted and decrypted at the device level. Data stored in our server is kept secret, even from us. Your master password, and the keys used to encrypt and decrypt data, are never sent to our servers, and are never accessible by us.
</Text>
</Container>
)
}
7 changes: 3 additions & 4 deletions src/components/screens/dashboard/HomeScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ import { DispatchContext, StateContext } from './../../../utils/context/MainCont
import ListAction from './../../../utils/context/actions/ListAction';
import Helper from './../../../utils/helpers/Helper';
import AppAction from './../../../utils/context/actions/AppAction';
import { useIsFocused } from "@react-navigation/native";
import Icon from './../../layouts/icon/Icon';
import DefineIcon from './../../layouts/icon/DefineIcon';
import { useNavigation } from '@react-navigation/native';
import { useIsFocused, useNavigation } from '@react-navigation/native';
import URL from './../../../utils/helpers/URL';
import { StatusBar } from 'expo-status-bar';
export default function HomeScreen() {
Expand Down Expand Up @@ -41,8 +40,8 @@ export default function HomeScreen() {
title: "All"
}, ...data.object]
})
//set all_cat as id
setSelect(cat[0]?._id)
Helper.encryptPass("")
}
} catch (e) {
console.log("error HomeScreen.js->", e);
Expand Down Expand Up @@ -94,7 +93,7 @@ export default function HomeScreen() {
}

const renderCategory = ({ item }) => {
return <SingleCat setSelect={setSelect} item={item} />
return <SingleCat setSelect={setSelect} item={item} select={select} />
}

return (
Expand Down
6 changes: 3 additions & 3 deletions src/components/screens/dashboard/SingleCat.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { StyleSheet, Image, Text, View, TouchableOpacity, Linking } from 'react-
import Theme from './../../../utils/helpers/Theme';
import Helper from './../../../utils/helpers/Helper';

export default function SingleCat({ item, setSelect }) {
export default function SingleCat({ item, setSelect, select }) {

return <View style={styles.item_container}>
return <View style={select === item?._id ? [styles.item_container, { backgroundColor: Theme.COLOR_PRIMARY }] : styles.item_container}>
<TouchableOpacity onPress={() => {
if (item.title !== "No Category")
setSelect(item?._id)
Expand All @@ -24,7 +24,7 @@ const styles = StyleSheet.create({
paddingHorizontal: 15,
paddingVertical: 8,
marginHorizontal: 5,
backgroundColor: Theme.COLOR_PRIMARY,
backgroundColor: Theme.COLOR_GRAY,
borderRadius: 30,
},

Expand Down
4 changes: 3 additions & 1 deletion src/components/screens/favourite/Fav.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useContext, useEffect } from 'react'
import React, { useContext, useEffect, useState } from 'react'
import { View, Text, ActivityIndicator, FlatList } from 'react-native'
import { useIsFocused } from "@react-navigation/native";
import { StateContext, DispatchContext } from './../../../utils/context/MainContext';
Expand All @@ -15,6 +15,8 @@ export default function Fav() {
const { app, pass } = useContext(StateContext)
const { appDispatch, passDispatch } = useContext(DispatchContext)



useEffect(() => {
// console.log("load password")
const listAc = new ListAction(passDispatch)
Expand Down
170 changes: 170 additions & 0 deletions src/components/screens/password/ChangePassword.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
import React, { useRef, useState, useContext, useEffect } from 'react'
import { View, Text } from 'react-native'
import Input from '../../layouts/form/Input';
import Container from './../../layouts/Container';
import Helper from './../../../utils/helpers/Helper';
import Icon from './../../layouts/icon/Icon';
import DefineIcon from './../../layouts/icon/DefineIcon';
import MButton from './../../layouts/form/MButton';
import { DispatchContext, StateContext } from './../../../utils/context/MainContext';
import Theme from './../../../utils/helpers/Theme';
import AppAction from './../../../utils/context/actions/AppAction';
import ListAction from './../../../utils/context/actions/ListAction';
import { useIsFocused } from '@react-navigation/native';
import AxiosHelper from './../../../utils/helpers/AxiosHelper';

export default function ChangePassword() {
const isFocused = useIsFocused();
//context
const { app, pass } = useContext(StateContext)
const { appDispatch, passDispatch } = useContext(DispatchContext)
//ref
const oldRef = useRef(null)
const newRef = useRef(null)
const newCRef = useRef(null)
//constants
const N_OLD_PASSWORD = "old_password"
const N_NEW_PASSWORD = "new_password"
const N_NEW_C_PASSWORD = "new_c_password"

const initValue = {
[N_OLD_PASSWORD]: "",
[N_NEW_PASSWORD]: "",
[N_NEW_C_PASSWORD]: "",
}
//state
const [uid, setUid] = useState("")
const [input, setInput] = useState(initValue)
const [error, setError] = useState(initValue)
//effect
useEffect(() => {
// console.log("load password")
const listAc = new ListAction(passDispatch)
const token = listAc.getSource()
const load = async () => {

try {
//load password..
const uid = await Helper.getUserID()
setUid(uid)
const val = await listAc.getAll(`pass/get-all/${uid}/`, [])
//console.log("change pass val =", val)
} catch (e) {
console.log("error ChangePassword.js->", e);
}
}
if (isFocused) {
load()
}

return () => {
token.cancel()
}
}, [isFocused, pass.length])
//method
const onSubmit = async () => {

//validation
const errorArray = Helper.validateObject({
[N_OLD_PASSWORD]: input[N_OLD_PASSWORD],
[N_NEW_PASSWORD]: input[N_NEW_PASSWORD],
[N_NEW_C_PASSWORD]: input[N_NEW_C_PASSWORD]
})
errorArray.forEach(item => {
return setError((pre) => ({ ...pre, [item[0]]: `It's Required.` }))
})
if (errorArray.length > 0) {
return
}
//check the old password is ok or not
const oldpasshash = await Helper.getPassHash()
if (Helper.getMasterPassHash(input[N_OLD_PASSWORD]) !== oldpasshash) {
setError((pre) => ({ ...pre, [N_OLD_PASSWORD]: `Wrong Old Password` }))
return
}
//ck the confirm pass & pass is same or not
if (input[N_NEW_PASSWORD] !== input[N_NEW_C_PASSWORD]) {
setError((pre) => ({ ...pre, [N_NEW_C_PASSWORD]: `Confirm Password is not same.` }))
return
}

//decrypt all the passsword using the old pass
//encrypt again with new password
const resultArray = await Promise.all(
pass.map(async (item) => {
const plainpass = await Helper.decryptPass(item.password)
const reEnc = Helper.encryptPassWithHas(plainpass, Helper.getMasterPassHash(input[N_NEW_PASSWORD]))

item.password = reEnc
return item
})
);

//console.log(resultArray);

let appac = new AppAction(appDispatch)

appac.START_LOADING()

const data = {
arr: resultArray,
user: {
_id: uid,
pass: input[N_NEW_PASSWORD]
}
}

let val = await AxiosHelper.updateData(`pass/rest/all-pass`, data)
console.log("after all update: ", val)

appac.STOP_LOADING()

if (val.success) {
//update offline
await Helper.setPassHash(input[N_NEW_PASSWORD])
setInput(initValue)
Helper.Toast("" + val.title)
} else {
Helper.Toast("" + val.title)
}

}

return (
<Container>
<Input
ref={oldRef}
nextRef={newRef}
autoCapitalize={'none'}
value={input[N_OLD_PASSWORD]}
error={error[N_OLD_PASSWORD]}
icon={<Icon type={DefineIcon.Feather} size={17} name="lock" />}
label="Enter Old Password"
onChangeText={(text) => Helper.onChange({ name: N_OLD_PASSWORD, value: text, setInput, setError })}
/>

<Input
ref={newRef}
nextRef={newCRef}
autoCapitalize={'none'}
value={input[N_NEW_PASSWORD]}
error={error[N_NEW_PASSWORD]}
icon={<Icon type={DefineIcon.Feather} size={17} name="lock" />}
label="Enter New Password"
onChangeText={(text) => Helper.onChange({ name: N_NEW_PASSWORD, value: text, setInput, setError })}
/>

<Input
ref={newCRef}
autoCapitalize={'none'}
value={input[N_NEW_C_PASSWORD]}
error={error[N_NEW_C_PASSWORD]}
icon={<Icon type={DefineIcon.Feather} size={17} name="lock" />}
label="Confirm New Password"
onChangeText={(text) => Helper.onChange({ name: N_NEW_C_PASSWORD, value: text, setInput, setError })}
/>

<MButton style={{ elevation: 0 }} title="Change Master Password" loading={app?.loading} color={Theme.COLOR_PRIMARY} onPress={onSubmit} disabled={app?.loading} />
</Container>
)
}
Loading

0 comments on commit 2bf9d02

Please sign in to comment.