Skip to content

Commit b34418b

Browse files
committed
does it work on staging?
1 parent 616f411 commit b34418b

15 files changed

Lines changed: 3136 additions & 1458 deletions

File tree

config/dev.exs

Lines changed: 6 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import Config
22

33
config :pr,
4-
playlist_name: "PlayRequest"
4+
playlist_name: "PlayRequest",
5+
session_same_site: "Lax",
6+
session_secure: false
57

68
# Configure your database
79
config :pr, PR.Repo,
@@ -78,8 +80,8 @@ config :pr, PRWeb.Endpoint,
7880
]
7981

8082
# Do not include metadata nor timestamps in development logs
81-
# config :logger, :console, format: "[$level] $message\n"
82-
config :logger, level: :info
83+
config :logger, :console, format: "[$level] $message\n", level: :debug
84+
# config :logger, level: :info
8385

8486
# Initialize plugs at runtime for faster development compilation
8587
config :phoenix, :plug_init_mode, :runtime
@@ -90,43 +92,4 @@ config :phoenix, :stacktrace_depth, 20
9092

9193
config :phoenix_live_view, :debug_heex_annotations, true
9294

93-
config :pr, :sonos,
94-
scopes: "playback-control-all",
95-
redirect_uri: "#{System.get_env("REDIRECT_URL_BASE")}/sonos/authorized",
96-
key: System.get_env("SONOS_KEY"),
97-
secret: System.get_env("SONOS_SECRET")
98-
99-
config :pr, :spotify,
100-
scopes:
101-
~w(user-modify-playback-state user-read-currently-playing user-read-playback-state playlist-modify-private playlist-read-private),
102-
redirect_uri: "#{System.get_env("REDIRECT_URL_BASE")}/spotify/authorized",
103-
user_id: System.get_env("SPOTIFY_USER_ID"),
104-
key: System.get_env("SPOTIFY_CLIENT_ID"),
105-
secret: System.get_env("SPOTIFY_SECRET")
106-
107-
config :ueberauth, Ueberauth.Strategy.Google.OAuth,
108-
client_id: System.get_env("GOOGLE_CLIENT_ID"),
109-
client_secret: System.get_env("GOOGLE_CLIENT_SECRET")
110-
111-
config :pr,
112-
allowed_user_domains: System.get_env("ALLOWED_USER_DOMAINS"),
113-
installation_name: System.get_env("INSTALLATION_NAME", "PlayRequest"),
114-
super_likes_allowed: System.get_env("SUPER_LIKES_ALLOWED", "2"),
115-
burns_allowed: System.get_env("BURNS_ALLOWED", "2")
116-
117-
config :pr, :feature_flags,
118-
show_volume: System.get_env("FF_VOLUME", ""),
119-
show_toggle_playback: System.get_env("FF_TOGGLE_PLAYBACK", ""),
120-
show_skip: System.get_env("FF_SKIP", ""),
121-
scale_play_button: System.get_env("FF_SCALE_PLAY_BUTTON", ""),
122-
show_super_like: System.get_env("FF_SUPER_LIKE", ""),
123-
show_burn: System.get_env("FF_BURN", "")
124-
125-
config :sentry,
126-
dsn: System.get_env("SENTRY_DSN"),
127-
environment_name: :dev,
128-
enable_source_code_context: true,
129-
root_source_code_path: File.cwd!(),
130-
tags: %{
131-
env: "dev"
132-
}
95+
# Runtime configuration for development environment has been moved to config/runtime.exs

config/prod.exs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
import Config
22

3-
config :logger,
4-
backends: [LoggerJSON],
5-
level: :info
3+
config :pr,
4+
session_same_site: "None",
5+
session_secure: true
66

7-
config :pr, PR.Repo, ssl: false, socket_options: [:inet6]
7+
# For production, don't forget to configure the url host
8+
# to something meaningful, Phoenix uses this information
9+
# when generating URLs.
810

9-
config :pr, PRWeb.Endpoint,
10-
force_ssl: [rewrite_on: [:x_forwarded_proto]],
11-
server: true,
12-
root: ".",
13-
version: Application.spec(:pr, :vsn)
11+
# Runtime configuration (such as database credentials) should be
12+
# placed in config/runtime.exs which is loaded at runtime.
1413

15-
config :pr,
16-
sleep: 60_000
14+
# Note we also include the path to a cache manifest
15+
# containing the digested version of static files. This
16+
# manifest is generated by the `mix assets.deploy` task,
17+
# which you should run after static files are built and
18+
# before starting your production server.
19+
config :pr, PRWeb.Endpoint, cache_static_manifest: "priv/static/cache_manifest.json"
20+
21+
# Do not print debug messages in production
22+
config :logger, level: :info
1723

