Auth

Appwrite is used in Patronus for handling secure user authentication and backend functionalities. This enables us to manage user sessions and provide each user with a personalized experience. Authentication ensures that only registered users can access and save flight data.

Authentication Flow

  1. Sign Up: New users can sign up by providing an email and password.

  2. Login: Registered users can log in with their email and password to access the application.

  3. Session Management: Once authenticated, Appwrite generates a session token. This session allows users to interact with protected resources without repeatedly logging in.

Steps for Implementing Authentication in Patronus

  1. Set Up Appwrite Project:

    • Log into your Appwrite console and create a new project named "Patronus."

    • Note the Project ID, as it will be required in the Next.js app setup.

  2. Configure Database and Auth Settings:

    • In the Appwrite console, enable email/password-based authentication.

    • Set up required permissions and roles (e.g., user role for general access).

  3. Add Environment Variables:

    • In your .env file, store sensitive keys and IDs to integrate Appwrite:

      APPWRITE_PROJECT_ID=<Your Project ID>
      APPWRITE_ENDPOINT=<Appwrite Server Endpoint>
    • These values are referenced in the application to connect to Appwrite.

  4. Frontend Integration (Next.js):

    • Use the Appwrite SDK to connect your Next.js app to Appwrite:

      import { Client, Account } from 'appwrite';
      
      const client = new Client();
      client.setEndpoint(process.env.APPWRITE_ENDPOINT!).setProject(process.env.APPWRITE_PROJECT_ID!);
      
      const account = new Account(client);
    • Sign Up: Call the sign-up function to create a new user.

      async function signUp(email: string, password: string) {
        try {
          await account.create('unique()', email, password);
          alert('User registered successfully!');
        } catch (error) {
          console.error(error);
        }
      }
    • Login: Log in with the user's email and password.

      async function logIn(email: string, password: string) {
        try {
          await account.createSession(email, password);
          alert('Logged in successfully!');
        } catch (error) {
          console.error(error);
        }
      }
    • Logout: Clear the user session to log out:

      async function logOut() {
        try {
          await account.deleteSession('current');
          alert('Logged out successfully!');
        } catch (error) {
          console.error(error);
        }
      }

Testing the Authentication

Run the application, and test each of the authentication functions (sign-up, log-in, log-out).

Verify that new users can create accounts, log in, and securely access data in Patronus through the Appwrite dashboard.


This guide covers the essential steps for integrating Appwrite Authentication in Patronus. Let me know if you want to add details for any other feature in your documentation!

Last updated