Bouncer.Session

A library of functions used to work with session data.

Summary

Functions

Makes the adapter specified in the environment configuration available

Creates a session for a given user and saves it to the session store. Returns the session token which will be used as the API authorization token

Destroys a session by removing session data from the store

Retrieves session data given a token (key) and assigns it to the connection

Parses JSON session data retrieved from the store into a map

Saves session data to the session store using a key

Convenience function to determine if the ID from the current_user in the request matches the given User ID

Verifies that the ID deciphered from the received token matches the ID in the session data

Functions

adapter()

Makes the adapter specified in the environment configuration available.

create(conn, user)

Creates a session for a given user and saves it to the session store. Returns the session token which will be used as the API authorization token.

destroy(key)

Destroys a session by removing session data from the store.

get(conn, token)

Retrieves session data given a token (key) and assigns it to the connection.

parse_data(arg1)

Parses JSON session data retrieved from the store into a map.

Examples

iex> Bouncer.Session.parse_data {:ok, ~s({"id": 1})}
{:ok, %{id: 1}}
iex> Bouncer.Session.parse_data {:error, nil}
{:error, nil}
iex> Bouncer.Session.parse_data {:ok, ""}
{:error, :invalid}
save(data, key)

Saves session data to the session store using a key.

user_request?(conn, id)

Convenience function to determine if the ID from the current_user in the request matches the given User ID.

examples

iex> Bouncer.Session.user_request? %{assigns: %{current_user: %{id: 1}}}, 1
true
iex> Bouncer.Session.user_request? %{assigns: %{current_user: %{id: 1}}}, 2
false
iex> Bouncer.Session.user_request? %{assigns: %{}}, 1
false
verify_user_match(arg1, id)

Verifies that the ID deciphered from the received token matches the ID in the session data.

Examples

iex> Bouncer.Session.verify_user_match {:ok, %{id: 1}}, 1
{:ok, %{id: 1}}
iex> Bouncer.Session.verify_user_match {:ok, %{id: 2}}, 1
{:error, "Token ID does not match session data ID"}
iex> Bouncer.Session.verify_user_match {:error, nil}, 1
{:error, nil}