Module: ChatgptAssistant::Bots::Telegram::Auth

Included in:
TelegramBot
Defined in:
lib/chatgpt_assistant/bots/telegram/auth.rb

Instance Method Summary collapse

Instance Method Details

#authenticate_user(email, password) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/chatgpt_assistant/bots/telegram/auth.rb', line 7

def authenticate_user(email, password)
  case telegram_user_auth(email, password, msg.chat.id)
  when "user not found"
    raise UserNotFoundError
  when "wrong password"
    raise WrongPasswordError
  when email
    send_message(chat_id: msg.chat.id, text: success_messages[:user_logged_in])
  end
end

#telegram_user_access(visitor, new_access) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/chatgpt_assistant/bots/telegram/auth.rb', line 30

def telegram_user_access(visitor, new_access)
  other_access = where_user(telegram_id: visitor.telegram_id)
  other_access&.each { |access| access.update(telegram_id: nil) } if other_access&.class == Array
  other_access&.update(telegram_id: nil) if other_access&.class == User
  new_access.update(telegram_id: visitor.telegram_id)
  new_access.email
end

#telegram_user_auth(email, password, telegram_id) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/chatgpt_assistant/bots/telegram/auth.rb', line 18

def telegram_user_auth(email, password, telegram_id)
  return "wrong password" if password.nil?

  visitor_access = find_visitor(telegram_id: telegram_id)
  return "something went wrong" unless visitor_access

  new_access = find_user(email: email)
  return "user not found" unless new_access

  user.valid_password?(password) ? telegram_user_access(visitor_access, new_access) : "wrong password"
end