|
| 1 | +--- |
| 2 | +layout: article |
| 3 | +title: Checking auth status |
| 4 | +description: Learn how to check a user's authentication status in your Appwrite application and handle authentication flow appropriately. |
| 5 | +--- |
| 6 | + |
| 7 | +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. |
| 8 | + |
| 9 | +# Check auth with `account.get()` |
| 10 | + |
| 11 | +The recommended approach for checking authentication status is to use the `account.get()` method when your application starts: |
| 12 | + |
| 13 | +{% multicode %} |
| 14 | +```client-web |
| 15 | +import { Client, Account } from "appwrite"; |
| 16 | + |
| 17 | +const client = new Client() |
| 18 | + .setEndpoint('https://<REGION>.cloud.appwrite.io/v1') |
| 19 | + .setProject('<PROJECT_ID>'); |
| 20 | + |
| 21 | +const account = new Account(client); |
| 22 | + |
| 23 | +// Check if user is logged in |
| 24 | +async function checkAuthStatus() { |
| 25 | + try { |
| 26 | + // If successful, user is authenticated |
| 27 | + const user = await account.get(); |
| 28 | + console.log("User is authenticated:", user); |
| 29 | + // Proceed with your authenticated app flow |
| 30 | + return user; |
| 31 | + } catch (error) { |
| 32 | + console.error("User is not authenticated:", error); |
| 33 | + // Redirect to login page or show login UI |
| 34 | + // window.location.href = '/login'; |
| 35 | + return null; |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +// Call this function when your app initializes |
| 40 | +checkAuthStatus(); |
| 41 | +``` |
| 42 | +```client-flutter |
| 43 | +import 'package:appwrite/appwrite.dart'; |
| 44 | + |
| 45 | +void checkAuthStatus() async { |
| 46 | + final client = Client() |
| 47 | + .setEndpoint('https://<REGION>.cloud.appwrite.io/v1') |
| 48 | + .setProject('<PROJECT_ID>'); |
| 49 | + |
| 50 | + final account = Account(client); |
| 51 | + |
| 52 | + try { |
| 53 | + // If successful, user is authenticated |
| 54 | + final user = await account.get(); |
| 55 | + print('User is authenticated: ${user.name}'); |
| 56 | + // Proceed with your authenticated app flow |
| 57 | + } catch (e) { |
| 58 | + print('User is not authenticated: $e'); |
| 59 | + // Redirect to login page or show login UI |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// Call this function when your app initializes |
| 64 | +``` |
| 65 | +```client-android-kotlin |
| 66 | +import io.appwrite.Client |
| 67 | +import io.appwrite.services.Account |
| 68 | +import io.appwrite.exceptions.AppwriteException |
| 69 | + |
| 70 | +class AuthManager { |
| 71 | + private val client = Client(context) |
| 72 | + .setEndpoint("https://<REGION>.cloud.appwrite.io/v1") |
| 73 | + .setProject("<PROJECT_ID>") |
| 74 | + |
| 75 | + private val account = Account(client) |
| 76 | + |
| 77 | + suspend fun checkAuthStatus(): Boolean { |
| 78 | + return try { |
| 79 | + val user = account.get() |
| 80 | + Log.d("Auth", "User is authenticated: ${user.name}") |
| 81 | + // Proceed with your authenticated app flow |
| 82 | + true |
| 83 | + } catch (e: AppwriteException) { |
| 84 | + Log.e("Auth", "User is not authenticated: ${e.message}") |
| 85 | + // Redirect to login page or show login UI |
| 86 | + false |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +// Call this function when your app initializes |
| 92 | +``` |
| 93 | +```client-apple |
| 94 | +import Appwrite |
| 95 | + |
| 96 | +func checkAuthStatus() { |
| 97 | + let client = Client() |
| 98 | + .setEndpoint("https://<REGION>.cloud.appwrite.io/v1") |
| 99 | + .setProject("<PROJECT_ID>") |
| 100 | + |
| 101 | + let account = Account(client) |
| 102 | + |
| 103 | + Task { |
| 104 | + do { |
| 105 | + // If successful, user is authenticated |
| 106 | + let user = try await account.get() |
| 107 | + print("User is authenticated: \(user.name)") |
| 108 | + // Proceed with your authenticated app flow |
| 109 | + } catch { |
| 110 | + print("User is not authenticated: \(error)") |
| 111 | + // Redirect to login page or show login UI |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +// Call this function when your app initializes |
| 117 | +``` |
| 118 | +{% /multicode %} |
| 119 | + |
| 120 | +# Missing scope error |
| 121 | + |
| 122 | +When a user is not authenticated and you call `account.get()`, you might see an error message like: |
| 123 | + |
| 124 | +``` |
| 125 | +User (role: guests) missing scope (account) |
| 126 | +``` |
| 127 | + |
| 128 | +This error is telling you that: |
| 129 | +1. The current user has the role of "guest" (unauthenticated visitor) |
| 130 | +2. This guest user does not have the required permission scope to access account information |
| 131 | +3. This is the expected behavior when a user is not logged in |
| 132 | + |
| 133 | +{% info title="Authentication flow" %} |
| 134 | +In a typical application flow: |
| 135 | + |
| 136 | +1. Call `account.get()` when your app starts |
| 137 | +2. If successful → User is authenticated → Show the main app UI |
| 138 | +3. If error → User is not authenticated → Redirect to login screen |
| 139 | +{% /info %} |
| 140 | + |
| 141 | +# Best practices |
| 142 | + |
| 143 | +- Call `account.get()` early in your application lifecycle |
| 144 | +- Handle both authenticated and unauthenticated states gracefully |
| 145 | +- Show appropriate loading states while checking authentication |
| 146 | +- Implement proper error handling to avoid showing error messages to users |
0 commit comments