-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Setup guardian session sharing with main app
- Loading branch information
Showing
17 changed files
with
179 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
defmodule Opsmaru.Accounts.User do | ||
use Ecto.Schema | ||
import Ecto.Changeset | ||
|
||
embedded_schema do | ||
field :username, :string | ||
end | ||
|
||
def changeset(user, attrs) do | ||
user | ||
|> cast(attrs, [:id, :username]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
defmodule Opsmaru.Guardian do | ||
use Guardian, otp_app: :opsmaru | ||
|
||
alias Opsmaru.Accounts.User | ||
|
||
def subject_for_token(%{id: id}, _claims) do | ||
sub = to_string(id) | ||
|
||
{:ok, sub} | ||
end | ||
|
||
def subject_for_token(_, _) do | ||
{:error, :invalid_resource} | ||
end | ||
|
||
def resource_from_claims(%{"sub" => id}) do | ||
id = String.to_integer(id) | ||
|
||
resource = %User{id: id} | ||
|
||
{:ok, resource} | ||
end | ||
|
||
def resource_from_claims(_claims) do | ||
{:error, :invalid_claim} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
defmodule OpsmaruWeb.UserHook do | ||
import Phoenix.Component | ||
|
||
def on_mount(:current_user, _params, session, socket) do | ||
socket = mount_current_user(session, socket) | ||
|
||
{:cont, socket} | ||
end | ||
|
||
defp mount_current_user(session, socket) do | ||
with %{"guardian_default_token" => token} <- session, | ||
{:ok, user, _claims} <- Opsmaru.Guardian.resource_from_token(token) do | ||
socket | ||
|> assign_new(:current_user, fn -> user end) | ||
|> assign_new(:current_user_id, fn -> user.id end) | ||
else | ||
_ -> | ||
socket | ||
|> assign_new(:current_user, fn -> nil end) | ||
|> assign_new(:current_user_id, fn -> nil end) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
defmodule OpsmaruWeb.Plugs.BrowserErrorHandler do | ||
use OpsmaruWeb, :controller | ||
|
||
@behaviour Guardian.Plug.ErrorHandler | ||
|
||
@impl Guardian.Plug.ErrorHandler | ||
def auth_error(conn, {:unauthenticated, _reason}, _opts) do | ||
redirect(conn, to: "/auth/users/log_in") | ||
end | ||
|
||
def auth_error(conn, {:invalid_token, :token_expired}, _opts) do | ||
conn | ||
|> Opsmaru.Guardian.Plug.sign_out() | ||
|> redirect(to: "/auth/users/log_in") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,19 @@ | ||
{ | ||
"name": "", | ||
"short_name": "", | ||
"icons": [ | ||
{ | ||
"src": "/android-chrome-192x192.png", | ||
"sizes": "192x192", | ||
"type": "image/png" | ||
}, | ||
{ | ||
"src": "/android-chrome-512x512.png", | ||
"sizes": "512x512", | ||
"type": "image/png" | ||
} | ||
], | ||
"theme_color": "#ffffff", | ||
"background_color": "#ffffff", | ||
"display": "standalone" | ||
"name": "Opsmaru", | ||
"short_name": "Opsmaru", | ||
"icons": [ | ||
{ | ||
"src": "/site/images/android-chrome-192x192.png", | ||
"sizes": "192x192", | ||
"type": "image/png" | ||
}, | ||
{ | ||
"src": "/site/images/android-chrome-512x512.png", | ||
"sizes": "512x512", | ||
"type": "image/png" | ||
} | ||
], | ||
"theme_color": "#ffffff", | ||
"background_color": "#ffffff", | ||
"display": "standalone" | ||
} |