1
0
mirror of https://github.com/mgerb/classic-wow-forums synced 2026-01-11 09:32:51 +00:00

authentication with persistence working

This commit is contained in:
2018-01-01 18:55:00 -06:00
parent e856cc5172
commit 135ce3a5d6
14 changed files with 172 additions and 38 deletions

View File

@@ -4,11 +4,15 @@ defmodule MyApp.JWT do
# ~1 year
defp tokenTTL(), do: {52, :weeks}
def get_jwt(user, claims) do
case Guardian.encode_and_sign(user, claims, ttl: tokenTTL()) do
{:ok, token, _claims} -> {:ok, token}
{:error, _token, _claims} -> {:error, "JWT error"}
@spec add_jwt(map | {atom, any}) :: {:ok, map} | {:error, String.t}
def add_jwt(user) when is_map(user) do
case Guardian.encode_and_sign(user, user, ttl: tokenTTL()) do
{:ok, token, _claims} -> {:ok, Map.merge(user, %{token: token})}
{:error, error} -> {:error, error}
end
end
def add_jwt({:ok, user}), do: add_jwt(user)
def add_jwt({:error, error}), do: {:error, error}
end