18-
config :sentry,
19-
enable_source_code_context: true,
20-
root_source_code_path: File.cwd!()
24+
# Runtime configuration moved to config/runtime.exs

config/releases.exs

Lines changed: 0 additions & 71 deletions
This file was deleted.

config/runtime.exs

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import Config
2+
3+
# config/runtime.exs is executed for all environments, including
4+
# during releases. It is executed after compilation and before the
5+
# system starts, so it is typically used to load production configuration
6+
# and secrets from environment variables or elsewhere. Do not define
7+
# any compile-time configuration in here, as it won't be applied.
8+
9+
# ## Using releases
10+
#
11+
# If you use `mix release`, you need to explicitly enable the server
12+
# by passing the PHX_SERVER=true when you start it:
13+
#
14+
# PHX_SERVER=true bin/pr start
15+
#
16+
# Alternatively, you can use `mix phx.gen.release` to generate a `bin/server`
17+
# script that automatically sets the env var above.
18+
if System.get_env("PHX_SERVER") do
19+
config :pr, PRWeb.Endpoint, server: true
20+
end
21+
22+
# Common runtime configuration for all environments
23+
config :pr, :sonos,
24+
scopes: "playback-control-all",
25+
redirect_uri: "#{System.get_env("REDIRECT_URL_BASE")}/sonos/authorized",
26+
key: System.get_env("SONOS_KEY"),
27+
secret: System.get_env("SONOS_SECRET")
28+
29+
config :pr, :spotify,
30+
scopes:
31+
~w(user-modify-playback-state user-read-currently-playing user-read-playback-state playlist-modify-private playlist-read-private),
32+
redirect_uri: "#{System.get_env("REDIRECT_URL_BASE")}/spotify/authorized",
33+
user_id: System.get_env("SPOTIFY_USER_ID"),
34+
key: System.get_env("SPOTIFY_CLIENT_ID"),
35+
secret: System.get_env("SPOTIFY_SECRET")
36+
37+
config :ueberauth, Ueberauth.Strategy.Google.OAuth,
38+
client_id: System.get_env("GOOGLE_CLIENT_ID"),
39+
client_secret: System.get_env("GOOGLE_CLIENT_SECRET")
40+
41+
config :pr,
42+
allowed_user_domains: System.get_env("ALLOWED_USER_DOMAINS"),
43+
installation_name: System.get_env("INSTALLATION_NAME", "PlayRequest"),
44+
super_likes_allowed: System.get_env("SUPER_LIKES_ALLOWED", "2"),
45+
burns_allowed: System.get_env("BURNS_ALLOWED", "2")
46+
47+
config :pr, :feature_flags,
48+
show_volume: System.get_env("FF_VOLUME", ""),
49+
show_toggle_playback: System.get_env("FF_TOGGLE_PLAYBACK", ""),
50+
show_skip: System.get_env("FF_SKIP", ""),
51+
scale_play_button: System.get_env("FF_SCALE_PLAY_BUTTON", ""),
52+
show_super_like: System.get_env("FF_SUPER_LIKE", ""),
53+
show_burn: System.get_env("FF_BURN", "")
54+
55+
config :sentry,
56+
dsn: System.get_env("SENTRY_DSN"),
57+
enable_source_code_context: true,
58+
root_source_code_path: File.cwd!()
59+
60+
# Production-specific configuration
61+
if config_env() == :prod do
62+
config :logger,
63+
backends: [LoggerJSON],
64+
level: :info
65+
66+
config :pr, PR.Repo,
67+
url: System.get_env("DATABASE_URL") || raise("DATABASE_URL not available"),
68+
pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10"),
69+
ssl: false,
70+
socket_options: [:inet6]
71+
72+
config :pr, PRWeb.Endpoint,
73+
http: [port: String.to_integer(System.get_env("PORT") || "4000")],
74+
url: [host: System.get_env("HOSTNAME"), port: 443, scheme: "https"],
75+
cache_static_manifest: "priv/static/cache_manifest.json",
76+
force_ssl: [rewrite_on: [:x_forwarded_proto]],
77+
server: true
78+
79+
# Production requires these environment variables
80+
unless System.get_env("SONOS_KEY"), do: raise("SONOS_KEY not available")
81+
unless System.get_env("SONOS_SECRET"), do: raise("SONOS_SECRET not available")
82+
unless System.get_env("SPOTIFY_USER_ID"), do: raise("SPOTIFY_USER_ID not available")
83+
unless System.get_env("SPOTIFY_CLIENT_ID"), do: raise("SPOTIFY_CLIENT_ID not available")
84+
unless System.get_env("SPOTIFY_SECRET"), do: raise("SPOTIFY_SECRET not available")
85+
unless System.get_env("GOOGLE_CLIENT_ID"), do: raise("GOOGLE_CLIENT_ID not available")
86+
unless System.get_env("GOOGLE_CLIENT_SECRET"), do: raise("GOOGLE_CLIENT_SECRET not available")
87+
88+
config :pr,
89+
sleep: 60_000
90+
91+
app_name =
92+
System.get_env("FLY_APP_NAME") ||
93+
raise "FLY_APP_NAME not available"
94+
95+
config :libcluster,
96+
debug: true,
97+
topologies: [
98+
fly6pn: [
99+
strategy: Cluster.Strategy.DNSPoll,
100+
config: [
101+
polling_interval: 5_000,
102+
query: "#{app_name}.internal",
103+
node_basename: app_name
104+
]
105+
]
106+
]
107+
108+
config :sentry,
109+
tags: %{
110+
env: System.get_env("RELEASE_STAGE") || "dev"
111+
},
112+
environment_name: System.get_env("RELEASE_STAGE") || :prod,
113+
release: System.get_env("APP_REVISION", "dev")
114+
end
115+
116+
# Development-specific configuration
117+
if config_env() == :dev do
118+
config :sentry,
119+
environment_name: :dev,
120+
tags: %{
121+
env: "dev"
122+
}
123+
end

