Skip to content

Commit

Permalink
mobile: disallow explicitly setting login state
Browse files Browse the repository at this point in the history
Instead, screens should request for the login state to be refreshed.
This gets rid of a whole suite of bugs that occur when we set the login
state to be true when it really isn't, which causes a bunch of Appwrite
calls to fail.
  • Loading branch information
ericswpark committed Apr 20, 2024
1 parent fc785e6 commit a256eaf
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 13 deletions.
4 changes: 2 additions & 2 deletions mobile/Components/ProfileTabs/ProfileTabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function MyTabBar({ state, descriptors, navigation, position }) {
}

function ProfileTab(props) {
const { setLoggedIn } = React.useContext(LoginStateContext)!;
const { refreshLoginState } = React.useContext(LoginStateContext)!;
const { navigation } = React.useContext(ProfileContext);
const account = new Account(client);
const navigateDeleteAccount = () => {
Expand All @@ -105,7 +105,7 @@ function ProfileTab(props) {
} catch (error) {
console.log(error);
}
setLoggedIn(false);
refreshLoginState();
}

return (
Expand Down
5 changes: 1 addition & 4 deletions mobile/Providers/LoginStateProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const account = new Account(client);

export interface LoginStateContextType {
loggedIn: boolean | null;
setLoggedIn: React.Dispatch<React.SetStateAction<boolean | null>>;
refreshLoginState: () => void;
}

Expand Down Expand Up @@ -39,9 +38,7 @@ function LoginStateProvider({ children }: { children: React.ReactNode }) {
}, []);

return (
<LoginStateContext.Provider
value={{ loggedIn, setLoggedIn, refreshLoginState }}
>
<LoginStateContext.Provider value={{ loggedIn, refreshLoginState }}>
{children}
</LoginStateContext.Provider>
);
Expand Down
4 changes: 2 additions & 2 deletions mobile/Screens/CreateAccount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const CreateAccount = (props) => {
const [errorModalVisible, setErrorModalVisible] = React.useState(false);
const [errorMessage, setErrorMessage] = React.useState("");
const [loading, setLoading] = React.useState(false);
const { setLoggedIn } = React.useContext(LoginStateContext);
const { refreshLoginState } = React.useContext(LoginStateContext)!;

const handleSwitch = () => {
navigation.navigate("sign_in");
Expand Down Expand Up @@ -91,7 +91,7 @@ export const CreateAccount = (props) => {
const response = await account.create(ID.unique(), email, password, name);
await account.createEmailPasswordSession(email, password);
const userResponse = await account.get();
setLoggedIn(true);
refreshLoginState();
const check = await registerIndieID(
userResponse.$id,
20878,
Expand Down
5 changes: 2 additions & 3 deletions mobile/Screens/DeleteAccount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import Toast from "react-native-toast-message";
const account = new Account(client);

function DeleteAccount() {
const { loggedIn, setLoggedIn, refreshLoginState } =
React.useContext(LoginStateContext);
const { loggedIn, refreshLoginState } = React.useContext(LoginStateContext)!;
const [confirmed, setConfirmed] = React.useState(false);
const [deleting, setDeleting] = React.useState(false);

Expand Down Expand Up @@ -46,7 +45,7 @@ function DeleteAccount() {

if (res.ok) {
account.deleteSessions();
setLoggedIn(false);
refreshLoginState();
Toast.show({
type: "success",
text1: "Your account was successfully deleted! See you again soon!",
Expand Down
4 changes: 2 additions & 2 deletions mobile/Screens/SignIn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const SignIn = ({ navigation }: Props) => {
const [errorModalVisible, setErrorModalVisible] = React.useState(false);
const [errorMessage, setErrorMessage] = React.useState("");
const [loading, setLoading] = React.useState(false);
const { setLoggedIn } = React.useContext(LoginStateContext)!;
const { refreshLoginState } = React.useContext(LoginStateContext)!;

const handleCreateAccount = () => {
navigation.navigate("create_account");
Expand All @@ -48,7 +48,7 @@ export const SignIn = ({ navigation }: Props) => {
try {
await account.createEmailPasswordSession(email, password);
const account_res = await account.get();
setLoggedIn(true);
refreshLoginState();
const yep = await registerIndieID(account_res.$id, 20878, 'sMBFDEdTPOzXb6A2bqP169');
console.log(yep);
} catch (error) {
Expand Down

0 comments on commit a256eaf

Please sign in to comment.