mirror of
https://github.com/memohai/Memoh.git
synced 2026-04-25 07:00:48 +09:00
fix: login
This commit is contained in:
@@ -76,11 +76,11 @@ export const validateUser = async (username: string, password: string) => {
|
|||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查询数据库中的用户
|
// 查询数据库中的用户(使用 username 而不是 id)
|
||||||
const [user] = await db
|
const [user] = await db
|
||||||
.select()
|
.select()
|
||||||
.from(users)
|
.from(users)
|
||||||
.where(eq(users.id, userId!))
|
.where(eq(users.username, username))
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
return null
|
return null
|
||||||
|
|||||||
@@ -7,11 +7,13 @@ import { createClient as createClientApi } from '@memohome/api/client'
|
|||||||
*/
|
*/
|
||||||
export function createClient(context?: MemoHomeContext) {
|
export function createClient(context?: MemoHomeContext) {
|
||||||
const ctx = context || getContext()
|
const ctx = context || getContext()
|
||||||
const storage = ctx.storage
|
const storage = ctx.storage
|
||||||
|
|
||||||
|
|
||||||
const apiUrlResult = typeof storage.getApiUrl === 'function'
|
const apiUrlResult = typeof storage.getApiUrl === 'function'
|
||||||
? storage.getApiUrl()
|
? storage.getApiUrl()
|
||||||
: (storage as unknown as Record<string, string>).apiUrl
|
: (storage as unknown as Record<string, string>).apiUrl
|
||||||
|
|
||||||
|
|
||||||
if (apiUrlResult instanceof Promise) {
|
if (apiUrlResult instanceof Promise) {
|
||||||
throw new Error('createClient does not support async storage. Use createClientAsync instead.')
|
throw new Error('createClient does not support async storage. Use createClientAsync instead.')
|
||||||
@@ -23,13 +25,16 @@ export function createClient(context?: MemoHomeContext) {
|
|||||||
? storage.getToken(ctx.currentUserId)
|
? storage.getToken(ctx.currentUserId)
|
||||||
: null
|
: null
|
||||||
|
|
||||||
|
|
||||||
// Handle async token retrieval
|
// Handle async token retrieval
|
||||||
if (token instanceof Promise) {
|
if (token instanceof Promise) {
|
||||||
throw new Error('createClient does not support async token storage. Use createClientAsync instead.')
|
throw new Error('createClient does not support async token storage. Use createClientAsync instead.')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const client = createClientApi(apiUrl, token ?? undefined)
|
const client = createClientApi(apiUrl, token ?? undefined)
|
||||||
|
|
||||||
|
|
||||||
return client
|
return client
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ export async function handleLogin(ctx: Context) {
|
|||||||
await ctx.reply('❌ Unable to identify user')
|
await ctx.reply('❌ Unable to identify user')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
console.log('telegramUserId', telegramUserId)
|
|
||||||
|
|
||||||
// Parse command arguments
|
// Parse command arguments
|
||||||
const args = ctx.message && 'text' in ctx.message
|
const args = ctx.message && 'text' in ctx.message
|
||||||
@@ -31,7 +30,6 @@ export async function handleLogin(ctx: Context) {
|
|||||||
|
|
||||||
const [username, password] = args
|
const [username, password] = args
|
||||||
|
|
||||||
try {
|
|
||||||
const storage = await getTokenStorage(telegramUserId)
|
const storage = await getTokenStorage(telegramUserId)
|
||||||
|
|
||||||
// Attempt login
|
// Attempt login
|
||||||
@@ -49,10 +47,6 @@ export async function handleLogin(ctx: Context) {
|
|||||||
} else {
|
} else {
|
||||||
await ctx.reply('❌ Login failed: Invalid response from server')
|
await ctx.reply('❌ Login failed: Invalid response from server')
|
||||||
}
|
}
|
||||||
} catch (error) {
|
|
||||||
const message = error instanceof Error ? error.message : 'Unknown error'
|
|
||||||
await ctx.reply(`❌ Login failed: ${message}`)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ export class TelegramPlatform extends BasePlatform {
|
|||||||
this.registerCommands()
|
this.registerCommands()
|
||||||
|
|
||||||
// Start bot
|
// Start bot
|
||||||
await this.bot.launch()
|
this.bot.launch()
|
||||||
console.log('✅ Telegram bot started successfully')
|
console.log('✅ Telegram bot started successfully')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,8 @@ import Redis from 'ioredis'
|
|||||||
|
|
||||||
export const getTokenStorage = async (telegramUserId: string): Promise<TokenStorage> => {
|
export const getTokenStorage = async (telegramUserId: string): Promise<TokenStorage> => {
|
||||||
const redis = new Redis(process.env.REDIS_URL || 'redis://localhost:6379')
|
const redis = new Redis(process.env.REDIS_URL || 'redis://localhost:6379')
|
||||||
const token = await redis.get(`memohome:telegram:${telegramUserId}:token`)
|
const isExists = await redis.exists(`memohome:telegram:${telegramUserId}:token`)
|
||||||
|
const token = isExists ? await redis.get(`memohome:telegram:${telegramUserId}:token`) : null
|
||||||
return {
|
return {
|
||||||
getApiUrl: () => process.env.API_URL || 'http://localhost:7002',
|
getApiUrl: () => process.env.API_URL || 'http://localhost:7002',
|
||||||
setApiUrl: () => {},
|
setApiUrl: () => {},
|
||||||
|
|||||||
Reference in New Issue
Block a user