#[Error: User (role: guests) missing scope (account)]
26 messages · Page 1 of 1 (latest)
This is the appwrite.js file
import { Client, Account, ID, Avatars, Databases } from "react-native-appwrite";
export const config = {
endpoint: "https://cloud.appwrite.io/v1",
platform: "com.zido.flexfury",
projectId: "6662901b0020e12fba5d",
databaseId: "666293ff001d5704f3f7",
userCollectionId: "6662954c000d71580730",
storageId: "6662965c0037c6a4e833",
};
const client = new Client();
client
.setEndpoint(config.endpoint) // Your Appwrite Endpoint
.setProject(config.projectId) // Your project ID
.setPlatform(config.platform); // Your application ID or bundle ID
const account = new Account(client);
const avatars = new Avatars(client);
const databases = new Databases(client);
export const createUser = async (email, password, username) => {
try {
const newAccount = await account.create(
ID.unique(),
email,
password,
username
);
if (!newAccount) throw new Error("Account creation failed");
const avatarUrl = avatars.getInitials(username);
const session = await signIn(email, password);
if (!session) throw new Error("Sign in failed");
// Create a new document in the database for the user
const newUser = await databases.createDocument(
config.databaseId,
config.userCollectionId,
ID.unique(),
{
accountId: newAccount.$id,
email: email,
username: username,
avatar: avatarUrl,
}
);
return newUser;
} catch (error) {
console.error(error);
throw new Error(error.message);
}
};
// Sign In
export async function signIn(email, password) {
try {
// Delete any existing session for the user
await account.deleteSession("current");
// Create a new session with email and password
const session = await account.createEmailSession(email, password);
return session;
} catch (error) {
console.error(error);
throw new Error(error.message);
}
}
This is what my code to submit looks like for both sign in and sign up
const [form, setForm] = useState({
username: "",
email: "",
password: "",
});
const submit = async () => {
if (!form.username === "" || !form.email === "" || !form.password === "") {
Alert.alert("Error", "Please fill in all fields");
}
setIsSubmitting(true);
try {
const result = await createUser(form.email, form.password, form.username);
router.replace("/home");
} catch (error) {
Alert.alert("Error", error.message);
} finally {
setIsSubmitting(false);
}
};
I’d guess that you’re calling the signIn() function here? Which is failing when you try to delete the session, because there’s no active session
BTW, you can wrap code in three backticks (```) in Discord for better formatting
FYI, it's best to wrap code in backticks to format a bit nicer. You can use 1 backtick for inline code (https://www.markdownguide.org/basic-syntax/#code) and 3 backticks for multiline code (https://www.markdownguide.org/extended-syntax/#syntax-highlighting).
thanks
export const config = {
endpoint: "https://cloud.appwrite.io/v1",
platform: "com.zido.flexfury",
projectId: "6662901b0020e12fba5d",
databaseId: "666293ff001d5704f3f7",
userCollectionId: "6662954c000d71580730",
storageId: "6662965c0037c6a4e833",
};
const client = new Client();
client
.setEndpoint(config.endpoint) // Your Appwrite Endpoint
.setProject(config.projectId) // Your project ID
.setPlatform(config.platform); // Your application ID or bundle ID
const account = new Account(client);
const avatars = new Avatars(client);
const databases = new Databases(client);
export const createUser = async (email, password, username) => {
try {
const newAccount = await account.create(
ID.unique(),
email,
password,
username
);
if (!newAccount) throw new Error("Account creation failed");
const avatarUrl = avatars.getInitials(username);
const session = await signIn(email, password);
if (!session) throw new Error("Sign in failed");
// Create a new document in the database for the user
const newUser = await databases.createDocument(
config.databaseId,
config.userCollectionId,
ID.unique(),
{
accountId: newAccount.$id,
email: email,
username: username,
avatar: avatarUrl,
}
);
return newUser;
} catch (error) {
console.error(error);
throw new Error(error.message);
}
};
// Sign In
export async function signIn(email, password) {
try {
// Delete any existing session for the user
await account.deleteSession("current");
// Create a new session with email and password
const session = await account.createEmailSession(email, password);
return session;
} catch (error) {
console.error(error);
throw new Error(error.message);
}
}```
const [form, setForm] = useState({
username: "",
email: "",
password: "",
});
const submit = async () => {
if (!form.username === "" !form.email === "" !form.password === "") {
Alert.alert("Error", "Please fill in all fields");
}
setIsSubmitting(true);
try {
const result = await createUser(form.email, form.password, form.username);
router.replace("/home");
} catch (error) {
Alert.alert("Error", error.message);
} finally {
setIsSubmitting(false);
}
};
Then what did yiu recommend i do pls
Please check if that delete sessions is throwing the error
do u reccomend i remove it?
Probably, yes. That will throw an exception if someone who isn't logged in tries to log in
well, if i create a new account , according to the code it should push me to the home page.
but its nt working, and the acct will be created in my appwrite auth dashboard
The create calls sign in which calls the delete sessions and would throw an exception
so i should remove the delete session inside the signIn fuction?
Yes
You said you don't show the sign in page if they're already signed in. Maybe something is wrong there
Nah, its like this.
If you were to sign up , after clicking on the sign-up button it should push to home after creating the account.
Also
If you were to login after creating an acct, it should push to home page after clicking on the sign-in btn.
Now what i realise is that if i did remove the delete session inside the signIn function, it will work for some times, then it will now bring up this error of can not create a session when there is an active session once i now added the same removed ( delete session inside the signIn fuction) it will work.
So what did you suggest is happening and how to go about it
Its now 2 different error, one required me to remove the delete session and the other require me to add it
[Error: User (role: guests) missing scope (account)] this error occur when you try to get the current session, if no session was found this error will arise, so maybe your checking for the session before you login
Please share more of your code, particularly around the app start
Ya and this error is perfectly normal and expected if there is no session
Hello Steven,