| 1 | # frozen_string_literal: true |
| 2 | |
| 3 | # Fix this later |
| 4 | # rubocop:disable Metrics/ClassLength |
| 5 | class SessionsController < ApplicationController |
| 6 | def new |
| 7 | redirect_to profile_path(current_user.username_or_id) if user_signed_in? |
| 8 | |
| 9 | session[:redirect_after_login_path] = params[:redirect_to] if params[:redirect_to].present? |
| 10 | session[:omniauth_session_locale] = I18n.locale |
| 11 | |
| 12 | meta_tags('login') |
| 13 | end |
| 14 | |
| 15 | def create |
| 16 | @user = User.find_by(email: params[:session][:email].downcase) |
| 17 | if @user&.valid_password?(params[:session][:password]) |
| 18 | perform_login_if_possible |
| 19 | else |
| 20 | flash.now[:danger] = t('invalid_login') |
| 21 | render 'new' |
| 22 | end |
| 23 | end |
| 24 | |
| 25 | def create_pubkey |
| 26 | pubkey = params[:pubkey] |
| 27 | |
| 28 | # Don't get fancy! |
| 29 | pubkey_email = "#{pubkey}@musik88.com".downcase |
| 30 | |
| 31 | case params[:postAction] |
| 32 | when 'createAccount' |
| 33 | perform_create_account(pubkey_email, pubkey) |
| 34 | perform_login_pubkey |
| 35 | when 'checkPubkey' |
| 36 | perform_check_pubkey(pubkey_email) |
| 37 | end |
| 38 | end |
| 39 | |
| 40 | def confirm_email |
| 41 | return redirect_to login_path if request.get? |
| 42 | |
| 43 | @user = User.find_by_id(params[:id]) |
| 44 | return redirect_to login_path if @user.nil? || @user&.confirmed? |
| 45 | |
| 46 | @user.resend_confirmation_instructions |
| 47 | # Render confirm_email.js.erb |
| 48 | @resend_text = t('unconfirmed_email_resend_sent', |
| 49 | email: @user.email, |
| 50 | email_client_link: email_client_link) |
| 51 | end |
| 52 | |
| 53 | def destroy |
| 54 | sign_out if user_signed_in? |
| 55 | |
| 56 | redirect_after_logout_path = params[:redirect_to] ||= root_path |
| 57 | redirect_to redirect_after_logout_path |
| 58 | end |
| 59 | |
| 60 | private |
| 61 | |
| 62 | def perform_check_pubkey(pubkey) |
| 63 | @user = User.find_by(email: pubkey) |
| 64 | if @user.nil? |
| 65 | render json: { |
| 66 | shouldPromptMessageSign: true |
| 67 | } |
| 68 | else |
| 69 | perform_login_pubkey |
| 70 | end |
| 71 | end |
| 72 | |
| 73 | def perform_create_account(pubkey_email, pubkey) |
| 74 | # Create user and return next path |
| 75 | # Generate name from 3 first and 3 last characters of pubkey |
| 76 | name = "#{pubkey[0..2]}___#{pubkey[-3..]}" |
| 77 | @user = User.new(email: pubkey_email, name: name, provider: 'solana', uid: pubkey) |
| 78 | @user.skip_confirmation! |
| 79 | @user.skip_password_validation = true |
| 80 | @user.save |
| 81 | save_or_update_provider(@user, pubkey) |
| 82 | end |
| 83 | |
| 84 | def save_or_update_provider(user, pubkey) |
| 85 | authorization = user.authorizations.find_by(provider: 'solana', uid: pubkey) |
| 86 | authorization ||= Authorization.new(user_id: user.id, provider: 'solana', uid: pubkey) |
| 87 | authorization.save |
| 88 | logger.debug("Authorization: #{authorization}") |
| 89 | end |
| 90 | |
| 91 | def perform_login_pubkey |
| 92 | sign_in(:user, @user) |
| 93 | redirect_to session_redirect |
| 94 | |
| 95 | session.delete(:redirect_after_login_path) |
| 96 | end |
| 97 | |
| 98 | def session_redirect |
| 99 | after_login_path = if current_user.need_onboarding? |
| 100 | onboarding_path |
| 101 | else |
| 102 | session[:redirect_after_login_path] |
| 103 | end |
| 104 | after_login_path || profile_path(user.username_or_id) |
| 105 | end |
| 106 | |
| 107 | def meta_tags(slug) |
| 108 | meta = Meta |
| 109 | .select(:title, :description) |
| 110 | .where(home: slug, locale: [I18n.locale, I18n.default_locale]) |
| 111 | .last |
| 112 | |
| 113 | @meta_title = meta.title if meta |
| 114 | @meta_description = meta.description if meta |
| 115 | end |
| 116 | |
| 117 | def perform_login_if_possible |
| 118 | return render_unconfirmed unless @user.confirmed? |
| 119 | |
| 120 | sign_in(:user, @user) |
| 121 | |
| 122 | create_wallet_if_needed |
| 123 | sync_spotify_if_needed |
| 124 | |
| 125 | @user.remember_me! if params[:session][:remember_me] == '1' |
| 126 | |
| 127 | redirect_to session_redirect |
| 128 | |
| 129 | session.delete(:redirect_after_login_path) |
| 130 | end |
| 131 | |
| 132 | def render_unconfirmed |
| 133 | flash.now[:info] = |
| 134 | t('unconfirmed_email', |
| 135 | click_here_link: view_context.link_to( |
| 136 | t('unconfirmed_email_click_here'), |
| 137 | confirm_email_path(id: @user.id), method: :post, remote: true |
| 138 | ), |
| 139 | email_client_link: email_client_link) |
| 140 | render 'new' |
| 141 | end |
| 142 | |
| 143 | def email_client_link |
| 144 | view_context.link_to( |
| 145 | t('unconfirmed_email_email_app'), |
| 146 | "https://#{@user.email_client}", |
| 147 | target: '__blank' |
| 148 | ) |
| 149 | end |
| 150 | |
| 151 | def create_wallet_if_needed |
| 152 | create_wallet_solana |
| 153 | create_wallet_evm |
| 154 | end |
| 155 | |
| 156 | def create_wallet_solana |
| 157 | solana_wallet = Wallet.find_by(user_id: @user.id, wallet_type: Wallet.wallet_types[:solana]) |
| 158 | return unless solana_wallet.nil? |
| 159 | |
| 160 | Wallet.new(user_id: @user.id, wallet_type: Wallet.wallet_types[:solana], status: :pending).save |
| 161 | WalletSolanaJob.perform_later(@user.id) |
| 162 | end |
| 163 | |
| 164 | def create_wallet_evm |
| 165 | evm_wallet = Wallet.find_by(user_id: @user.id, wallet_type: Wallet.wallet_types[:evm]) |
| 166 | return unless evm_wallet.nil? |
| 167 | |
| 168 | Wallet.new(user_id: @user.id, wallet_type: Wallet.wallet_types[:evm], status: :pending).save |
| 169 | WalletEvmJob.perform_later(@user.id) |
| 170 | end |
| 171 | |
| 172 | def sync_spotify_if_needed |
| 173 | return if current_user.need_onboarding? |
| 174 | |
| 175 | return unless @user.user_setting.sync_spotify? |
| 176 | |
| 177 | SpotifyOnboardingExportJob.perform_later(@user.id, 'short_term') |
| 178 | SpotifySyncExportJob.perform_later(@user.id) |
| 179 | end |
| 180 | end |