31 lines
700 B
Elixir
31 lines
700 B
Elixir
|
|
defmodule EzcontainerRailwayWeb.SessionController do
|
||
|
|
use EzcontainerRailwayWeb, :controller
|
||
|
|
|
||
|
|
def token(conn, %{"railway_token" => token}) do
|
||
|
|
case token_valid?(token) do
|
||
|
|
true ->
|
||
|
|
conn
|
||
|
|
|> put_session(:railway_token, token)
|
||
|
|
|> assign(:railway_token, token)
|
||
|
|
|> redirect(to: "/containers")
|
||
|
|
false ->
|
||
|
|
conn
|
||
|
|
|> put_flash(:error, "Invalid Token")
|
||
|
|
|> redirect(to: "/")
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
defp token_valid?(token) do
|
||
|
|
resp = EzcontainerRailway.Railway.get_projects(token).body
|
||
|
|
|
||
|
|
if has_errors?(resp["errors"]) do
|
||
|
|
false
|
||
|
|
else
|
||
|
|
true
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
defp has_errors?([_|_]), do: true
|
||
|
|
defp has_errors?(_), do: false
|
||
|
|
end
|