Skip to content

Commit

Permalink
Merge branch 'main' into feat/core-create-post
Browse files Browse the repository at this point in the history
  • Loading branch information
juanzeen committed Dec 7, 2024
2 parents 0d2ad10 + ceb6e32 commit 7853cf7
Show file tree
Hide file tree
Showing 27 changed files with 297 additions and 192 deletions.
75 changes: 75 additions & 0 deletions assets/css/noticias.scss
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,78 @@
margin-top: 20px;
}
}

.noticia {
display: flex;
flex-direction: column;
align-items: center;
padding: 40px 220px;
}

.noticia h1 {
margin-bottom: 30px;
line-height: 46px;
font-weight: 700;
}

.noticia .noticia-descricao h2 {
color: #0064C8;
font-size: 32px;
line-height: 38px;
font-weight: 700;
margin-top: 60px;
margin-bottom: 30px;
}

.noticia .noticia-descricao h3 {
font-size: 24px;
line-height: 28px;
font-weight: 700;
color: #404040;
margin-bottom: 30px;
}

.noticia .noticia-descricao strong em {
text-decoration: underline;
color: #0064C8;
font-weight: 400;
font-style: normal;
}

.noticia .noticia-descricao p {
font-size: 16px;
color: #404040;
margin-bottom: 40px;
}


.noticia .noticia-descricao p a {
font-weight: 700;
line-height: 24px;
color: #0064C8;
}

.noticia .noticia-descricao p a::after {
content: "";
display: inline-block;
color: inherit;
font-size: 0.8em;
position: relative;
transform: rotate(-45deg);
bottom: 2px;
}

.noticia .noticia-descricao p a:hover {
text-decoration-line: underline;
}

.noticia .noticia-descricao img {
width: 100%;
border-radius: 16px;
}

@media (max-width: 768px) {
.noticia {
padding: 40px;
}
}
6 changes: 0 additions & 6 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ config :pescarte, PescarteWeb,
notice_title_max_length: 110,
notice_desc_max_length: 145

config :supabase_gotrue,
endpoint: PescarteWeb.Endpoint,
signed_in_path: "/app/pesquisa/perfil",
not_authenticated_path: "/acessar",
authentication_client: Pescarte.Supabase.Auth

config :tesla, adapter: {Tesla.Adapter.Finch, name: PescarteHTTPClient}

config :flop, repo: Pescarte.Database.Repo.Replica
Expand Down
2 changes: 1 addition & 1 deletion config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ config :phoenix, :plug_init_mode, :runtime

config :git_hooks,
verbose: false,
auto_install: true,
auto_install: false,
branches: [
whitelist: ["main"]
],
Expand Down
7 changes: 3 additions & 4 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ config :pescarte, :pesquisa_ingestion, sheet_url: System.get_env("SHEET_URL")

config :resend, Resend.Client, api_key: System.get_env("RESEND_KEY")

config :supabase_potion,
manage_clients: true,
supabase_base_url: System.get_env("SUPABASE_URL"),
supabase_api_key: System.get_env("SUPABASE_KEY")
config :pescarte, Pescarte.Supabase,
base_url: System.get_env("SUPABASE_URL"),
api_key: System.get_env("SUPABASE_KEY")

if System.get_env("PHX_SERVER") do
config :pescarte, PescarteWeb.Endpoint, server: true
Expand Down
5 changes: 5 additions & 0 deletions lib/pescarte.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ defmodule Pescarte do
@environment
end

@spec get_supabase_client :: {:ok, Supabase.Client.t()} | {:error, term}
def get_supabase_client do
Pescarte.Supabase.get_client()
end

def get_static_file_path(folder, file) do
:pescarte
|> :code.priv_dir()
Expand Down
8 changes: 5 additions & 3 deletions lib/pescarte/identidades/register_usuario.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ defmodule Pescarte.Identidades.RegisterUsuario do
alias Pescarte.Database.Repo
alias Pescarte.Identidades.Adapters.UsuarioAdapter
alias Pescarte.Identidades.Models.Usuario
alias Pescarte.Supabase.Auth
alias Supabase.GoTrue

