Добавлены модели и функциональность для управления администраторами, включая авторизацию через JWT, запросы и мутации для получения информации об администраторах и управления пользователями. Обновлены стили и логика работы с токенами в Apollo Client. Улучшен интерфейс взаимодействия с пользователем.
This commit is contained in:
@ -11,6 +11,10 @@ interface Context {
|
||||
id: string
|
||||
phone: string
|
||||
}
|
||||
admin?: {
|
||||
id: string
|
||||
username: string
|
||||
}
|
||||
}
|
||||
|
||||
// Создаем Apollo Server
|
||||
@ -31,27 +35,42 @@ const handler = startServerAndCreateNextHandler<NextRequest>(server, {
|
||||
|
||||
if (!token) {
|
||||
console.log('GraphQL Context - No token provided')
|
||||
return { user: undefined }
|
||||
return { user: undefined, admin: undefined }
|
||||
}
|
||||
|
||||
try {
|
||||
// Верифицируем JWT токен
|
||||
const decoded = jwt.verify(token, process.env.JWT_SECRET!) as {
|
||||
userId: string
|
||||
phone: string
|
||||
userId?: string
|
||||
phone?: string
|
||||
adminId?: string
|
||||
username?: string
|
||||
type?: string
|
||||
}
|
||||
|
||||
console.log('GraphQL Context - Decoded user:', { id: decoded.userId, phone: decoded.phone })
|
||||
|
||||
return {
|
||||
user: {
|
||||
id: decoded.userId,
|
||||
phone: decoded.phone
|
||||
// Проверяем тип токена
|
||||
if (decoded.type === 'admin' && decoded.adminId && decoded.username) {
|
||||
console.log('GraphQL Context - Decoded admin:', { id: decoded.adminId, username: decoded.username })
|
||||
return {
|
||||
admin: {
|
||||
id: decoded.adminId,
|
||||
username: decoded.username
|
||||
}
|
||||
}
|
||||
} else if (decoded.userId && decoded.phone) {
|
||||
console.log('GraphQL Context - Decoded user:', { id: decoded.userId, phone: decoded.phone })
|
||||
return {
|
||||
user: {
|
||||
id: decoded.userId,
|
||||
phone: decoded.phone
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { user: undefined, admin: undefined }
|
||||
} catch (error) {
|
||||
console.error('GraphQL Context - Invalid token:', error)
|
||||
return { user: undefined }
|
||||
return { user: undefined, admin: undefined }
|
||||
}
|
||||
}
|
||||
})
|
||||
|
Reference in New Issue
Block a user