feature/share-libs
rb 160 lines 4.85 KB
Raw
1 # frozen_string_literal: true
2
3 # Fix this later
4 # rubocop:disable Metrics/ClassLength
5 module Users
6 class OmniauthCallbacksController < Devise::OmniauthCallbacksController
7 # Apple call the callback as a POST request. Just Apple being Apple.
8 skip_before_action :verify_authenticity_token, only: [:apple]
9 before_action :set_locale
10
11 def facebook
12 # TODO: Onboarding from Facebook
13 @user = User.from_omniauth(request.env['omniauth.auth'])
14 try_login
15 end
16
17 def spotify
18 auth = request.env['omniauth.auth']
19 # Check if this is a pubkey auth onboarding by checking onboarding_provider session
20 return web3_setup_onboarding_provider(auth) if session[:onboarding_provider] == 'solana'
21
22 @user = User.find_by(email: auth.info.email)
23 try_login_or_signup(auth)
24 end
25
26 def google_oauth2
27 auth = request.env['omniauth.auth']
28 # Check if this is a pubkey auth onboarding by checking onboarding_provider session
29 return web3_setup_onboarding_provider(auth) if session[:onboarding_provider] == 'solana'
30
31 @user = User.find_by(email: auth.info.email)
32 try_login_or_signup(auth)
33 end
34
35 def apple
36 # TODO: Onboarding from Apple
37 @user = User.from_omniauth(request.env['omniauth.auth'])
38 try_login
39 end
40
41 def failure
42 redirect_to root_path
43 end
44
45 # Setup locale from the previous omniauth call
46 def set_locale
47 I18n.locale = session[:omniauth_session_locale]
48 end
49
50 private
51
52 def web3_setup_onboarding_provider(auth)
53 # We should have a current_user here that is pubkey authenticated
54 return redirect_to login_path if current_user.nil?
55
56 logger.info "web3_setup_spotify_provider: #{current_user}"
57 @user = current_user
58 User.from_omniauth_web3(auth, current_user.email)
59 onboarding(nil)
60 end
61
62 def try_login_or_signup(auth)
63 return signup(auth) if @user.nil?
64
65 User.from_omniauth(auth)
66 try_login
67 end
68
69 def try_login
70 return handle_unconfirmed_email unless @user.confirmed?
71
72 return redirect_to login_path unless @user.persisted?
73
74 ab_finished(:only_spotify_youtube, reset: true)
75
76 sign_in @user
77 create_wallet_if_needed
78 sync_spotify_if_needed
79
80 flash[:success] = t('welcome_user', user_name: @user.name) unless current_user.need_onboarding?
81 redirect_to session_redirect
82 end
83
84 def handle_unconfirmed_email
85 flash[:info] =
86 t('unconfirmed_email',
87 click_here_link: view_context.link_to(
88 t('unconfirmed_email_click_here'),
89 confirm_email_path(id: @user.id), method: :post, remote: true
90 ),
91 email_client_link: email_client_link)
92 redirect_to login_path
93 end
94
95 def email_client_link
96 view_context.link_to(
97 t('unconfirmed_email_email_app'),
98 "https://#{@user.email_client}",
99 target: '__blank'
100 )
101 end
102
103 def create_wallet_if_needed
104 create_wallet_solana
105 create_wallet_evm
106 end
107
108 def create_wallet_solana
109 solana_wallet = Wallet.find_by(user_id: @user.id, wallet_type: Wallet.wallet_types[:solana])
110 return unless solana_wallet.nil?
111
112 Wallet.new(user_id: @user.id, wallet_type: Wallet.wallet_types[:solana], status: :pending).save
113 WalletSolanaJob.perform_later(@user.id)
114 end
115
116 def create_wallet_evm
117 evm_wallet = Wallet.find_by(user_id: @user.id, wallet_type: Wallet.wallet_types[:evm])
118 return unless evm_wallet.nil?
119
120 Wallet.new(user_id: @user.id, wallet_type: Wallet.wallet_types[:evm], status: :pending).save
121 WalletEvmJob.perform_later(@user.id)
122 end
123
124 def sync_spotify_if_needed
125 return if current_user.need_onboarding?
126
127 return unless @user.user_setting.sync_spotify?
128
129 SpotifyOnboardingExportJob.perform_later(@user.id, 'short_term')
130 SpotifySyncExportJob.perform_later(@user.id)
131 end
132
133 def session_redirect
134 # Override redirection if needed
135 redirect_after_login_path = if current_user.need_onboarding?
136 onboarding_path
137 else
138 session[:redirect_after_login_path]
139 end
140 redirect_after_login_path || profile_path(current_user.username_or_id)
141 end
142
143 def delete_session
144 session.delete(:redirect_after_login_path)
145 session.delete(:omniauth_session_locale)
146 end
147
148 def signup(auth)
149 redirect_to signup_path(provider: auth.provider, email: auth.info.email, name: auth.info.name,
150 uid: auth.uid, token: auth.credentials.token)
151 end
152
153 def onboarding(auth)
154 return redirect_to onboarding_path if auth.nil?
155
156 redirect_to onboarding_path(provider: auth.provider, email: auth.info.email, name: auth.info.name,
157 uid: auth.uid, token: auth.credentials.token)
158 end
159 end
160 end