Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- `index.ts:`
- import { Elysia } from "elysia";
- import { setupLoginRoute } from "./modules/users/auth/login";
- const app = new Elysia();
- // Define the root route
- app.get("/", () => "Hello Elysia");
- // Setup the login route
- setupLoginRoute(app);
- // Start the server
- const server = app.listen(3000);
- console.log(
- `🦊 Elysia is running at ${server.server?.hostname}:${server.server?.port}, with endpoint: ${process.env.APPWRITE_ENDPOINT} - ${ process.env.APPWRITE_PROJECT_ID}`
- );
- `login.tx:`
- import { Elysia, t } from 'elysia';
- import { account } from '../../appwrite manager/appwrite';
- export function setupLoginRoute(app: Elysia) {
- app.post(
- '/login',
- async ({ body }) => {
- try {
- const { email, password } = body;
- // Validate input
- if (!email || !password) {
- return { error: 'Username and password are required' };
- }
- // Authenticate the user with Appwrite
- await account.createEmailPasswordSession(email, password);
- return { message: 'Login successful', user: email };
- } catch (error: any) {
- return { error: error.message || 'Invalid credentials' };
- }
- },
- {
- body: t.Object({
- email: t.String(), // Username (or email) must be a string
- password: t.String(), // Password must be a string
- }),
- }
- );
- }
- `appwrite.ts:`
- import { Client, Databases, Storage, Users, Account } from 'node-appwrite';
- import * as dotenv from 'dotenv';
- // Load environment variables
- dotenv.config();
- // Initialize the Appwrite client with SSL configuration
- const client = new Client()
- .setEndpoint(process.env.APPWRITE_ENDPOINT || '')
- .setProject(process.env.APPWRITE_PROJECT_ID || '')
- .setKey(process.env.APPWRITE_API_KEY || '')
- .setSelfSigned(true);
- // Initialize Appwrite services
- export const databases = new Databases(client);
- export const storage = new Storage(client);
- export const users = new Users(client);
- export const account = new Account(client);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement