Checking auth status_
Learn how to check a user's authentication status in your Appwrite application and handle authentication flow appropriately.
2 min read
One of the first things your application needs to do when starting up is to check if the user is authenticated. This is an important step in creating a great user experience, as it determines whether to show login screens or protected content.
Check auth with account.get()
The recommended approach for checking authentication status is to use the account.get() method when your application starts:
import { Client, Account } from "appwrite";
const client = new Client() .setEndpoint('https://<REGION>.cloud.appwrite.io/v1') .setProject('<PROJECT_ID>');
const account = new Account(client);
// Check if user is logged inasync function checkAuthStatus() { try { // If successful, user is authenticated const user = await account.get(); console.log("User is authenticated:", user); // Proceed with your authenticated app flow return user; } catch (error) { console.error("User is not authenticated:", error); // Redirect to login page or show login UI // window.location.href = '/login'; return null; }}
// Call this function when your app initializescheckAuthStatus();Missing scope error
When a user is not authenticated and you call account.get(), you might see an error message like:
User (role: guests) missing scope (account)This error is telling you that:
- The current user has the role of "guest" (unauthenticated visitor)
- This guest user does not have the required permission scope to access account information
- This is the expected behavior when a user is not logged in
In a typical application flow:
- Call
account.get()when your app starts - If successful → User is authenticated → Show the main app UI
- If error → User is not authenticated → Redirect to login screen
Best practices
- Call
account.get()early in your application lifecycle - Handle both authenticated and unauthenticated states gracefully
- Show appropriate loading states while checking authentication
- Implement proper error handling to avoid showing error messages to users
Was this page helpful?
Share what worked or what we should fix. Once approved, our agents automatically apply suggested updates to the docs.