Class: Ibrain::Mutations::SignInMutation

Inherits:
AuthMutation
  • Object
show all
Defined in:
app/graphql/ibrain/mutations/sign_in_mutation.rb

Instance Method Summary collapse

Methods inherited from AuthMutation

#_prefixes, #ready?

Instance Method Details

#resolve(_args) ⇒ Object

Raises:

  • (ActionController::InvalidAuthenticityToken)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/graphql/ibrain/mutations/sign_in_mutation.rb', line 12

def resolve(_args)
  raise ActionController::InvalidAuthenticityToken, I18n.t('ibrain.errors.account.incorrect') if auth_resource.blank?

  if !auth_resource.try(:can_skip_confirmation?) && !auth_resource.try(:confirmed?)
    raise ActionController::InvalidAuthenticityToken, I18n.t('ibrain.errors.account.not_verified')
  end

  auth_resource.skip_confirmation! unless auth_resource.try(:confirmed?)
  (resource_name, auth_resource)
  @current_user = warden.authenticate!(auth_options)

  if !current_user.try(:is_activated?) && Ibrain::Config.
    raise ActionController::InvalidAuthenticityToken, I18n.t('ibrain.errors.account.is_deactivated')
  end

  # warden.set_user(current_user)
  current_user.jwt_token, jti = auth_headers(request, auth_resource)
  current_user.jti = jti
  current_user.save!

  if params[:device_token].present?
    device_token = current_user.device_tokens.find_by(token: params[:device_token])
    current_user.device_tokens.create!({ token: params[:device_token] }) if device_token.blank?
  end

  context[:current_user] = current_user

  graphql_returning(
    user_signed_in?,
    user_signed_in? ? current_user : nil,
    current_user.try(:jwt_token)
  )
end