lib/pr_web.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ defmodule PRWeb do
5151
def plug do
5252
quote do
5353
import Plug.Conn
54-
use Phoenix.Controller, namespace: PRWeb
54+
import Phoenix.Controller, only: [redirect: 2]
5555
unquote(verified_routes())
5656
end
5757
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<div class="content">
2+
<header class="container--header">
3+
<h1><%= installation_name() %></h1>
4+
</header>
5+
<div class="alert-container">
6+
<.flash class="alert alert-info" kind={:info} title="Success!" flash={@flash} />
7+
<.flash class="alert alert-danger" kind={:error} title="Error!" flash={@flash} />
8+
</div>
9+
<%= @inner_content %>
10+
</div>

lib/pr_web/controllers/auth_controller.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
defmodule PRWeb.AuthController do
22
use PRWeb, :controller
33
plug(Ueberauth)
4+
plug(:put_layout, html: {PRWeb.Layouts, :auth})
45

56
alias PR.Auth
67

lib/pr_web/endpoint.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ defmodule PRWeb.Endpoint do
66
store: :cookie,
77
key: "_pr_web_key",
88
signing_salt: "ZG+jiBR2",
9-
same_site: "None"
9+
# Different same site polic for local where there is no HTTPS
10+
same_site: Application.compile_env(:pr, :session_same_site, "Lax"),
11+
secure: Application.compile_env(:pr, :session_secure, false)
1012
]
1113

1214
socket("/live", Phoenix.LiveView.Socket,

lib/pr_web/live/auth_hooks.ex

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
defmodule PRWeb.AuthHooks do
2+
@moduledoc """
3+
LiveView authentication hooks for handling user sessions.
4+
"""
5+
use PRWeb, :verified_routes
6+
7+
import Phoenix.Component
8+
import Phoenix.LiveView
9+
10+
alias PR.Auth
11+
12+
def on_mount(:require_authenticated_user, _params, %{"user_id" => user_id} = session, socket) do
13+
require Logger
14+
Logger.debug("AuthHook: Found user_id in session: #{inspect(user_id)}")
15+
Logger.debug("AuthHook: Full session: #{inspect(session)}")
16+
17+
case Auth.get_user(user_id) do
18+
nil ->
19+
Logger.debug("AuthHook: User not found in database for user_id: #{inspect(user_id)}")
20+
21+
socket =
22+
socket
23+
|> put_flash(:error, "You must log in to access this page.")
24+
|> redirect(to: ~p"/auth")
25+
26+
{:halt, socket}
27+
28+
user ->
29+
Logger.debug("AuthHook: User authenticated: #{inspect(user.id)}")
30+
{:cont, assign(socket, current_user: user)}
31+
end
32+
end
33+
34+
def on_mount(:require_authenticated_user, _params, session, socket) do
35+
require Logger
36+
Logger.debug("AuthHook: No user_id in session. Session: #{inspect(session)}")
37+
38+
socket =
39+
socket
40+
|> put_flash(:error, "You must log in to access this page.")
41+
|> redirect(to: ~p"/auth")
42+
43+
{:halt, socket}
44+
end
45+
end

0 commit comments

Comments
 (0)