Back

User (role: guests) missing scope (account)

  • 1
  • Users
  • Accounts
  • Web
paris
23 Feb, 2024, 05:11

I am getting this error when I am trying to see if a User is currently logged into a session. I am using Next14 with Typescript.

I am using client-side functions for both the Account creation and the SignUp page. (If the user is signed in already and goes to sign up, it should just route them to their account page.)

TypeScript
// inside of my SignUp page.tsx
const getSession = () => {
    // this is where the error is getting thrown
    account.get().then((res) => {
      setSession(res);
    }).catch((e) => {
      console.log(e);
    })
  }

  useEffect(() => {
    getSession();

    if (session) {
      router.push("/my-account")
    }
  }, []);

I am a little confused on how the account/user scopes work when youre trying to check for a logged in user that is not logged in?

TL;DR
Developers are having issues with missing scope (account) while trying to check for a logged-in user using client-side functions. To resolve, modify the logic to handle the user session retrieval and routing as shown in the provided code snippets. Be sure to handle errors and set the user to null if necessary.
Steven
23 Feb, 2024, 05:12

Please don't tag people as it can be disruptive. Please just wait

paris
23 Feb, 2024, 05:12

apologies!

paris
23 Feb, 2024, 05:15
TypeScript
  async function init() {
    try {
      const loggedIn = await account.get();
      setUser(loggedIn);
    } catch (err) {
      setUser(null);
    }
  }
paris
23 Feb, 2024, 05:15

according to the own docs, you just catch the error and set the user to null if there is an error?

paris
23 Feb, 2024, 05:15

strange

Steven
23 Feb, 2024, 05:15

No worries!

paris
23 Feb, 2024, 05:33
TypeScript
  const [user, setUser] = useState<Models.User<Models.Preferences> | null>(null);
  const [session, setSession] = useState<Models.Session>();

  const router = useRouter();

  const handleSignUp = () => {
    setLoading(true);
    signUp(email, password).then((res) => {
      setError("");
      setLoading(false);
      account.createEmailPasswordSession(email, password).then((sesh) => {
        setSession(sesh);
      })
      router.push("/my-account")
    }).catch((e: AppwriteException) => {
      setError(e.message);
      setLoading(false);
    });
  }

  const getUser = async () => {
    getAccount().then((res) => {
      setUser(res);
    }).catch((e) => {
      setUser(null);
    })
  }

  useEffect(() => {
    getUser().then(() => {
      if (user) {
        router.push("/my-account");
      }
    });
  }, [user]);
paris
23 Feb, 2024, 05:33

for anyone who comes against this problem in the future, this seems to work

paris
23 Feb, 2024, 05:33

i cant say as to why the design works like this

paris
23 Feb, 2024, 05:47
TypeScript
const [email, setEmail] = useState("");
  const [password, setPassword] = useState("");
  const [error, setError] = useState("");
  const [loading, setLoading] = useState(false);
  const [session, setSession] = useState<Models.Session>();

  const router = useRouter();

  const handleSignUp = () => {
    setLoading(true);
    signUp(email, password).then((res) => {
      setError("");
      setLoading(false);
      account.createEmailPasswordSession(email, password).then((sesh) => {
        setSession(sesh);
      })
      router.push("/my-account");
    }).catch((e: AppwriteException) => {
      setError(e.message);
      setLoading(false);
    });
  }

  useEffect(() => {
    getAccount().then((res) => {
      router.push('/my-account')
    }).catch((e) => {
      // do nothing
    });
  }, []);
paris
23 Feb, 2024, 05:47

Even shorter version

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more