Comments (2)
Would something like this be a good workaround?
defmodule MyApp.Guardian do
use Guardian, otp_app: :my_app
defoverridable [encode_and_sign: 3]
...
@spec encode_and_sign(any, Guardian.Token.claims(), Guardian.options()) ::
{:ok, Guardian.Token.token(), Guardian.Token.claims()} | {:error, any}
def encode_and_sign(resource, claims \\ %{}, opts \\ []) do
mod = __MODULE__
claims =
claims
|> Enum.into(%{})
|> Guardian.stringify_keys()
token_mod = Guardian.token_module(mod)
with {:ok, subject} <- Guardian.returning_tuple({mod, :subject_for_token, [resource, claims]}),
{:ok, claims} <- Guardian.returning_tuple({token_mod, :build_claims, [mod, resource, subject, claims, opts]}),
{:ok, claims} <- Guardian.returning_tuple({mod, :build_claims, [claims, resource, opts]}),
{:ok, token} <- Guardian.returning_tuple({token_mod, :create_token, [mod, claims, opts]}),
{:ok, token} <- Guardian.returning_tuple({mod, :after_encode_and_sign, [resource, claims, token, opts]}) do
{:ok, token, claims}
end
end
...
end
from guardian.
After a quick search: https://github.com/search?q=after_encode_and_sign+language%3AElixir&type=code&l=Elixir&p=1
They are many people who misuse Guardian.DB.after_encode_and_sign/4
function which does not return {:ok, Guardian.Token.token()} | {:error, atom}
but instead they it returns {:ok, {resource, type, claims, jwt}}
Based on the contract alone, it is safe to assume they are making a mistake today.
But I digress.
I could have just made a Pull Request, but I thought perhaps this behavior is what some people actually want?
The token was already created at this point in the pipeline, so you wouldn't "modify" the token.
I do agree with you that it is weird expecting {:ok, token}
back instead of :ok.
The tricky situation is that it would be a breaking change to change the signature.
@doomspork do you remember what was your intent here? It feels that it should be :ok
as a return (aside from the breaking change situation).
from guardian.
Related Issues (20)
- Compilation error in file guardian.ex: type options/0 undefined HOT 1
- Is guardian relying on JWT's fixed side ? HOT 2
- Provide a way to retrieve verifying secret at runtime using connection information
- Help with EnsureNotAuthenticated HOT 1
- Successfully validates a JWT token with a timestamp in the past
- use Bitwise is deprecated in elixir 1.14-dev
- Warning causing compile error with Elixir 1.14.0 HOT 1
- Unable to set secret_key in runtime.exs HOT 12
- changelog is missing in the doc HOT 1
- all_permissions? function returns true even if permission set given is not within the list of permissions provided and compiled
- I would like to be able to set permissions from a DB table rather than hard coded in a permissions set. HOT 8
- guardian.encode_and_sign 1st argument: not an atom HOT 1
- Opaque errors when token verification fails HOT 3
- Using an environment variable as a secret key in elixir 1.14 HOT 2
- Breaking Change on 2.3.1 - Can't encode Bitwise Permissions HOT 6
- Generating a JWT with kty=oct can fail because of incorrect Base64 encoding
- Move to Elixir 1.14 and otp 25 HOT 3
- system_time vs os_time and time drift HOT 1
- refresh_from_cookie only accepts "default" as key HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from guardian.