~sbaildon/yemma

yemma/lib/yemma.ex -rw-r--r-- 4.2 KiB
38e5b1faSean Baildon user_settings_url with conn 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
defmodule Yemma do
  @moduledoc """
  Yemma keeps the contexts that define your domain
  and business logic.

  Contexts are also responsible for managing your data, regardless
  if it comes from the database, an external API or others.
  """
  use GenServer

  alias Yemma.{Config, Users}
  alias YemmaWeb.UserAuth

  def start_link(opts) do
    conf = Config.new(opts)
    GenServer.start_link(__MODULE__, conf, name: __MODULE__)
  end

  @impl true
  def init(conf) do
    {:ok, conf}
  end

  @impl true
  def handle_call(:config, _from, conf) do
    {:reply, conf, conf}
  end

  @type name :: term

  @spec config(name()) :: Config.t()
  def config(name \\ __MODULE__) do
    GenServer.call(name, :config)
  end

  def log_in_user(name \\ __MODULE__, conn, user) do
    name |> config() |> UserAuth.log_in_user(conn, user)
  end

  def log_out_user(name \\ __MODULE__, conn) do
    name |> config() |> UserAuth.log_out_user(conn)
  end

  def fetch_current_user(name \\ __MODULE__, conn, opts) do
    name |> config() |> UserAuth.fetch_current_user(conn, opts)
  end

  def require_authenticated_user(name \\ __MODULE__, conn, opts) do
    name |> config() |> UserAuth.require_authenticated_user(conn, opts)
  end

  @doc """
  Used for routes that require the user to not be authenticated.

  ## Options

    * `:to` - the destination to redirect the user to if they're already
      authenticated

  ## Examples

      plug :redirect_if_user_is_authenticated

  Create a pipeline to redirect users to your dashboard if they're already authenticated

      pipeline :requires_unauthenticated_user do
        plug :redirect_if_user_is_authenticated
      end

      scope "/", MyAppWeb do
        pipe_through [:browser, :requires_unauthenticated_user]

        get("/", PageController, :index)
      end

      scope "/dashboard", MyAppWeb do
        pipe_through [:browser]

        get("/", DashboardController, :index)
      end

  """
  def redirect_if_user_is_authenticated(name \\ __MODULE__, conn, opts) do
    name |> config() |> UserAuth.redirect_if_user_is_authenticated(conn, opts)
  end

  def get_user_by_email(name \\ __MODULE__, email) when is_binary(email) do
    name |> config() |> Users.get_user_by_email(email)
  end

  def get_user!(name \\ __MODULE__, id) do
    name |> config() |> Users.get_user!(id)
  end

  def register_user(name \\ __MODULE__, attrs) do
    name |> config() |> Users.register_user(attrs)
  end

  def register_or_get_by_email(name \\ __MODULE__, email) when is_binary(email) do
    name |> config() |> Users.register_or_get_by_email(email)
  end

  defdelegate change_user_registration(user, attrs \\ %{}), to: Users
  defdelegate change_user_email(user, attrs \\ %{}), to: Users
  defdelegate apply_user_email(user, attrs), to: Users

  def update_user_email(name \\ __MODULE__, user, token) do
    name |> config() |> Users.update_user_email(user, token)
  end

  def deliver_update_email_instructions(
        name \\ __MODULE__,
        user,
        current_email,
        update_email_url_fun
      ) do
    name
    |> config()
    |> Users.deliver_update_email_instructions(user, current_email, update_email_url_fun)
  end

  def generate_user_session_token(name \\ __MODULE__, user) do
    name |> config() |> Users.generate_user_session_token(user)
  end

  def get_user_by_session_token(name \\ __MODULE__, token) do
    name |> config() |> Users.get_user_by_session_token(token)
  end

  def delete_session_token(name \\ __MODULE__, token) do
    name |> config() |> Users.delete_session_token(token)
  end

  def deliver_magic_link_instructions(
        name \\ __MODULE__,
        user,
        magic_link_email_url_fun
      ) do
    name
    |> config()
    |> Users.deliver_magic_link_instructions(user, magic_link_email_url_fun)
  end

  def confirm_user(name \\ __MODULE__, token) do
    name |> config() |> Users.confirm_user(token)
  end

  def prepare_yemma(conn, opts) do
    conf =
      Keyword.get(opts, :name, __MODULE__)
      |> config()

    UserAuth.put_private(conf, conn)
  end

  def new_session_url(name \\ __MODULE__, conn, params \\ []) do
    conf = name |> config()

    conf.routes.new_session_url(conn, params)
  end

  def sign_out_url(name \\ __MODULE__, conn) do
    conf = name |> config()

    conf.routes.delete_session_url(conn)
  end
end