def run(attrs, tipo) when tipo in ~w(pesquisador admin)a do
Repo.transaction(fn ->
attrs = Map.put(attrs, :papel, tipo)
{:ok, internal} = do_create_usuario(attrs)
{:ok, external} = UsuarioAdapter.to_external(internal)
{:ok, _} = Auth.Admin.create_user(external)
{:ok, client} = Pescarte.get_supabase_client()
{:ok, _} = GoTrue.Admin.create_user(client, external)
internal
end)
end
Expand All @@ -18,7 +19,8 @@ defmodule Pescarte.Identidades.RegisterUsuario do
Repo.transaction(fn ->
{:ok, internal} = do_create_usuario(attrs)
{:ok, external} = UsuarioAdapter.to_external(internal)
{:ok, _} = Auth.Admin.create_user(external)
{:ok, client} = Pescarte.get_supabase_client()
{:ok, _} = GoTrue.Admin.create_user(client, external)
internal
end)
end
Expand Down
8 changes: 5 additions & 3 deletions lib/pescarte/modulo_pesquisa/register_pesquisador.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ defmodule Pescarte.ModuloPesquisa.RegisterPesquisador do
alias Pescarte.Identidades.Adapters.UsuarioAdapter
alias Pescarte.Identidades.Models.Usuario
alias Pescarte.ModuloPesquisa.Models.Pesquisador
alias Pescarte.Supabase.Auth

alias Supabase.GoTrue

