When `createEmailPasswordSession` using the react-native library, `this` is `undefined` in `sdk.js`
- 0
- Self Hosted
- Apple
I'm calling appwrite.account.createEmailPasswordSession
in my react-native app, but i keep getting
cannot read property 'client' of undefined
thrown in sdk.js
on line 1199
:
const uri = new URL(this.client.config.endpoint + apiPath);
Could someone please help me debug? I've just been following these docs: https://appwrite.io/docs/quick-starts/react-native
Full code:
appwrite-service.ts
import { Account, Client, ID } from "react-native-appwrite";
const client = new Client();
if (
!process.env.EXPO_PUBLIC_APPWRITE_ENDPOINT ||
!process.env.EXPO_PUBLIC_APPWRITE_APP_ID
) {
throw new Error("Missing environment variables");
}
client.setEndpoint(process.env.EXPO_PUBLIC_APPWRITE_ENDPOINT);
client.setProject(process.env.EXPO_PUBLIC_APPWRITE_APP_ID);
client.setPlatform("app.jordy.rozemarijn");
const account = new Account(client);
console.log(account);
export const appwrite = {
client,
account,
ID,
};
AuthContext.tsx
import {
createContext,
ReactNode,
useContext,
useEffect,
useState,
} from "react";
import { appwrite } from "../lib/appwrite-service";
import { Models } from "react-native-appwrite";
export type AuthContextValue = {
signIn: typeof appwrite.account.createEmailPasswordSession;
signUp: (
email: string,
password: string
) => ReturnType<typeof appwrite.account.create>;
signOut: typeof appwrite.account.deleteSession;
session: Models.Session | null;
isLoading: boolean;
};
const AuthContext = createContext<AuthContextValue | undefined>(undefined);
export function SessionProvider(props: { children: ReactNode }) {
const [session, setSession] = useState<Models.Session | null>(null);
const [isLoading, setIsLoading] = useState(true);
useEffect(() => {
(async () => {
try {
const session = await appwrite.account.getSession("current");
setSession(session);
} catch (error) {
console.error(error);
setSession(null);
}
setIsLoading(false);
})();
}, []);
return (
<AuthContext.Provider
value={{
signIn: appwrite.account.createEmailPasswordSession,
signUp: (email, password) =>
appwrite.account.create(appwrite.ID.unique(), email, password),
signOut: appwrite.account.deleteSession,
session,
isLoading,
}}
>
{props.children}
</AuthContext.Provider>
);
}
export function useSession() {
const value = useContext(AuthContext);
if (!value) {
throw new Error("useSession must be wrapped in a <SessionProvider />");
}
return value;
}
Moving to Supabase for now given that this was the first thing I wanted to do and it immediately failed, but hoping to try AppWrite again in the future π
Recommended threads
- Error: User (role: guests) missing scope...
I want to send a verification code to the user and the given phone number and check it and create a session right after the user entered the secret. For me that...
- Sign In With Apple OAuth Help
Hi All! I've got a flutter & appwrite app which Im trying to use sign in with apple for. I already have sign in with google working and the function is the sam...
- Realtime with multiple connections
I need the Realtime on multiple Collections for diffrent applicational logic. So my question is: Is there a way to have only 1 Websocket connection or do I need...