mirror of
https://github.com/kevin-DL/phoenix_api_template.git
synced 2026-01-11 18:54:33 +00:00
Adding the user to the session
This commit is contained in:
27
lib/phoenix_api_template_web/auth/set_user.ex
Normal file
27
lib/phoenix_api_template_web/auth/set_user.ex
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
defmodule PhoenixApiTemplateWeb.Auth.SetUser do
|
||||||
|
import Plug.Conn
|
||||||
|
alias PhoenixApiTemplateWeb.Auth.ErrorResponse
|
||||||
|
alias PhoenixApiTemplate.Accounts
|
||||||
|
|
||||||
|
def init(_options) do
|
||||||
|
end
|
||||||
|
|
||||||
|
def call(conn, _options) do
|
||||||
|
if conn.assigns[:user] do
|
||||||
|
conn
|
||||||
|
else
|
||||||
|
user_id = get_session(conn, :user_id)
|
||||||
|
|
||||||
|
if user_id == nil do
|
||||||
|
raise ErrorResponse.Unauthorized
|
||||||
|
end
|
||||||
|
|
||||||
|
user = Accounts.get_user!(user_id)
|
||||||
|
|
||||||
|
cond do
|
||||||
|
user_id && user -> assign(conn, :user, user)
|
||||||
|
true -> assign(conn, :user, nil)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -29,6 +29,7 @@ defmodule PhoenixApiTemplateWeb.UserController do
|
|||||||
case Guardian.authenticate(email, password) do
|
case Guardian.authenticate(email, password) do
|
||||||
{:ok, user, token} ->
|
{:ok, user, token} ->
|
||||||
conn
|
conn
|
||||||
|
|> Plug.Conn.put_session(:user_id, user.id)
|
||||||
|> put_status(:ok)
|
|> put_status(:ok)
|
||||||
|> render("user_token.json", %{user: user, token: token})
|
|> render("user_token.json", %{user: user, token: token})
|
||||||
|
|
||||||
|
|||||||
@@ -16,10 +16,12 @@ defmodule PhoenixApiTemplateWeb.Router do
|
|||||||
|
|
||||||
pipeline :api do
|
pipeline :api do
|
||||||
plug(:accepts, ["json"])
|
plug(:accepts, ["json"])
|
||||||
|
plug :fetch_session
|
||||||
end
|
end
|
||||||
|
|
||||||
pipeline :auth do
|
pipeline :auth do
|
||||||
plug PhoenixApiTemplateWeb.Auth.Pipeline
|
plug PhoenixApiTemplateWeb.Auth.Pipeline
|
||||||
|
plug PhoenixApiTemplateWeb.Auth.SetUser
|
||||||
end
|
end
|
||||||
|
|
||||||
scope "/api", PhoenixApiTemplateWeb do
|
scope "/api", PhoenixApiTemplateWeb do
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
GET http://localhost:4000/api/users/by_id/eae6f03c-6276-48e3-b6df-0797b2f8cb99 HTTP/1.1
|
GET http://localhost:4000/api/users/by_id/f0818034-be59-4618-bf16-0632bbef3a72 HTTP/1.1
|
||||||
content-type: application/json
|
content-type: application/json
|
||||||
Authorization: Bearer eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJwaG9lbml4X2FwaV90ZW1wbGF0ZSIsImV4cCI6MTY3OTEzODAxMiwiaWF0IjoxNjc2NzE4ODEyLCJpc3MiOiJwaG9lbml4X2FwaV90ZW1wbGF0ZSIsImp0aSI6ImRmYjc5NDExLTgwY2QtNDNkZC1hYmU0LWMxYjg2OWJhODI4YyIsIm5iZiI6MTY3NjcxODgxMSwic3ViIjoiZWFlNmYwM2MtNjI3Ni00OGUzLWI2ZGYtMDc5N2IyZjhjYjk5IiwidHlwIjoiYWNjZXNzIn0.b1F2a57dv4hCp1015--QPaE3bEVePXNeg-JajkqA_PGxKZx_kJoedLt8KIuCDTVAF-Sn--iWr1miOUowwgbwhA
|
Authorization: Bearer eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJwaG9lbml4X2FwaV90ZW1wbGF0ZSIsImV4cCI6MTY3OTE1Njk5NiwiaWF0IjoxNjc2NzM3Nzk2LCJpc3MiOiJwaG9lbml4X2FwaV90ZW1wbGF0ZSIsImp0aSI6Ijk0ZjJlOGQ5LTJkZmYtNDM4Zi1hY2Y4LWZiMzAwODJmZDU2YiIsIm5iZiI6MTY3NjczNzc5NSwic3ViIjoiZWFlNmYwM2MtNjI3Ni00OGUzLWI2ZGYtMDc5N2IyZjhjYjk5IiwidHlwIjoiYWNjZXNzIn0.32jNDsUQZemN6V_sR8xZtmlQp1kECPEcS63yCR655HlyWYsaNYCF3t4Wi37to6lmYUuE8QUD0qI3BHkqhroScQ
|
||||||
|
|
||||||
Reference in New Issue
Block a user