def run(attrs) do
Repo.transaction(fn ->
with {:ok, pesquisador} <- do_create_pesquisador(attrs),
external = UsuarioAdapter.to_external(pesquisador.usuario),
{:ok, external} <- Auth.Admin.create_user(external),
{:ok, client} <- Pescarte.get_supabase_client(),
{:ok, external} <- GoTrue.Admin.create_user(client, external),
opts = [type: :signup, redirect_to: ~p"/confirmar"],
:ok <- Auth.resend(external.email, opts),
:ok <- GoTrue.resend(client, external.email, opts),
{:ok, _} <- Usuario.link_to_external(pesquisador.usuario, external.id) do
pesquisador
else
Expand Down
7 changes: 4 additions & 3 deletions lib/pescarte/scripts/pesquisa_ingestion.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ defmodule Pescarte.Scripts.PesquisaIngestion do
alias Ecto.Multi, as: TRX
alias Pescarte.Database.Repo

alias Pescarte.Supabase.Auth

alias Pescarte.Identidades.Models.Contato
alias Pescarte.Identidades.RegisterUsuario

Expand Down Expand Up @@ -197,7 +195,10 @@ defmodule Pescarte.Scripts.PesquisaIngestion do
|> Map.put("phone_confirm", true)
|> Map.put("role", "pesquisador")
|> Map.put("app_metadata", app_metadata)
|> then(&Auth.Admin.create_user/1)
|> then(fn attrs ->
{:ok, client} = Pescarte.get_supabase_client()
Supabase.GoTrue.Admin.create_user(client, attrs)
end)
end)
|> TRX.run(key, fn _repo, state ->
contato = state[contato_key]
Expand Down
24 changes: 1 addition & 23 deletions lib/pescarte/supabase.ex
Original file line number Diff line number Diff line change
@@ -1,25 +1,3 @@
defmodule Pescarte.Supabase do
defmodule Auth do
use Supabase.GoTrue, client: Pescarte.Supabase.Auth
end

defmodule Storage do
use Supabase.Storage, client: Pescarte.Supabase.Auth
end

def start_link(_opts) do
children = [__MODULE__.Auth]
opts = [strategy: :one_for_one, name: __MODULE__]
Supervisor.start_link(children, opts)
end

def child_spec(opts) do
%{
id: __MODULE__,
start: {__MODULE__, :start_link, [opts]},
type: :worker,
restart: :permanent,
shutdown: 500
}
end
use Supabase.Client, otp_app: :pescarte
end
24 changes: 20 additions & 4 deletions lib/pescarte_web/authorization.ex → lib/pescarte_web/auth.ex
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
defmodule PescarteWeb.Authorization do
defmodule PescarteWeb.Auth do
@moduledoc """
Módulo responsável por controlar tanto autenticação quanto
autorização das "dead views" e live views, além de autenticação via API.
"""
use Supabase.GoTrue.Plug,
client: Pescarte.Supabase,
endpoint: PescarteWeb.Endpoint,
signed_in_path: "/app/pesquisa/perfil",
not_authenticated_path: "/acessar"

use Supabase.GoTrue.LiveView,
client: Pescarte.Supabase,
endpoint: PescarteWeb.Endpoint,
signed_in_path: "/app/pesquisa/perfil",
not_authenticated_path: "/acessar"

import Plug.Conn

alias Pescarte.Identidades.Models.Usuario
alias Pescarte.Supabase.Auth
alias Supabase.GoTrue
alias Supabase.GoTrue.Session
alias Supabase.GoTrue.User

def require_admin_role(conn, _opts) do
token = get_session(conn, :user_token)

with {:ok, user} <- Auth.get_user(%Session{access_token: token}),
with {:ok, client} <- Pescarte.get_supabase_client(),
{:ok, user} <- GoTrue.get_user(client, %Session{access_token: token}),
{:ok, usuario} <- Usuario.fetch_by(external_customer_id: user.id) do
(token && usuario)
|> permit?(:admin)
Expand All @@ -30,7 +46,7 @@ defmodule PescarteWeb.Authorization do
end

def on_mount(:ensure_admin_role, _params, session, socket) do
socket = GoTrue.LiveView.mount_current_user(session, socket)
socket = mount_current_user(session, socket)
user = socket.assigns.current_user

if user && permit?(user, :admin) do
Expand Down
4 changes: 2 additions & 2 deletions lib/pescarte_web/controllers/agenda_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ defmodule PescarteWeb.AgendaController do
def show(conn, _params) do
current_month =
"appointments_data"
|> Pescarte.get_static_file_path("agenda_novembro.csv")
|> Pescarte.get_static_file_path("agenda_dezembro.csv")
|> File.stream!()
|> CSV.parse_stream(skip_headers: false)
|> Enum.take(1)
|> List.first()

table_data =
"appointments_data"
|> Pescarte.get_static_file_path("agenda_novembro.csv")
|> Pescarte.get_static_file_path("agenda_dezembro.csv")
|> File.stream!()
|> CSV.parse_stream()
|> Stream.drop(1)
Expand Down
6 changes: 3 additions & 3 deletions lib/pescarte_web/controllers/login_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ defmodule PescarteWeb.LoginController do
import Phoenix.LiveView.Controller, only: [live_render: 3]

alias Pescarte.Identidades.Models.Usuario
alias PescarteWeb.Auth
alias PescarteWeb.LoginLive
alias Supabase.GoTrue

require Logger

Expand All @@ -23,7 +23,7 @@ defmodule PescarteWeb.LoginController do
with {:ok, user} <- Usuario.fetch_by(cpf: cpf),
email = user.contato.email_principal,
params = %{email: email, password: password},
%Plug.Conn{} = conn <- GoTrue.Plug.log_in_with_password(conn, params) do
%Plug.Conn{} = conn <- Auth.log_in_with_password(conn, params) do
conn
else
err ->
Expand All @@ -41,7 +41,7 @@ defmodule PescarteWeb.LoginController do
def delete(conn, _params) do
conn
|> put_flash(:info, "Desconectado com sucesso")
|> GoTrue.Plug.log_out_user(:local)
|> Auth.log_out_user(:local)
end

defp to_live_view_session(conn) do
Expand Down
18 changes: 11 additions & 7 deletions lib/pescarte_web/controllers/token_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule PescarteWeb.TokenController do

import Phoenix.LiveView.Controller, only: [live_render: 2]

alias Pescarte.Supabase.Auth
alias PescarteWeb.Auth
alias PescarteWeb.LoginLive
alias Supabase.GoTrue

Expand All @@ -13,10 +13,12 @@ defmodule PescarteWeb.TokenController do
@cannot_reset_err "Você não possui permissão para acessar essa página"

def confirm(conn, %{"token_hash" => token, "type" => "signup"}) do
with {:ok, session} <- Auth.verify_otp(%{token_hash: token, type: :signup}),
{:ok, _} <- Auth.get_user(session) do
with {:ok, client} <- Pescarte.get_supabase_client(),
params = %{token_hash: token, type: :signup},
{:ok, session} <- GoTrue.verify_otp(client, params),
{:ok, _} <- GoTrue.get_user(client, session) do
conn
|> GoTrue.Plug.put_token_in_session(session.access_token)
|> Auth.put_token_in_session(session.access_token)
|> redirect(to: ~p"/app/pesquisa/perfil?type=recovery")
else
{:error, %{"error_code" => "otp_expired"}} ->
Expand All @@ -41,10 +43,12 @@ defmodule PescarteWeb.TokenController do
end

def confirm(conn, %{"token_hash" => token, "type" => "recovery"}) do
with {:ok, session} <- Auth.verify_otp(%{token_hash: token, type: :recovery}),
{:ok, _} <- Auth.get_user(session) do
with {:ok, client} <- Pescarte.get_supabase_client(),
params = %{token_hash: token, type: :recovery},
{:ok, session} <- GoTrue.verify_otp(client, params),
{:ok, _} <- GoTrue.get_user(client, session) do
conn
|> GoTrue.Plug.put_token_in_session(session.access_token)
|> Auth.put_token_in_session(session.access_token)
|> redirect(to: ~p"/app/pesquisa/perfil?type=recovery")
else
{:error, %{"error_code" => "otp_expired"}} ->
Expand Down
2 changes: 1 addition & 1 deletion lib/pescarte_web/gettext.ex
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
defmodule PescarteWeb.Gettext do
use Gettext, otp_app: :pescarte
use Gettext.Backend, otp_app: :pescarte
end
6 changes: 4 additions & 2 deletions lib/pescarte_web/graphql/context.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule PescarteWeb.GraphQL.Context do
import Plug.Conn

alias Pescarte.Identidades.Models.Usuario
alias Supabase.GoTrue

def init(opts), do: opts

Expand All @@ -25,9 +26,10 @@ defmodule PescarteWeb.GraphQL.Context do
if Pescarte.env() == :test do
Usuario.fetch_by(id: token)
else
session = %Supabase.GoTrue.Session{access_token: token}
session = %GoTrue.Session{access_token: token}

with {:ok, user} <- Pescarte.Supabase.Auth.get_user(session) do
with {:ok, client} <- Pescarte.get_supabase_client(),
{:ok, user} <- GoTrue.get_user(client, session) do
Usuario.fetch_by(external_customer_id: user.id)
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/pescarte_web/graphql/resolver/login.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule PescarteWeb.GraphQL.Resolver.Login do
alias Pescarte.Identidades.Models.Usuario

alias Pescarte.Supabase.Auth
alias PescarteWeb.Auth

def resolve(%{input: %{cpf: cpf, senha: password}}, _resolution) do
if Pescarte.env() == :test do
Expand All @@ -12,7 +12,7 @@ defmodule PescarteWeb.GraphQL.Resolver.Login do
with {:ok, user} <- Usuario.fetch_by(cpf: cpf),
email = user.contato.email_principal,
params = %{email: email, password: password},
{:ok, session} <- Auth.sign_in_with_password(params) do
{:ok, session} <- Auth.log_in_with_password(params) do
{:ok, %{usuario: user, token: session.access_token}}
end
end
Expand Down
Loading

0 comments on commit 7853cf7

Please sign in to comment.