Class: Solrengine::Auth::SessionsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/solrengine/auth/sessions_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/controllers/solrengine/auth/sessions_controller.rb', line 42

def create
  user = _user_class.find_by(wallet_address: params[:wallet_address])

  unless user&.nonce_valid?
    return render json: { error: "Could not sign in", code: "nonce_expired" },
                  status: :unprocessable_entity
  end

  verifier = SiwsVerifier.new(
    wallet_address: params[:wallet_address],
    message: params[:message],
    signature: params[:signature],
    expected_nonce: user.nonce
  )

  unless verifier.verify
    return render json: { error: "Could not sign in", code: "verification_failed" },
                  status: :unauthorized
  end

  user.generate_nonce!

  reset_session
  session[:user_id] = user.id
  render json: { success: true, wallet_address: user.wallet_address }
end

#destroyObject



69
70
71
72
73
74
75
# File 'app/controllers/solrengine/auth/sessions_controller.rb', line 69

def destroy
  reset_session
  respond_to do |format|
    format.html { redirect_to Solrengine::Auth.configuration.after_sign_out_path, notice: "Disconnected" }
    format.json { head :no_content }
  end
end

#newObject



21
22
23
# File 'app/controllers/solrengine/auth/sessions_controller.rb', line 21

def new
  # Renders the wallet connect view
end

#nonceObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/controllers/solrengine/auth/sessions_controller.rb', line 25

def nonce
  user = _user_class.find_or_create_by!(wallet_address: params[:wallet_address])
  user.generate_nonce!

  message = SiwsMessageBuilder.new(
    domain: Solrengine::Auth.configuration.domain,
    wallet_address: user.wallet_address,
    nonce: user.nonce,
    uri: request.base_url
  ).build

  render json: { message: message, nonce: user.nonce }
rescue ActiveRecord::RecordInvalid
  render json: { error: "Invalid wallet address", code: "invalid_wallet_address" },
         status: :unprocessable_entity
end