Advertisement
DammyHacks

Untitled

Feb 8th, 2025
52
0
364 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TypeScript 2.17 KB | Software | 0 0
  1.  
  2. `index.ts:`
  3. import { Elysia } from "elysia";
  4. import { setupLoginRoute } from "./modules/users/auth/login";
  5.  
  6. const app = new Elysia();
  7.  
  8. // Define the root route
  9. app.get("/", () => "Hello Elysia");
  10.  
  11. // Setup the login route
  12. setupLoginRoute(app);
  13.  
  14. // Start the server
  15. const server = app.listen(3000);
  16.  
  17. console.log(
  18.     `🦊 Elysia is running at ${server.server?.hostname}:${server.server?.port}, with endpoint: ${process.env.APPWRITE_ENDPOINT} - ${ process.env.APPWRITE_PROJECT_ID}`
  19. );
  20.  
  21. `login.tx:`
  22. import { Elysia, t } from 'elysia';
  23. import { account } from '../../appwrite manager/appwrite';
  24.  
  25. export function setupLoginRoute(app: Elysia) {
  26.     app.post(
  27.         '/login',
  28.         async ({ body }) => {
  29.             try {
  30.                 const { email, password } = body;
  31.  
  32.                 // Validate input
  33.                 if (!email || !password) {
  34.                     return { error: 'Username and password are required' };
  35.                 }
  36.  
  37.                    // Authenticate the user with Appwrite
  38.                 await account.createEmailPasswordSession(email, password);
  39.  
  40.                 return { message: 'Login successful', user: email };
  41.             } catch (error: any) {
  42.                 return { error: error.message || 'Invalid credentials' };
  43.             }
  44.         },
  45.         {
  46.             body: t.Object({
  47.                 email: t.String(), // Username (or email) must be a string
  48.                 password: t.String(), // Password must be a string
  49.             }),
  50.         }
  51.     );
  52. }
  53.  
  54. `appwrite.ts:`
  55. import { Client, Databases, Storage, Users, Account } from 'node-appwrite';
  56. import * as dotenv from 'dotenv';
  57.  
  58. // Load environment variables
  59. dotenv.config();
  60.  
  61. // Initialize the Appwrite client with SSL configuration
  62. const client = new Client()
  63.     .setEndpoint(process.env.APPWRITE_ENDPOINT || '')
  64.     .setProject(process.env.APPWRITE_PROJECT_ID || '')
  65.     .setKey(process.env.APPWRITE_API_KEY || '')
  66.     .setSelfSigned(true);
  67.  
  68. // Initialize Appwrite services
  69. export const databases = new Databases(client);
  70. export const storage = new Storage(client);
  71. export const users = new Users(client);
  72. export const account = new Account(client);
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement