Class: Daidan::LoginUser

Inherits:
BaseMutation
  • Object
show all
Defined in:
lib/daidan/graphql/mutations/login_user.rb

Instance Method Summary collapse

Instance Method Details

#resolve(email:, password:) ⇒ Object

Raises:

  • (GraphQL::ExecutionError)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/daidan/graphql/mutations/login_user.rb', line 9

def resolve(email:, password:)
  user = User.where(email: email).first

  raise GraphQL::ExecutionError, 'Invalid email or password' unless user && user.authenticate(password)

  exp_time = Time.now.to_i + (24 * 60 * 60)
  payload = {
    user_id: user.id,
    exp: exp_time
  }

  token = JWT.encode(payload, ENV['JWT_SECRET'], 'HS256')
  {
    token: token,
    user: user
  }
end